Advertisement
Guest User

Treap

a guest
Feb 7th, 2012
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <ctime>
  6. using namespace std;
  7. typedef long long ll;
  8. struct treap
  9. {
  10.     int x,y,cnt,sum,add;
  11.     treap *l,*r;
  12.     treap() {}
  13.     treap(int x): x(x),y(rand()^(rand()<<15)),cnt(1),sum(x),add(0),l(0),r(0) {}
  14. };
  15. typedef treap* ptreap;
  16. int psize(ptreap t)
  17. {
  18.     return !t?0:t->cnt;
  19. }
  20. int cost(ptreap t)
  21. {
  22.     return !t?0:t->sum+t->add*psize(t);
  23. }
  24. void recalc(ptreap &t)
  25. {
  26.     if (t)
  27.     {
  28.         t->cnt=psize(t->l)+psize(t->r)+1;
  29.         t->sum=cost(t->l)+cost(t->r)+t->x;
  30.     }
  31. }
  32. void push(ptreap t)
  33. {
  34.     if (t)
  35.     {
  36.         if (t->l)
  37.             t->l->add+=t->add;
  38.         if (t->r)
  39.             t->r->add+=t->add;
  40.         t->add=0;
  41.     }
  42. }
  43. void merge(ptreap &t,ptreap l,ptreap r)
  44. {
  45.     if (!l)
  46.     {
  47.         push(r);
  48.         t=r;
  49.     } else if (!r)
  50.     {
  51.         push(l);
  52.         t=l;
  53.     }
  54.     else if (l->y>r->y)
  55.     {
  56.         push(l);
  57.         merge(l->r,l->r,r);
  58.         t=l;
  59.     } else
  60.     {
  61.         push(r);
  62.         merge(r->l,l,r->l);
  63.         t=r;
  64.     }
  65.     recalc(t);
  66. }
  67. void split(ptreap t,ptreap &l,ptreap &r,int x)
  68. {
  69.     if (!t)
  70.     {
  71.         l=r=0;
  72.         return;
  73.     }
  74.     push(t);
  75.     if (t->x<=x)
  76.     {
  77.         split(t->r,t->r,r,x);
  78.         l=t;
  79.         recalc(l);
  80.     } else
  81.     {
  82.         split(t->l,l,t->l,x);
  83.         r=t;
  84.         recalc(r);
  85.     }
  86. }
  87. void insert(ptreap &t,int x)
  88. {
  89.     ptreap l,r;
  90.     split(t,l,r,x);
  91.     merge(l,l,new treap(x));
  92.     merge(t,l,r);
  93. }
  94. void erase(ptreap &t,int x)
  95. {
  96.     ptreap l,r;
  97.     split(t,l,r,x-1);
  98.     split(r,t,r,x);
  99.     merge(t,l,r);
  100. }
  101. int sum(ptreap t,int a,int b)
  102. {
  103.     ptreap l,r;
  104.     split(t,l,r,a-1);
  105.     split(r,l,r,++b);
  106.     return cost(l);
  107. }
  108. void upd(ptreap &t,int a,int b,int d)
  109. {
  110.     ptreap l,r;
  111.     split(t,l,r,a-1);
  112.     split(r,t,r,++b);
  113.     if (t)
  114.         t->add+=d;
  115.     merge(t,l,t);
  116.     merge(t,t,r);
  117. }
  118. ptreap t=0;
  119. int main()
  120. {
  121.     srand(time(NULL));
  122.     int n=10;
  123.     for(int i=0;i<n;i++)
  124.     {
  125.         int x=rand()%200;
  126.         cout<<x<<" ";
  127.         insert(t,x);
  128.     }
  129.     cout<<endl;
  130.     int cod,l,r,d;
  131.     for(;;)
  132.     {
  133.         cin>>cod>>l;
  134.         if (cod>=3)
  135.             cin>>r;
  136.         if (cod==4)
  137.             cin>>d;
  138.         if (cod==1)
  139.             insert(t,l);
  140.         else if (cod==2)
  141.             erase(t,l);
  142.         else if (cod==3)
  143.             cout<<sum(t,l,r)<<endl;
  144.         else
  145.             upd(t,l,r,d);
  146.     }
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement