CF19D Points 平衡树

没调完呢~ 

code:

#include <set>
#include <map>  
#include <cstdio> 
#include <algorithm>   
#define N 200005    
#define inf 1200000005   
#define lson p[x].ch[0] 
#define rson p[x].ch[1]   
#define setIO(s) freopen(s".in","r",stdin)      
using namespace std;                 
int tot,cnt,pp,root;                       
set<int>S[N];  
map<int,int>idx,sp;          
set<int>::iterator it;   
struct data
{   
    int ch[2],f,maxx,val,id,size;         
}p[N];   
int newnode() { return ++tot; }   
int get(int x) { return p[p[x].f].ch[1]==x; }  
void pushup(int x) 
{  
    p[x].maxx=p[x].val; 
    p[x].maxx=max(p[x].val,max(p[lson].maxx,p[rson].maxx));     
    p[x].size=p[lson].size+p[rson].size+1;   
}
void rotate(int x) 
{  
    int old=p[x].f,fold=p[old].f,which=get(x);    
    p[old].ch[which]=p[x].ch[which^1],p[p[old].ch[which]].f=old;   
    p[x].ch[which^1]=old,p[old].f=x,p[x].f=fold;   
    if(fold)   p[fold].ch[p[fold].ch[1]==old]=x;         
    pushup(old),pushup(x); 
}                           
void splay(int x,int &tar) 
{
    int fa,u=p[tar].f;  
    for(;(fa=p[x].f)!=u;rotate(x)) if(p[fa].f!=u)    rotate(get(fa)==get(x)?fa:x);     
    tar=x;     
}                        
void insert(int &x,int id,int v) 
{
    if(!x) 
    {
        x=newnode();                    
        p[x].id=id,p[x].val=v;       
        if(!sp[id])  sp[id]=++pp;    
        pushup(x);   
    } 
    else 
    {
        insert(p[x].ch[id>p[x].id],id,v);      
        pushup(x);     
    }
}   
int getr(int x) 
{
    while(rson)  x=rson;  
    return x;   
}       
int getpre(int v) 
{     
    int x=root,pre=root;       
    while(x) 
    {      
        if(p[x].id<=v)  pre=x,  x=rson;               
        else x=lson;       
    }  
    return pre;   
}
int find(int x,int d) 
{     
    if(p[lson].maxx>d)   return find(lson,d);   
    else if(p[x].val>d)  return p[x].id;    
    else return find(rson,d);     
}
int main() 
{ 
    // setIO("input");      
    p[0].maxx=-inf;                
    int i,j,n;   
    insert(root,-inf,-inf),insert(root,inf,-inf);                 
    scanf("%d",&n);      
    for(i=1;i<=n;++i) 
    { 
        char str[10]; 
        scanf("%s",str);   
        if(str[0]=='a') 
        {      
            int x,y; 
            scanf("%d%d",&x,&y);       
            if(!idx[x])  idx[x]=++cnt;                                   
            S[idx[x]].insert(-y);               
            int xx=-(*S[idx[x]].begin());          
            if(sp[x])                   
            {   
                splay(sp[x], root);   
                p[root].val=xx;  
                pushup(root);    
            }    
            else 
            {              
                insert(root,x,y);      
                splay(tot,root);       
            }   
        } 
        if(str[0]=='r') 
        {    
            int x,y; 
            scanf("%d%d",&x,&y);       
            S[idx[x]].erase(y);        
            if(S[idx[x]].empty())
            {   
                splay(sp[x],root);      
                int L=getr(p[root].ch[0]);     
                int R=p[root].ch[1];   
                splay(L,p[root].ch[0]);   
                p[L].f=0, p[L].ch[1]=R, p[R].f=root=L;  
                pushup(L);                   
                idx[x]=sp[x]=0;     
            } 
            else 
            {
                splay(sp[x],root);   
                p[root].val=-(*S[idx[x]].begin());     
                pushup(root);       
            }
        } 
        if(str[0]=='f') 
        {   
            int x,y; 
            scanf("%d%d",&x,&y);         
            int L=getpre(x);          
            splay(x,root);                  
            if(p[p[root].ch[1]].maxx<=y)   printf("-1\n");  
            else 
            {
                int R=p[root].ch[1];    
                int xx=find(R,y); 
                int yy=idx[xx];          
                it=S[yy].lower_bound(y);        
                if(it==S[yy].end())  it--;  
                while(*it>=y)  it--;   
                printf("%d %d\n",xx,*it);   
            }
        }
    }
    return 0;   
}

  

猜你喜欢

转载自www.cnblogs.com/guangheli/p/11779555.html