Advertisement
pasholnahuy

префиксы

May 28th, 2023
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <tuple>
  3. #include <random>
  4.  
  5.  
  6. using std::pair;
  7. using std::cin;
  8. using std::cout;
  9. using std::vector;
  10. using int64 = int64_t;
  11. using std::max;
  12. using std::min;
  13.  
  14. int main() {
  15.     std::ios_base::sync_with_stdio(false);
  16.     cin.tie(nullptr);
  17.     int n, m;
  18.     cin >> n >> m;
  19.     vector<int64> val(n);
  20.     for (size_t i = 0; i < n; ++i) {
  21.         cin >> val[i];
  22.     }
  23.     vector<int64> pref(n);
  24.     pref[0] = val[0];
  25.     for (int i = 1; i < pref.size(); ++i){
  26.         pref[i] = val[i] + pref[i-1];
  27.     }
  28.     for (size_t i = 0; i < m; ++i){
  29.         int l, r;
  30.         cin >> l >> r, --l, --r;
  31.         if (l == 0){
  32.             cout << pref[r] << '\n';
  33.         } else {
  34.             cout << pref[r] - pref[l-1] << '\n';
  35.         }
  36.     }
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement