fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. vector<int> a;
  33.  
  34. vector<int> consistency(int n, int k) {
  35.  
  36. unordered_map<int,int> mp = {{0, -1}};
  37. int sum = 0;
  38. int maxLen = 0;
  39. for(int i=0; i<n; i++){
  40. sum += a[i];
  41. if(mp.count(sum-k)){
  42. maxLen = max(maxLen, i-mp[sum-k]);
  43. }
  44.  
  45. if(!mp.count(sum)) mp[sum] = i;
  46. }
  47.  
  48. int e=0, s=0;
  49. sum = 0;
  50. while(e<n){
  51. sum += a[e];
  52. if(e-s+1<maxLen) e++;
  53. else{
  54. if(sum == k){
  55. return {s+1, e+1};
  56. }
  57. sum -= a[s];
  58. e++;
  59. s++;
  60. }
  61. }
  62. return {};
  63. }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. vector<int> practice(int n, int k){
  82.  
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89. void solve() {
  90.  
  91. int n, k;
  92. cin >> n >> k;
  93.  
  94. a.resize(n);
  95. for(int i=0; i<n; i++) cin >> a[i];
  96.  
  97. auto ans = consistency(n, k) ;
  98. if(ans.size() == 0){
  99. cout << "-1" << endl;
  100. return;
  101. }
  102. for(auto& t : ans) cout << t << " "; cout << endl;
  103.  
  104. }
  105.  
  106.  
  107.  
  108.  
  109.  
  110. int32_t main() {
  111. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  112.  
  113. int t = 1;
  114. // cin >> t;
  115. while (t--) {
  116. solve();
  117. }
  118.  
  119. return 0;
  120. }
Success #stdin #stdout 0s 5320KB
stdin
7 9
1 2 3 4 5 -1 6
stdout
2 4