Advertisement
Guest User

Untitled

a guest
Jan 18th, 2015
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. //Code made by Bayemirov Beket
  2.  
  3. #include <algorithm>
  4. //iostream is so mainstream
  5. #include <iostream>
  6. #include <assert.h>
  7. #include <stdlib.h>
  8. #include <cstring>
  9. #include <cstdlib>
  10. #include <stdio.h>
  11. #include <cstdio>
  12. #include <string>
  13. #include <vector>
  14. #include <cmath>
  15. #include <map>
  16. inline int Rand() { return (rand() << 16) | rand(); }
  17. #define y1 google
  18. #define ms(a,x) memset(a,x,sizeof a)
  19. #define sqr(x) x * x
  20. #define sz(x) (int)(x.size());
  21. #define INF 2147483647
  22. #define MOD 1000000007
  23. #define N 1005
  24. #define pf push_front
  25. #define pb push_back
  26. #define mp make_pair
  27. #define INF 2147483647
  28. #define F first
  29. #define S second
  30. #define fname ""
  31.  
  32. using namespace std;
  33.  
  34. typedef long long ll;
  35.  
  36. struct node {
  37.     ll l, r, key, y, cnt;
  38.     bool rev;
  39.     ll sum;
  40. };
  41.  
  42. ll root, A, B, len = 0, M, l, r, p, new_val;
  43. node t[1000005];
  44.  
  45. void push (ll v) {
  46.     if (v && t[v].rev) {
  47.         t[v].rev = false;
  48.         swap (t[v].l, t[v].r);
  49.         if (t[v].l)
  50.             t[t[v].l].rev ^= true;
  51.         if (t[v].r)
  52.             t[t[v].r].rev ^= true;
  53.     }
  54. }
  55.  
  56. void update(ll v) {
  57.     t[v].cnt = t[t[v].l].cnt + t[t[v].r].cnt + 1;
  58.     t[v].sum = t[t[v].l].sum + t[t[v].r].sum + t[v].key;
  59. }
  60.  
  61. void split (ll v, ll &A, ll &B, ll x, ll now = 0) {
  62.     if (!v) {
  63.         A = B = 0;
  64.         return;
  65.     }
  66.     push(v);
  67.     ll cur = now + t[t[v].l].cnt + 1;
  68.     if (x < cur) {
  69.         split (t[v].l, A, t[v].l, x, now);
  70.         B = v;
  71.     }
  72.     else {
  73.         split (t[v].r, t[v].r, B, x, now + t[t[v].l].cnt + 1);
  74.         A = v;
  75.     }
  76.     update(v);
  77. }
  78.  
  79. ll _merge (ll A, ll B) {
  80.     if (!A || !B)
  81.         return A + B;
  82.     push(A);
  83.     push(B);
  84.     if (t[A].y > t[B].y) {
  85.         t[A].r = _merge(t[A].r, B);
  86.         update(A);
  87.         return A;
  88.     }
  89.     else {
  90.         t[B].l = _merge(A, t[B].l);
  91.         update(B);
  92.         return B;
  93.     }
  94. }
  95.  
  96. void add (ll v) {
  97.     t[++len].key = v;
  98.     t[len].y = Rand();
  99.     t[len].cnt = 1;
  100.     t[len].l = t[len].r = 0;
  101.     t[len].sum = v;
  102.     t[len].rev = false;
  103.     root = _merge (root, len);
  104. }
  105.  
  106. int main () {
  107.  
  108.     #ifndef ONLINE_JUDGE
  109.     freopen(fname".in", "r", stdin);
  110.     freopen(fname".out", "w", stdout);
  111.     #endif
  112.  
  113.     ll n;
  114.     scanf ("%I64d", &n);
  115.  
  116.     for (ll i = 1; i <= n; i++) {
  117.         ll x;
  118.         scanf ("%I64d", &x);
  119.         add (x);
  120.     }
  121.  
  122.     ll m;
  123.     scanf ("%I64d", &m);
  124.  
  125.         char ch;
  126.  
  127.     while (m--) {
  128.         scanf ("\n%c", &ch);
  129.         if (ch == 'R') {
  130.             M = 0;
  131.             scanf ("%I64d%I64d", &l, &r);
  132.             split (root, A, B, r);
  133.             split (A, A, M, l - 1);
  134.             t[M].rev ^= true;
  135.             A = _merge (A, M);
  136.             root = _merge (A, B);
  137.         }
  138.         else if (ch == 'Q') {
  139.             M = 0;
  140.             scanf ("%I64d%I64d", &l, &r);
  141.             A = B = 0;
  142.             split (root, A, B, r);
  143.             split (A, A, M, l - 1);
  144.             printf ("%I64d\n", t[M].sum);
  145.             A = _merge (A, M);
  146.             root = _merge (A, B);
  147.         }
  148.         else {
  149.             A = B = M = 0;
  150.             scanf ("%I64d%I64d", &p, &new_val);
  151.             split (root, A, B, p);
  152.             split (A, A, M, p - 1);
  153.             t[M].key = new_val;
  154.             t[M].sum = new_val;
  155.             A = _merge(A, M);
  156.             root = _merge(A, B);
  157.         }
  158.     }
  159.  
  160.     return 0;
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement