Advertisement
K_Y_M_bl_C

Untitled

Jan 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. mt19937 mrand(306);
  2.  
  3. struct Node
  4. {
  5.     int x, val, y, l, r, sz;
  6.     Node() : x(0), val(0), y(mrand()), l(0), r(0), sz(0) {};
  7.     Node(int x) : x(x), val(x), y(mrand()), l(0), r(0), sz(1) {};
  8. };
  9.  
  10. const int SZ = 10e6;
  11.  
  12. Node t[SZ];
  13.  
  14. void recalc(int v)
  15. {
  16.     if (!v)
  17.         return;
  18.     t[v].sz = t[t[v].l].sz + t[t[v].r].sz + 1;
  19.     t[v].val = (t[v].x | t[t[v].l].val | t[t[v].r].val);
  20. }
  21.  
  22. pii split(int v, int x)
  23. {
  24.     if (!v)
  25.         return mk(0, 0);
  26.     if (t[t[v].l].sz < x)
  27.     {
  28.         pii cur = split(t[v].r, x - t[t[v].l].sz - 1);
  29.         t[v].r = cur.X;
  30.         recalc(v);
  31.         return mk(v, cur.Y);
  32.     }
  33.     else
  34.     {
  35.         pii cur = split(t[v].l, x);
  36.         t[v].l = cur.Y;
  37.         recalc(v);
  38.         return mk(cur.X, v);
  39.     }
  40. }
  41.  
  42. int merge(int l, int r)
  43. {
  44.     if (l * r == 0)
  45.     {
  46.         if (l)
  47.             return l;
  48.         return r;
  49.     }
  50.     if (t[l].y > t[r].y)
  51.     {
  52.         t[l].r = merge(t[l].r, r);
  53.         recalc(l);
  54.         return l;
  55.     }
  56.     else
  57.     {
  58.         t[r].l = merge(l, t[r].l);
  59.         recalc(r);
  60.         return r;
  61.     }
  62. }
  63.  
  64. void print(int v)
  65. {
  66.     if (!v)
  67.         return;
  68.     print(t[v].l);
  69.     printf("%d ", t[v].x);
  70.     print(t[v].r);
  71. }
  72.  
  73. int solve()
  74. {
  75.     int n;
  76.     scanf("%d", &n);
  77.     int fr = 1;
  78.     int root = 1;
  79.     while (n--)
  80.     {
  81.         char ct;
  82.         scanf(" %c", &ct);
  83.         if (ct == '+')
  84.         {
  85.             int pos, cnt;
  86.             char c;
  87.             scanf("%d %d %c", &pos, &cnt, &c);
  88.             --pos;
  89.             int x = (1 << (c - 'a'));
  90.             int croot = fr;
  91.             t[croot] = Node(x);
  92.             ++fr;
  93.             for (int i = 0; i < cnt - 1; ++i)
  94.             {
  95.                 t[fr] = Node(x);
  96.                 croot = merge(croot, fr);
  97.                 ++fr;
  98.             }
  99.             if (croot == root)
  100.             {
  101.                 root = croot;
  102.             }
  103.             else
  104.             {
  105.                 pii cur = split(root, pos);
  106.                 //print(cur.X), puts("");
  107.                 //print(cur.Y), puts("");
  108.                 root = merge(cur.X, croot);
  109.                 root = merge(root, cur.Y);
  110.                 //print(root), puts("");
  111.             }
  112.             //puts("-----------------------");
  113.         }
  114.         if (ct == '-')
  115.         {
  116.             int pos, cnt;
  117.             scanf("%d %d", &pos, &cnt);
  118.             --pos;
  119.             pii cur = split(root, pos);
  120.             //print(cur.X), puts("");
  121.             //print(cur.Y), puts("");
  122.             pii ccur = split(cur.Y, cnt);
  123.             root = merge(cur.X, ccur.Y);
  124.             //print(root), puts("");
  125.             //puts("errrrrrrrrrrrrrrrrrrrrrrase");
  126.         }
  127.         if (ct == '?')
  128.         {
  129.             int l, r;
  130.             scanf("%d %d", &l, &r);
  131.             --l;
  132.             pii cur = split(root, l);
  133.             pii ccur = split(cur.Y, r - l);
  134.             recalc(ccur.X);
  135.             printf("%d\n", __builtin_popcount(t[ccur.X].val));
  136.             root = merge(cur.X, ccur.X);
  137.             root = merge(root, ccur.Y);
  138.         }
  139.     }
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement