Advertisement
istinishat

implicit segment tree

May 14th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. problem : http://codeforces.com/problemset/problem/555/C
  2. code from : http://codeforces.com/problemset/submission/555/38210662
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. #define si(n) scanf("%d",&n)
  7.  
  8. struct Node{
  9.     int m;
  10.     Node *l,*r;
  11.     Node(){m=0;l=NULL;r=NULL;}
  12. };
  13.  
  14. void update(Node *root,int tl,int tr,int l,int r,int val)
  15. {
  16.     if(l>r || r<tl || l>tr)return;
  17.  
  18.     if(tl>=l && tr<=r){
  19.         root->m=max(root->m,val);
  20.         return ;
  21.     }
  22.     int mid=(tl+tr)/2;
  23.     if(root->l==NULL)root->l=new Node();
  24.     if(root->r==NULL)root->r=new Node();
  25.     update(root->l,tl,mid,l,r,val);
  26.     update(root->r,mid+1,tr,l,r,val);
  27. }
  28.  
  29. int get(Node *root,int tl,int tr,int pos)
  30. {
  31.     if(root==NULL || tl>tr || pos<tl || pos>tr)
  32.         return 0;
  33.     if(tl==tr)return root->m;
  34.     int mid=(tl+tr)/2;
  35.     return max(root->m,max(get(root->l,tl,mid,pos),
  36.                            get(root->r,mid+1,tr,pos)));
  37. }
  38.  
  39. Node *tv,*th;
  40.  
  41.  
  42. int main()
  43. {
  44.     //freopen("input.txt","r",stdin);
  45.     int i,j,n,q;
  46.     si(n);si(q);
  47.     tv=new Node();
  48.     th=new Node();
  49.     while(q--){
  50.         int x,y,z;
  51.         char ch;
  52.         si(y);si(x);scanf(" %c",&ch);
  53.  
  54.         if(ch=='U'){
  55.             z=get(tv,1,n,y);
  56.             //cout<<z<<endl;
  57.             printf("%d\n",x-z);
  58.             update(tv,1,n,y,y,x);
  59.             update(th,1,n,z,x,y);
  60.         }
  61.         else{
  62.             z=get(th,1,n,x);
  63.             printf("%d\n",y-z);
  64.             update(th,1,n,x,x,y);
  65.             update(tv,1,n,z,y,x);
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement