fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int a[11111];
  6.  
  7. int query(int l, int sum, int r) {
  8. r++;
  9. sum += a[l];
  10. while (l + 1 < r) {
  11. int k = (l + r) / 2;
  12. if (a[k] <= sum) l = k;
  13. else r = k;
  14. }
  15. return l;
  16. }
  17.  
  18. int main() {
  19. // your code goes here
  20. int n, m;
  21. cin >> n >> m;
  22. for (int i=0; i < n; i++) {
  23. int t;
  24. cin >> t;
  25. a[i+1] = a[i] + t;
  26. cout << a[i+1] << " ";
  27. }
  28. cout << endl;
  29. for (int j= 0; j < m; j++) {
  30. int a, b;
  31. cin >> a >> b;
  32. cout << query(a,b,n) << endl;
  33. }
  34. return 0;
  35. }
Success #stdin #stdout 0s 5520KB
stdin
10 7
1
4
0
5
6
0
0
1
5
3
0 100
0 5
4 11
4 12
4 13
10 100
stdout
1 5 5 10 16 16 16 17 22 25 
10
3
8
9
9
10
10