Advertisement
Mlxa

Простой splay на сумму и изменение в точке

Jan 21st, 2021
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #ifdef LC
  2. #include "pch.h"
  3. #else
  4. #include <bits/stdc++.h>
  5. #endif
  6. using namespace std;
  7. using ll = long long;
  8. #define all(x) x.begin(), x.end()
  9. #define x first
  10. #define y second
  11. #define mp make_pair
  12. #define mt make_tuple
  13.  
  14. const int N = 1e6;
  15.  
  16. struct node {
  17.     int s[2];
  18.     int p;
  19.     int f;
  20.     int v;
  21. } pool[N];
  22.  
  23. #define son(x, i) pool[x].s[i]
  24. #define ls(x) pool[x].s[0]
  25. #define rs(x) pool[x].s[1]
  26. #define pr(x) pool[x].p
  27. #define fn(x) pool[x].f
  28. #define va(x) pool[x].v
  29.  
  30. int who(int v) {
  31.     if (v == ls(pr(v))) {
  32.         return 0;
  33.     } else {
  34.         assert(v == rs(pr(v)));
  35.         return 1;
  36.     }
  37. }
  38.  
  39. int pull(int v) {
  40.     fn(v) = fn(ls(v)) + va(v) + fn(rs(v));
  41.     return v;
  42. }
  43.  
  44. void rot(int v) {
  45.     int p = pr(v);
  46.     // assert(p);
  47.     int g = pr(p);
  48.     int w = who(v);
  49.  
  50.     if (g) {
  51.         son(g, who(p)) = v;
  52.         pr(v) = g;
  53.         pull(g);
  54.     } else {
  55.         pr(v) = 0;
  56.     }
  57.  
  58.     int &u = son(v, w ^ 1);
  59.  
  60.     son(p, w) = u;
  61.     pr(u) = p;
  62.     pull(p);
  63.  
  64.     u = p;
  65.     pr(p) = v;
  66.     pull(v);
  67. }
  68.  
  69. void splay(int v) {
  70.     while (pr(v)) {
  71.         int p = pr(v), g = pr(p);
  72.         if (!g) {
  73.             rot(v);
  74.         } else if (who(v) == who(p)) {
  75.             rot(p);
  76.             rot(v);
  77.         } else {
  78.             rot(v);
  79.             rot(v);
  80.         }
  81.     }
  82. }
  83.  
  84. void print(int v, int h = 0) {
  85.     if (v) {
  86.         print(ls(v), h + 1);
  87.         cout << string(4 * h, ' ') << " " << v << endl;
  88.         print(rs(v), h + 1);
  89.     }
  90.     if (!h) {
  91.         cout << "======" << endl;
  92.     }
  93. }
  94.  
  95. int n, m;
  96. int t = 0;
  97.  
  98. int prefix(int v) {
  99.     if (!v) {
  100.         return 0;
  101.     }
  102.     splay(t = v);
  103.     return fn(ls(v)) + va(v);
  104. }
  105.  
  106. int query(int l, int r) {
  107.     return prefix(r) - prefix(l - 1);
  108. }
  109.  
  110. void update(int x, int y) {
  111.     va(x) = y;
  112.     pull(x);
  113.     splay(t = x);
  114. }
  115.  
  116. signed main() {
  117. #ifdef LC
  118.     assert(freopen("input.txt", "r", stdin));
  119. #endif
  120.     ios::sync_with_stdio(0); cin.tie(0);
  121.  
  122.     mt19937 rnd;
  123.     cin >> n >> m;
  124.     for (int i = 1; i <= n; ++i) {
  125.         cin >> va(i);
  126.         ls(i) = t;
  127.         pr(t) = i;
  128.         t = i;
  129.         pull(t);
  130.     }
  131.    
  132.     char tp;
  133.     int hhh = 0;
  134.     for (int x, y, i = 0; i < m; ++i) {
  135.         cin >> tp >> x >> y;
  136.         if (tp == '?') {
  137.             int cur = query(x, y);
  138.             hhh += cur;
  139.         } else {
  140.             update(x, y);
  141.         }
  142.     }
  143.  
  144.     cout << "#" << hhh << endl;
  145.     cout << 1000 * clock() / CLOCKS_PER_SEC << " ms" << endl;
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement