HDU - 2475:Box(splay維護森林)
阿新 • • 發佈:2018-12-13
There are N boxes on the ground, which are labeled by numbers from 1 to N. The boxes are magical, the size of each one can be enlarged or reduced arbitrarily.
Jack can perform the “MOVE x y” operation to the boxes: take out box x; if y = 0, put it on the ground; Otherwise, put it inside box y. All the boxes inside box x remain the same. It is possible that an operation is illegal, that is, if box y is contained (directly or indirectly) by box x, or if y is equal to x.
In the following picture, box 2 and 4 are directly inside box 6, box 3 is directly inside box 4, box 5 is directly inside box 1, box 1 and 6 are on the ground.
The picture below shows the state after Jack performs “MOVE 4 1”:
Then he performs “MOVE 3 0”, the state becomes:
During a sequence of MOVE operations, Jack wants to know the root box of a specified box. The root box of box x is defined as the most outside box which contains box x. In the last picture, the root box of box 5 is box 1, and box 3’s root box is itself.
InputInput contains several test cases.
For each test case, the first line has an integer N (1 <= N <= 50000), representing the number of boxes.
Next line has N integers: a1, a2, a3, ... , aN (0 <= ai <= N), describing the initial state of the boxes. If ai is 0, box i is on the ground, it is not contained by any box; Otherwise, box i is directly inside box ai. It is guaranteed that the input state is always correct (No loop exists).
Next line has an integer M (1 <= M <= 100000), representing the number of MOVE operations and queries.
On the next M lines, each line contains a MOVE operation or a query:
1. MOVE x y, 1 <= x <= N, 0 <= y <= N, which is described above. If an operation is illegal, just ignore it.
2. QUERY x, 1 <= x <= N, output the root box of box x.
OutputFor each query, output the result on a single line. Use a blank line to separate each test case.Sample Input Sample Output
Jack can perform the “MOVE x y” operation to the boxes: take out box x; if y = 0, put it on the ground; Otherwise, put it inside box y. All the boxes inside box x remain the same. It is possible that an operation is illegal, that is, if box y is contained (directly or indirectly) by box x, or if y is equal to x.
In the following picture, box 2 and 4 are directly inside box 6, box 3 is directly inside box 4, box 5 is directly inside box 1, box 1 and 6 are on the ground.
The picture below shows the state after Jack performs “MOVE 4 1”:
Then he performs “MOVE 3 0”, the state becomes:
During a sequence of MOVE operations, Jack wants to know the root box of a specified box. The root box of box x is defined as the most outside box which contains box x. In the last picture, the root box of box 5 is box 1, and box 3’s root box is itself.
InputInput contains several test cases.
For each test case, the first line has an integer N (1 <= N <= 50000), representing the number of boxes.
Next line has N integers: a1, a2, a3, ... , aN (0 <= ai <= N), describing the initial state of the boxes. If ai is 0, box i is on the ground, it is not contained by any box; Otherwise, box i is directly inside box ai. It is guaranteed that the input state is always correct (No loop exists).
Next line has an integer M (1 <= M <= 100000), representing the number of MOVE operations and queries.
On the next M lines, each line contains a MOVE operation or a query:
1. MOVE x y, 1 <= x <= N, 0 <= y <= N, which is described above. If an operation is illegal, just ignore it.
2. QUERY x, 1 <= x <= N, output the root box of box x.
OutputFor each query, output the result on a single line. Use a blank line to separate each test case.Sample Input
2 0 1 5 QUERY 1 QUERY 2 MOVE 2 0 MOVE 1 2 QUERY 1 6 0 6 4 6 1 0 4 MOVE 4 1 QUERY 3 MOVE 1 4 QUERY 1
1 1 2 1 1
題意:給定一些盒子,以及盒子的巢狀關係,現在有一些操作,可以把盒子移到另外的盒子裡。一些詢問,問包含x盒子的最外面的盒子是哪個。
思路:題意轉化一下,就是森林,Cut(x,y)操作是可以把x為根的子樹砍下來,接在y節點下面。 詢問Query(x)就是查詢x的根。
(模型很裸,還是寫一下裝進模板裡。
像這樣維護森林,求根,並查集肯定不夠。我們用括號序列來做。
- 那麼點x的子樹就的區間就是[x,x+N];
- 點y在x的子樹裡,當且僅當pos[x]<=pos[y]<=pos[y+N]<=pos[x+N];(pos是點在splay裡面的排名。)
- 把x加到y的子樹裡,即[y,y+N]裡面加序列[x,x+N];可以把後者加到y的後面。
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; const int maxn=100010; int ch[maxn][2],fa[maxn],Laxt[maxn],Next[maxn],To[maxn],cnt,dd,N; int get(int x){ return ch[fa[x]][1]==x; } void rotate(int x) { int old=fa[x],fold=fa[old],opt=get(x); fa[x]=fold; fa[old]=x; fa[ch[x][opt^1]]=old; ch[old][opt]=ch[x][opt^1];ch[x][opt^1]=old; if(fold) ch[fold][ch[fold][1]==old]=x; } void splay(int x,int y) { for(int f;(f=fa[x])!=y;rotate(x)){ if(fa[f]!=y) rotate(get(x)==get(f)?f:x); } } void add(int u,int v) { Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; } void dfs(int u) //按照括號序建spaly,起初全是單鏈。 { fa[u]=dd; ch[dd][1]=u; dd=u; for(int i=Laxt[u];i;i=Next[i]) dfs(To[i]); fa[u+N]=dd; ch[dd][1]=u+N; dd=u+N; } void build() { for(int i=Laxt[0];i;i=Next[i]){ dd=0; dfs(To[i]); } } int query(int x) { splay(x,0); int now=x; while(ch[now][0]) now=ch[now][0]; return now; } void move(int a,int b) { if(a==b) return ; //不合法1 splay(a,0); splay(a+N,a); for(int t=b;t;t=fa[t]) if(ch[a+N][0]==t) return ; //不合法2 int x=ch[a][0],y=ch[a+N][1]; fa[x]=fa[y]=ch[a][0]=ch[a+N][1]=0; int t=y; while(ch[t][0]) t=ch[t][0]; splay(t,0); fa[x]=t; ch[t][0]=x;//[a,a+N]區間分離出來,合併剩餘部分 //右邊的最小值放根,再合併,保證平衡。 if(b==0) return ; splay(b,0); t=ch[b][1]; while(ch[t][0]) t=ch[t][0]; splay(t,b);// t此時沒有左兒子,把[a,a+N]擠進去。 fa[a]=t; ch[t][0]=a; } int main() { int M,x,y,T=0; char opt[6]; while(~scanf("%d",&N)){ if(T) printf("\n"); else T++; rep(i,0,N) Laxt[i]=0; cnt=0; rep(i,0,N+N) ch[i][0]=ch[i][1]=0; rep(i,1,N){ scanf("%d",&x); add(x,i); } build(); scanf("%d",&M); rep(i,1,M){ scanf("%s",opt); if(opt[0]=='Q'){ scanf("%d",&x); printf("%d\n",query(x)); } else { scanf("%d%d",&x,&y); move(x,y); } } } return 0; }