didedoshka

ДО Снизу

Nov 3rd, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <cmath>
  6. #include <set>
  7. #include <stack>
  8. #include <bitset>
  9. #include <map>
  10. #include <ctime>
  11. #include <numeric>
  12.  
  13.  
  14. #ifndef M_PI
  15. #define M_PI 3.141592653589
  16. #endif
  17. #define int long long
  18. #define double long double
  19.  
  20. #ifdef TIME
  21. #define start cin.tie(NULL); cout.tie(NULL); cout.setf(ios::fixed); cout.precision(10); ios_base::sync_with_stdio(false);int32_t START = clock()
  22. #define finish cout << "\ntime: " << (clock() - START) / (CLOCKS_PER_SEC * 1.0); return 0
  23. #endif
  24.  
  25. #ifndef TIME
  26. #define start cin.tie(NULL); cout.tie(NULL); cout.setf(ios::fixed); cout.precision(10); ios_base::sync_with_stdio(false)
  27. #define finish return 0
  28. #endif
  29.  
  30. using namespace std;
  31.  
  32.  
  33. //vector input
  34. template<typename T>
  35. istream &operator>>(istream &is, vector<T> &vec) {
  36.     for (auto &i : vec) {
  37.         cin >> i;
  38.     }
  39.     return is;
  40. }
  41.  
  42. //pair output
  43. template<typename E>
  44. ostream &operator<<(ostream &os, pair<E, E> &t) {
  45.     os << t.first << ' ' << t.second;
  46.     return os;
  47. }
  48.  
  49. //"map" pair output
  50. template<typename E>
  51. ostream &operator<<(ostream &os, pair<const E, E> &t) {
  52.     os << t.first << ' ' << t.second;
  53.     return os;
  54. }
  55.  
  56. //vector output
  57. template<typename T>
  58. ostream &operator<<(ostream &os, vector<T> &vec) {
  59.     for (T i : vec) {
  60.         os << i << ' ';
  61.     }
  62.     return os;
  63. }
  64.  
  65. //2 dimensional vector output
  66. template<typename T>
  67. ostream &operator<<(ostream &os, vector<vector<T> > &vec) {
  68.     for (vector<T> i : vec) {
  69.         os << i << '\n';
  70.     }
  71.     return os;
  72. }
  73.  
  74. struct segtree {
  75.     int n;
  76.     vector<int> tree;
  77.  
  78.     segtree(vector<int> &a) {
  79.         n = a.size();
  80.         tree.resize(2 * n);
  81.         for (int i = 0; i < n; ++i) {
  82.             tree[n + i] = a[i];
  83.         }
  84.         for (int i = n - 1; i >= 1; --i) {
  85.             tree[i] = tree[2 * i] + tree[2 * i + 1];
  86.         }
  87.     }
  88.  
  89.     void set(int i, int v) {
  90.         i += n;
  91.         tree[i] = v;
  92.         i /= 2;
  93.         while (i != 0) {
  94.             tree[i] = tree[2 * i] + tree[2 * i + 1];
  95.             i /= 2;
  96.         }
  97.     }
  98.  
  99.     int get(int l, int r) {
  100.         l += n;
  101.         r += n;
  102.         int ans = 0;
  103.         while (l <= r) {
  104.             if (l % 2 == 1) {
  105.                 ans += tree[l];
  106.                 l += 1;
  107.             }
  108.             if (r % 2 == 0) {
  109.                 ans += tree[r];
  110.                 r -= 1;
  111.             }
  112.             l /= 2;
  113.             r /= 2;
  114.         }
  115.         return ans;
  116.     }
  117. };
  118.  
  119. int32_t main() {
  120.     start;
  121.  
  122.     int n, m;
  123.     cin >> n >> m;
  124.     vector<int> a(n);
  125.     cin >> a;
  126.     segtree sgt(a);
  127.  
  128.     while (m--) {
  129.         int op;
  130.         cin >> op;
  131.         if (op == 1) {
  132.             int i, v;
  133.             cin >> i >> v;
  134.             sgt.set(i, v);
  135.         } else {
  136.             int l, r;
  137.             cin >> l >> r;
  138.             cout << sgt.get(l, r - 1) << '\n';
  139.         }
  140.     }
  141.  
  142.     finish;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment