Guest User

Untitled

a guest
Oct 28th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node
  4. {
  5.     long long v, p, s;
  6.     node *l, *r;
  7.     node()
  8.     {
  9.         l = r = NULL;
  10.         v = p = 0;
  11.         s = 0;
  12.     }
  13. };
  14. typedef long long int ll;
  15. typedef node* pnode;
  16. ll n, q;
  17. char c;
  18. ll sz(pnode t)
  19. {
  20.     if (t)return t->s;
  21.     return 0;
  22. }
  23. void upsz(pnode &t)
  24. {
  25.     if (!t)return;
  26.     t->s = sz(t->r) + sz(t->l) + 1;
  27. }
  28. void split(pnode t, pnode &l, pnode &r,ll k)
  29. {
  30.     if (!t)l = r = NULL;
  31.     else if (t->v <= k)split(t->r,t->r,r,k),l = t;
  32.     else split(t->l, l, t->l, k), r = t;
  33.     upsz(t);
  34. }
  35. void merge(pnode &t, pnode l, pnode r)
  36. {
  37.     if (!l)t = r;
  38.     else if (!r)t = l;
  39.     else if (l->p > r->p)merge(l->r,l->r, r), t = l;
  40.     else merge(r->l, l, r->l), t = r;
  41.     upsz(t);
  42. }
  43. void ins(pnode nt, pnode &t)
  44. {
  45.     if (!t || !(t->s))t = nt;
  46.     else if (t->p < nt->p)split(t, nt->l, nt->r, nt->v), t = nt;
  47.     else if (nt->v <= t->v)ins(nt, t->l);
  48.     else ins(nt, t->r);
  49.     upsz(t);
  50. }
  51. void era(pnode &t, ll k)
  52. {
  53.     if (!t)return;
  54.     else if (t->v == k){ pnode te = t; merge(t, t->l, t->r); te = NULL; }
  55.     else if (t->v > k)era(t->l, k);
  56.     else era(t->r, k);
  57.     upsz(t);
  58. }
  59. bool find(pnode t,ll va)
  60. {
  61.     if (!t || t->s==0)return false;
  62.     if (t->v == va)return 1;
  63.     if (t->v > va)return find(t->l,va);
  64.     else return find(t->r, va);
  65. }
  66. pnode ne(ll va)
  67. {
  68.     pnode r=new node();
  69.     r->l = r->r = NULL;
  70.     r->s = 1;
  71.     r->v = va;
  72.     r->p = rand();
  73.     return r;
  74. }
  75. ll kth(pnode &t, ll k,ll l)
  76. {
  77.     if (l + sz(t->l)+1== k)
  78.         return t->v;
  79.     else if (k > l + sz(t->l))
  80.         return kth(t->r, k, l + sz(t->l)+1);
  81.     else
  82.         return kth(t->l,k,l);
  83. }
  84. ll co(pnode &t,ll x)
  85. {
  86.     if (!t)return 0;
  87.     if (t->v == x)return sz(t->l);
  88.     if (t->v < x)return sz(t->l) + 1 + co(t->r, x);
  89.     return co(t->l, x);
  90. }
  91. int main()
  92. {
  93.     ios::sync_with_stdio(false);
  94.     cin.tie(0);
  95.     cout.tie(0);
  96.     pnode t = new node();
  97.     cin >> q;
  98.     while (q--)
  99.     {
  100.         cin >> c >> n;
  101.         if (c == 'I')
  102.         {
  103.             if (find(t, n))
  104.                 continue;
  105.             ins(ne(n), t);
  106.         }
  107.         else if (c == 'D')
  108.             era(t, n);
  109.         else if (c == 'K')
  110.         {
  111.             if (t->s < n)cout << "invalid";
  112.             else cout << kth(t, n,0);
  113.             cout << "\n";
  114.         }
  115.         else
  116.             cout << co(t, n) << "\n";
  117.     }
  118.     return 0;
  119. }
Add Comment
Please, Sign In to add comment