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. unordered_map<int,int> mp = {{0, -1}};
  29. int sum = 0;
  30. int ans = 0;
  31. for(int i=0; i<n; i++){
  32. sum += a[i];
  33. if(mp.count(sum-k)){
  34. ans = max(ans, i-mp[sum-k]);
  35. }
  36.  
  37. if(!mp.count(sum)) mp[sum] = i;
  38. }
  39.  
  40. int e=0, s=0;
  41. sum = 0;
  42. while(e<n){
  43. sum += a[e];
  44. if(e-s+1<ans) e++;
  45. else{
  46. if(sum == k){
  47. cout << s+1 << " " << e+1 << endl;
  48. return;
  49. }
  50. sum -= a[s];
  51. e++;
  52. s++;
  53. }
  54. }
  55. cout << "-1" << endl;
  56. }
  57.  
  58. void solve() {
  59.  
  60. int n, k;
  61. cin >> n >> k;
  62. a.resize(n);
  63. for(int i=0; i<n; i++) cin >> a[i];
  64. consistency(n, k) ;
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72. int32_t main() {
  73. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  74.  
  75. int t = 1;
  76. while (t--) {
  77. solve();
  78. }
  79.  
  80. return 0;
  81. }
Success #stdin #stdout 0.01s 5284KB
stdin
7 9
1 2 3 4 5 -1 6
stdout
2 4