Tranvick

Untitled

Feb 22nd, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #define N 111111
  4.  
  5. using namespace std;
  6.  
  7. struct tree{
  8.     int x,y,ls,rs;
  9. } t[N];
  10. int n,c,randarr[N],root;
  11.  
  12. void merge(int &x,int left,int right){
  13.     if (!left || !right){
  14.         x=left?left:right;
  15.         return;
  16.     }
  17.     if (t[left].y>t[right].y){
  18.         x=left;
  19.         merge(t[left].rs,t[left].rs,right);
  20.     }else{
  21.         x=right;
  22.         merge(t[right].ls,left,t[right].ls);
  23.     }
  24. }
  25.  
  26. void split(int x,int key,int &left,int &right){
  27.     if (!x){
  28.         left=right=0;
  29.         return;
  30.     }
  31.     if (t[x].x<=key){
  32.         left=x;
  33.         split(t[x].rs,key,t[x].rs,right);
  34.     }else{
  35.         right=x;
  36.         split(t[x].ls,key,left,t[x].ls);
  37.     }
  38. }
  39.  
  40. void insert(int x){
  41.     t[++c]=(tree){x,randarr[c],0,0};
  42.     int l,r,m;
  43.     split(root,x,l,r);
  44.     merge(m,l,c);
  45.     merge(root,m,r);
  46. }
  47.  
  48. void erase(int x){
  49.     int l,r,m,l1;
  50.     split(root,x,l,r);
  51.     split(l,x-1,l1,m);
  52.     merge(root,l1,r);
  53. }
  54.  
  55. void print(int x=root){
  56.     if (!x) return;
  57.     print(t[x].ls);
  58.     cout<<t[x].x<<" ";
  59.     print(t[x].rs);
  60. }
  61.  
  62. void init(){
  63.     cin>>n;
  64.     for (int i=1;i<=n;i++) randarr[i]=i;
  65.     random_shuffle(randarr+1,randarr+n+1);
  66.     for (int i=1;i<=n;i++){
  67.         int x;
  68.         cin>>x;
  69.         insert(x);
  70.     }
  71.     print();
  72.     cin>>n;
  73.     for (int i=1;i<=n;i++){
  74.         int x;
  75.         cin>>x;
  76.         erase(x);
  77.         print();
  78.     }
  79. }
  80.  
  81. int main(){
  82.     init();
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment