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. // 9 3
  28. // 1 1 -1 4 -1 0 -1 2 -1
  29.  
  30.  
  31.  
  32.  
  33. vector<int> a;
  34.  
  35. vector<int> consistency(int n, int k) {
  36.  
  37. unordered_map<int,int> mp = {{0, -1}};
  38. int sum = 0;
  39. int maxLen = 0;
  40.  
  41. for(int i=0; i<n; i++){
  42.  
  43. sum += a[i];
  44. if(mp.count(sum-k)){
  45. maxLen = max(maxLen, i-mp[sum-k]);
  46. }
  47.  
  48. if(!mp.count(sum)) mp[sum] = i;
  49. }
  50.  
  51. int e=0, s=0;
  52. sum = 0;
  53. while(e<n){
  54. sum += a[e];
  55. if(e-s+1<maxLen) e++;
  56. else{
  57. if(sum == k){
  58. return {s+1, e+1};
  59. }
  60. sum -= a[s];
  61. e++;
  62. s++;
  63. }
  64. }
  65.  
  66. return {};
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. vector<int> practice(int n, int k){
  86.  
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93. void solve() {
  94.  
  95. int n, k;
  96. cin >> n >> k;
  97.  
  98. a.resize(n);
  99. for(int i=0; i<n; i++) cin >> a[i];
  100.  
  101. auto ans = consistency(n, k) ;
  102. if(ans.size() == 0){
  103. cout << "-1" << endl;
  104. return;
  105. }
  106. for(auto& t : ans) cout << t << " "; cout << endl;
  107.  
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. int32_t main() {
  115. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  116.  
  117. int t = 1;
  118. // cin >> t;
  119. while (t--) {
  120. solve();
  121. }
  122.  
  123. return 0;
  124. }
Success #stdin #stdout 0.01s 5276KB
stdin
9 3
1 1 -1 4 -1 0 -1 2 -1  
stdout
2 9