fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. vector<int> a;
  26. void consistency(int n, int k) {
  27.  
  28. int sum = 0;
  29. int len = 0;
  30. unordered_map<int,int> mp = {{0, 0}};
  31.  
  32. for(int i=0; i<n; i++){
  33. sum += a[i];
  34. if(mp.count(sum-k)){
  35. len = max(len, i-mp[sum-k]);
  36. }
  37. if(!mp.count(sum)) mp[sum] = i;
  38. }
  39.  
  40. int s = 0 , e= 0;
  41. sum = 0;
  42. while(e<n){
  43. sum += a[e];
  44. if(e-s+1<len){
  45. e++;
  46. }else{
  47. if(sum==k){
  48. cout << s << ", " << e << endl;
  49. return;
  50. }
  51. sum -= a[s];
  52. s++;
  53. e++;
  54. }
  55. }
  56. cout << "-1" << endl;
  57.  
  58. }
  59.  
  60. void solve() {
  61.  
  62. int n, k;
  63. cin >> n >> k;
  64. a.resize(n);
  65. for(int i=0; i<n; i++) cin >> a[i];
  66. consistency(n, k) ;
  67.  
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74. int32_t main() {
  75. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  76.  
  77. int t = 1;
  78. while (t--) {
  79. solve();
  80. }
  81.  
  82. return 0;
  83. }
Success #stdin #stdout 0s 5320KB
stdin
7 9
1 2 3 4 5 -1 6
stdout
1, 3