pratiyush7

Segment Tree(Sum)

Nov 12th, 2021
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. // Pratiyush Mishra
  2.  
  3. #include <bits/stdc++.h>
  4. #define ull unsigned long long int
  5. #define ll long long int
  6. #define LL_MAX 9223372036854775807
  7. #define pb push_back
  8. #define pf push_front
  9. #define mp make_pair
  10. #define popb pop_back
  11. #define vl vector<ll>
  12. #define pl pair<ll,ll>
  13. #define bs(v, x) binary_search(v.begin(), v.end(), x)
  14. #define mem1(a) memset(a, -1, sizeof(a))
  15. #define popf pop_front
  16. #define p0(x) cout << x << " "
  17. #define p1(x) cout << x << '\n'
  18. #define p2(x, y) cout << x << " " << y << '\n'
  19. #define p3(x, y, z) cout << x << " " << y << " " << z << '\n'
  20. #define printv(v)                   \
  21.   for (ll i = 0; i < v.size(); ++i) \
  22.     cout << v[i] << " ";            \
  23.   cout << '\n'
  24. #define pr1(x) cout << fixed << setprecision(15) << x << '\n'
  25. #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update>
  26. // ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
  27. // s.find_by_order(i)  itertor to ith element (0 indexed)
  28. #define mod 1000000007
  29. #define mod1 998244353
  30. #define fio                         \
  31.   ios_base::sync_with_stdio(false); \
  32.   cin.tie(NULL)
  33. #define get(n) \
  34.   ll n;        \
  35.   cin >> n
  36. #define getvec(v, n)         \
  37.   vector<ll> v(n);           \
  38.   for (ll i = 0; i < n; i++) \
  39.     cin >> v[i];
  40. #define getstr(s) \
  41.   string s;       \
  42.   cin >> s
  43. #define all(x) x.begin(), x.end()
  44. #define countBits(x) __builtin_popcount(x)
  45. using namespace std;
  46.  
  47. // Solves the following problem: https://codeforces.com/edu/course/2/lesson/4/1/practice/contest/273169/problem/A
  48.  
  49. const ll M = 1e6;
  50. ll v[M], tree[M];
  51. ll n, q, l, r, pos, val, t;
  52.  
  53. void build(int tl, int tr, ll node)
  54. {
  55.     if (tl == tr)
  56.     {
  57.         tree[node] = v[tl];
  58.         return;
  59.     }
  60.     int tm = (tl + tr) / 2;
  61.     build(tl, tm, 2 * node);
  62.     build(tm + 1, tr, 2 * node + 1);
  63.     tree[node] = tree[2 * node] + tree[2 * node + 1];
  64. }
  65.  
  66. void update(int tl, int tr, ll node)
  67. {
  68.     if (tl == tr)
  69.     {
  70.         tree[node] = val;
  71.         v[pos] = val;
  72.         return;
  73.     }
  74.     int tm = (tl + tr) / 2;
  75.     if (pos <= tm)
  76.         update(tl, tm, 2 * node);
  77.     else
  78.         update(tm + 1, tr, 2 * node + 1);
  79.     tree[node] = (tree[2 * node] + tree[2 * node + 1]);
  80. }
  81.  
  82. ll query(int tl, int tr, ll node)
  83. {
  84.     if (tl > r || tr < l)
  85.         return 0;
  86.     if (tl >= l && tr <= r)
  87.         return tree[node];
  88.     int tm = (tl + tr) / 2;
  89.     ll ans = query(tl, tm, 2 * node) + query(tm + 1, tr, 2 * node + 1);
  90.     return ans;
  91. }
  92.  
  93. void mainSolve()
  94. {
  95.     cin >> n >> q;
  96.     for (int i = 1; i <= n; i++)
  97.         cin >> v[i];
  98.     build(1, n, 1);
  99.     while (q--)
  100.     {
  101.         cin >> t;
  102.         if (t == 1)
  103.         {
  104.             cin >> pos >> val;
  105.             ++pos;
  106.             update(1, n, 1);
  107.         }
  108.         else
  109.         {
  110.             cin >> l >> r;
  111.             ++l;
  112.             cout << query(1, n, 1) << endl;
  113.         }
  114.     }
  115. }
  116.  
  117. int main()
  118. {
  119.     fio;
  120. #ifndef ONLINE_JUDGE
  121.     freopen("input.txt", "r", stdin);
  122.     freopen("output.txt", "w", stdout);
  123. #endif
  124.     //get(t);
  125.     ll t = 1;
  126.     while (t--)
  127.     {
  128.         mainSolve();
  129.     }
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment