Advertisement
MiinaMagdy

12086 - Potentiometers

May 26th, 2023
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. /*
  2. +---------------------------------------------+
  3. |                                             |
  4. |       © 26/05/2023 (20:23) MinaMagdy        |
  5. |                                             |
  6. +---------------------------------------------+
  7. */
  8. #include <bits/stdc++.h>
  9. #include <ext/pb_ds/assoc_container.hpp>
  10. #include <ext/pb_ds/tree_policy.hpp>
  11.  
  12. using namespace std;
  13. using namespace __gnu_pbds;
  14. #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  15. #define multi_ordered_set tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
  16. #define endl "\n"
  17. #define MOD 1000000007
  18. #define INF 2000000000
  19. #define all(s) s.begin(), s.end()
  20. #define rall(s) s.rbegin(), s.rend()
  21. #define sz(x) int(x.size())
  22.  
  23. typedef long long ll;
  24. typedef long double ld;
  25. typedef unsigned long long ull;
  26.  
  27. vector<ll> seg, arr;
  28. int sz, n;
  29.  
  30. void build(int N = 1, int l = 0, int r = sz - 1) {
  31.     if (l == r) {
  32.         seg[N] = (l < n ? arr[l] : 0);
  33.         return;
  34.     }
  35.     int m = l + (r - l) / 2;
  36.     build(N * 2, l, m), build(N * 2 + 1, m + 1, r);
  37.     seg[N] = seg[N * 2] + seg[N * 2 + 1];
  38. }
  39.  
  40. ll query(ll x, ll y, int N = 1, int l = 0, int r = sz - 1) {
  41.     if (y < l || x > r) return 0;
  42.     if (x <= l && r <= y) return seg[N];
  43.     int m = l + (r - l) / 2;
  44.     return query(x, y, N * 2, l, m) + query(x, y, N * 2 + 1, m + 1, r);
  45. }
  46.  
  47. void update(ll x, ll vl, int N = 1, int l = 0, int r = sz - 1) {
  48.     if (x < l || r < x) return;
  49.     if (l == r) {
  50.         seg[N] = vl;
  51.         return;
  52.     }
  53.     int m = l + (r - l) / 2;
  54.     update(x, vl, N * 2, l, m), update(x, vl, N * 2 + 1, m + 1, r);
  55.     seg[N] = seg[N * 2] + seg[N * 2 + 1];
  56.     return;
  57. }
  58.  
  59. void solve() {
  60.     int tc = 0;
  61.     while (cin >> n, n) {
  62.         if (tc)
  63.             cout << endl;
  64.         cout << "Case " << ++tc << ":\n";
  65.         arr.assign(n, 0);
  66.         for (int i = 0; i < n; i++) cin >> arr[i];
  67.         sz = 1;
  68.         while (sz < n) sz *= 2;
  69.         seg.assign(sz * 2, 0);
  70.         build();
  71.         char c;
  72.         while (cin >> c, c != 'E') {
  73.             ll x, y;
  74.             cin >> x >> y;
  75.             if (c == 'M') {
  76.                 --x, --y;
  77.                 cout << query(x, y) << endl;
  78.             }
  79.             else {
  80.                 --x;
  81.                 update(x, y);
  82.             }
  83.         }
  84.         cin >> c >> c; // for N, D in END
  85.     }
  86. }
  87.  
  88. int main(void)
  89. {
  90.     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  91.     int testcase = 1;
  92.     // cin >> testcase;
  93.     while (testcase--)
  94.         solve();
  95.     return 0;
  96. }
Tags: Bit Seg tree
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement