2733: [HNOI2012] Yong No township time limit:10 Sec Memory limit:128 MB
submit:3955 solved:2112
[Submit] [Status] [Discuss] Description
Yong No township contains N Island, numbering from 1 to N, each island has its own unique importance, according to the importance of this can be ranked in the N Island, ranking with 1 to N to represent. Some of the islands are connected by huge bridges that can be reached from one island to another via a bridge. If a number of blocks (including 0-seater) can reach Island B from Island A, it is said that island A and island B are connected. There are now two operations: B x y to construct a bridge between Island X and Island Y. Q x K indicates which island is important in all the islands that are currently connected to Island X, which is the island of importance in all islands connected with island X, please output the number of that island.
Input
The first line of the input file is two positive integers, N and M, separated by a space, representing the number of islands and the number of bridges that existed at the beginning. The next line is the number of n separated by spaces, which in turn describes the importance ranking from Island 1 to Island N. Each row of the subsequent m line is a two positive integer AI and bi separated by a space, indicating that there was a bridge connecting island AI and island bi at the outset. The remainder of the following describes the operation, the first line of the section is a positive integer q, indicating that there is a total of Q operations, the next Q line describes each operation, the format of the operation as described above, starting with the capital letter Q or B, followed by two not more than n positive integers, letters and numbers and two numbers separated by a space. For 20% of data n≤1000,q≤1000
For 100% of data n≤100000,m≤n,q≤300000
Output
For each Q x K operation, one line is output, which contains an integer that represents the number of the island being queried. If the island does not exist, then output-1.
Sample Input5 1
4 3 2) 5 1
1 2
7
Q 3 2
Q 2 1
B 2 3
B 1 5
Q 2 1
Q 2 4
Q 2 3
Sample Output-1
2
5
1
2
the problem at the time of the first thought is like a graph theory tarjan problem, and then saw the operation of the ranking, which has to consider the balance tree, because the balance of the tree I will only splay, will not have no spin treap so I only speak splay. This problem think of the balance of the tree began to want to merge operations, if the violent merger of the worst seems to be a blow-up, at first thought there is any magical play, such as the interval inserted also self-balanced black technology and the like, the result Deskmate told me is a heuristic merger. The so-called heuristic merger is a violent merger, just a pruning that almost everyone wants, so that the small items to merge large items, the time will naturally be much smaller. so for each merger we are just DFS side of the current kid tree and then the violence merges just fine, other operations as usual.
1 #pragmaGCC Optimze ("O3")2#include <iostream>3#include <cstdlib>4#include <cstdio>5#include <cstring>6#include <queue>7#include <algorithm>8#include <cmath>9#include <map>Ten #defineN 100004 One using namespacestd; A intn,m,q,f[n],fa[n],size[n],ch[n][2],data[n]; - intFindintx) - { the if(f[x]==x)returnx; - returnf[x]=find (F[x]); - } - BOOL Get(intx) + { - returnx==ch[fa[x]][1]; + } A voidUpdateintx) at { - if(x) - { -size[x]=1; - if(ch[x][0]) size[x]+=size[ch[x][0]]; - if(ch[x][1]) size[x]+=size[ch[x][1]]; in } - } to voidRotateintx) + { - intFaa=fa[x],ffa=Fa[fa[x]]; the intop=Get(x); *ch[faa][op]=ch[x][op^1]; $fa[ch[faa][op]]=FAA;Panax Notoginsengch[x][op^1]=FAA; -fa[faa]=x; thefa[x]=FFA; + if(FFA) ch[ffa][ch[ffa][1]==faa]=x; A Update (FAA); the update (x); + return; - } $ voidSplay (intx) $ { - for(intff;ff=fa[x];rotate (x)) - { the if(Fa[ff]) -Rotate ((Get(x) = =Get(FF))?ff:x);Wuyi } the return; - } Wu intLart; - voidInsertintXintNowintFAA) About { $ - if(now==0) - { -fa[x]=FAA; A if(DATA[X]<DATA[FAA]) ch[faa][0]=x; + Elsech[faa][1]=x; the splay (x); -lart=x; $ return; the } the if(Data[x]<data[now]) insert (x,ch[now][0],now); the ElseInsert (x,ch[now][1],now); the } - voidDfsintXinty) in { the intle=ch[x][0],ri=ch[x][1]; thech[x][0]=ch[x][1]=0; AboutInsert (x, Y,0); the if(LE) DFS (le,lart); the if(RI) DFS (ri,lart); the } + intGet_rank (intXintLA) - { the if(la==size[ch[x][0]]+1)Bayi returnx; the if(la<=size[ch[x][0]]) the returnGet_rank (ch[x][0],la); - Else - returnGet_rank (ch[x][1],la-size[ch[x][0]]-1); the } the intMain () the { thescanf"%d%d",&n,&m); - for(intI=1; i<=n;i++) the { thescanf"%d",&data[i]); thef[i]=i;size[i]=1;94 } the for(intI=1; i<=m;i++) the { the intx, y;98scanf"%d%d",&x,&y); About intAa=find (x), bb=find (y); - if(AA==BB)Continue;101f[aa]=BB;102 splay (x), splay (y);103 if(size[x]>Size[y])104 DFS (y,x); the Else106 dfs (x, y);107 }108scanf"%d",&q);109 Charb[Ten]; the for(intI=1; i<=q;i++)111 { thescanf"%s", b);113 intx, y; thescanf"%d%d",&x,&y); the if(b[0]=='B') the {117 intAa=find (x), bb=find (y);118 if(AA==BB)Continue;119f[aa]=BB; - splay (x), splay (y);121 if(size[x]>Size[y])122 DFS (y,x);123 Else124 dfs (x, y); the }126 Else127 { - splay (x);129 if(size[x]<y) printf ("-1\n"); the Else131 { theprintf"%d\n", Get_rank (x, y));133 }134 }135 }136 return 0;137}
View Code
Bzoj 2733: [HNOI2012] never village array splay+ heuristic merge