Advertisement
Josif_tepe

Untitled

Feb 13th, 2023
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <set>
  5. #include <cmath>
  6. #include <map>
  7. #include <cstring>
  8. using namespace std;
  9. vector<long long> prefix_sum;
  10.  
  11. long long query(int a, int b) {
  12.     if(a == 0) {
  13.         return prefix_sum[b];
  14.     }
  15.     return prefix_sum[b] - prefix_sum[a - 1];
  16. }
  17. int main() {
  18.     int n, q;
  19.     cin >> n >> q;
  20.    
  21.     vector<int> v(n);
  22.     long long sum = 0;
  23.     for(int i = 0; i < n; i++) {
  24.         cin >> v[i];
  25.         sum += v[i];
  26.         prefix_sum.push_back(sum);
  27.     }
  28.    
  29.    
  30.     for(int i = 0; i < q; i++) {
  31.         int a, b;
  32.         cin >> a >> b;
  33.         a--;
  34.         b--;
  35.        
  36.         cout << query(a, b) << endl;
  37.        
  38.     }
  39.    
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement