[2017.11.29]BZOJ4825[Hnoi2017]单旋

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[2017.11.29]BZOJ4825[Hnoi2017]单旋相关的知识,希望对你有一定的参考价值。

  1 #include<bits/stdc++.h>
  2 #define M 100010
  3 #define RG register
  4 #define inf 0x3f3f3f3f
  5 using namespace std;
  6 bool rev[M];
  7 set<int> tr;
  8 set<int>::iterator it;
  9 int m,rt,tp,big,cnt,cur,dau,dep,loc,sml,sum,tmp,c[M],fa[M],sz[M],dad[M],key[M],srt[M],sta[M],ch[2][M],son[2][M];
 10 inline int gi(){
 11     RG int x=0;RG char y=getchar();
 12     while(y<0||y>9) y=getchar();
 13     while(y>=0&&y<=9) x=x*10+y-0,y=getchar();
 14     return x;
 15 }
 16 inline bool cmp(RG const int &x,RG const int &y){
 17     return key[x]<key[y];
 18 }
 19 inline bool isroot(RG int x){
 20     return son[0][fa[x]]!=x&&son[1][fa[x]]!=x;
 21 }
 22 inline void pushdown(RG int x){
 23     rev[x]=0;
 24     rev[son[0][x]]^=1;
 25     rev[son[1][x]]^=1;
 26     swap(son[0][x],son[1][x]);
 27 }
 28 inline void pushup(RG int x){
 29     sz[x]=sz[son[0][x]]+sz[son[1][x]]+1;
 30 }
 31 inline void rot(RG int x){
 32     RG int f=fa[x],ff=fa[f];
 33     RG bool dir=son[1][f]==x;
 34     if(!isroot(f)) son[son[1][ff]==f][ff]=x;
 35     fa[x]=ff;
 36     son[dir][f]=son[dir^1][x];
 37     fa[son[dir^1][x]]=f;son[dir^1][x]=f;fa[f]=x;
 38     pushup(f);pushup(x);
 39 }
 40 inline void spl(RG int x){
 41     tp=0;sta[++tp]=x;
 42     for (RG int i=x;!isroot(i);i=fa[i])
 43     sta[++tp]=fa[i];
 44     for (RG int i=tp;i;--i)
 45     if(rev[sta[i]])
 46         pushdown(sta[i]);
 47     while(!isroot(x)){
 48     RG int f=fa[x],ff=fa[f];
 49     if(!isroot(f)){
 50         if((son[0][ff]==f)^(son[1][f]==x)) rot(x);
 51         else                               rot(f);
 52     }
 53     rot(x);
 54     }
 55 }
 56 inline void acs(RG int x){
 57     RG int y=0;
 58     while(x){
 59     spl(x);
 60     son[1][x]=y;
 61     pushup(x);
 62     y=x;x=fa[x];
 63     }
 64 }
 65 inline void mkrt(RG int x){
 66     acs(x);spl(x);
 67     rev[x]^=1;
 68 }
 69 inline int query(RG int x){
 70     mkrt(rt);
 71     acs(x);spl(x);
 72     return sz[x];
 73 }
 74 inline void link(RG int x,RG int y){
 75     if(!x||!y||x==y) return;
 76     mkrt(x);fa[x]=y;
 77 }
 78 inline void cut(RG int x,RG int y){    
 79     if(!x||!y||x==y) return;
 80     mkrt(x);acs(y);spl(y);
 81     fa[x]=son[0][y]=0;pushup(y);
 82 }
 83 inline void update(RG int x,RG bool op1,RG bool op2){
 84     RG int f=dad[x];dau=ch[op1][x];
 85     if(op2&&x==rt) rt=dau;
 86     if(!op2) ch[op1][x]=rt,dad[rt]=x,dad[x]=0;
 87     ch[op1^1][f]=dau;dad[dau]=f;
 88     if(op2) ch[op1][x]=dad[x]=0;
 89 }
 90 int main(){
 91     freopen("splay.in","r",stdin);
 92     freopen("splay.out","w",stdout);
 93     m=gi();
 94     for (RG int i=1;i<=m;++i){
 95     c[i]=gi();srt[i]=i;
 96     key[i]=c[i]<2?gi():inf;
 97     }
 98     sort(srt+1,srt+m+1,cmp);
 99     for (RG int i=1;i<=m&&c[srt[i]]<2;++i)
100     key[srt[i]]=++sum;
101     tr.insert(-inf);tr.insert(inf);
102     for (RG int i=1;i<=m;++i)
103     if(c[i]==1){
104         if(!cnt){
105         rt=key[i];
106         puts("1");
107         }
108         else{
109         it=tr.upper_bound(key[i]);
110         big=*it;--it;sml=*it;dep=0;
111         if(sml>-inf){tmp=query(sml);if(dep<tmp) loc=sml,dep=tmp;} 
112         if(big< inf){tmp=query(big);if(dep<tmp) loc=big,dep=tmp;} 
113         dad[key[i]]=loc;ch[key[i]>loc][loc]=key[i];link(loc,key[i]);
114         printf("%d\n",dep+1);
115         }
116         tr.insert(key[i]);++cnt;
117     }
118     else if(c[i]==2)
119         if(cnt==1) puts("1");
120         else{
121         it=tr.begin();++it;
122         cur=*it;RG int f=dad[cur];dau=ch[1][cur];printf("%d\n",query(cur));
123         if(cur!=rt) cut(cur,f),cut(cur,dau),link(cur,rt),link(f,dau),update(cur,1,0),rt=cur;
124         }
125     else if(c[i]==3)
126         if(cnt==1) puts("1");
127         else{
128         it=tr.end();--it;--it;
129         cur=*it;RG int f=dad[cur];dau=ch[0][cur];printf("%d\n",query(cur));
130         if(cur!=rt) cut(cur,f),cut(cur,dau),link(cur,rt),link(f,dau),update(cur,0,0),rt=cur;
131         }
132     else if(c[i]==4){
133         --cnt;
134         if(!cnt){
135         puts("1");
136         tr.erase(tr.find(rt));
137         rt=0;
138         }
139         else{
140         it=tr.begin();++it;
141         cur=*it;RG int f=dad[cur];dau=ch[1][cur];
142         printf("%d\n",query(cur));
143         cut(cur,f),cut(cur,dau),link(f,dau),tr.erase(cur);update(cur,1,1);
144         }      
145     }  
146     else{
147         --cnt;
148         if(!cnt){
149         puts("1");
150         tr.erase(tr.find(rt));
151         rt=0;
152         }
153         else{
154         it=tr.end();--it;--it;
155         cur=*it;RG int f=dad[cur];dau=ch[0][cur];printf("%d\n",query(cur));
156         cut(cur,f),cut(cur,dau),link(f,dau),tr.erase(cur);update(cur,0,1);
157         }      
158     }  
159     return 0;
160 }

 

以上是关于[2017.11.29]BZOJ4825[Hnoi2017]单旋的主要内容,如果未能解决你的问题,请参考以下文章

bzoj4825 [Hnoi2017]单旋

BZOJ4825[Hnoi2017]单旋 线段树+set

BZOJ 4825 [Hnoi2017]单旋

bzoj4825 [Hnoi2017]单旋

BZOJ4825HNOI2017单旋(Link-Cut Tree)

BZOJ4825 单旋