bzoj2733: [HNOI2012]永無鄉(splay)
阿新 • • 發佈:2017-08-12
getc splay sam enter 不存在 ++ sub inpu sample
Submit: 3778 Solved: 2020
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
2
5
1
2
2733: [HNOI2012]永無鄉
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3778 Solved: 2020
Description
永無鄉包含 n 座島,編號從 1 到 n,每座島都有自己的獨一無二的重要度,按照重要度可 以將這 n 座島排名,名次用 1 到 n 來表示。某些島之間由巨大的橋連接,通過橋可以從一個島 到達另一個島。如果從島 a 出發經過若幹座(含 0 座)橋可以到達島 b,則稱島 a 和島 b 是連 通的。現在有兩種操作:B x y 表示在島 x 與島 y 之間修建一座新橋。Q x k 表示詢問當前與島 x連通的所有島中第 k 重要的是哪座島,即所有與島 x 連通的島中重要度排名第 k 小的島是哪 座,請你輸出那個島的編號。
Input
輸入文件第一行是用空格隔開的兩個正整數 n 和 m,分別 表示島的個數以及一開始存在的橋數。接下來的一行是用空格隔開的 n 個數,依次描述從島 1 到島 n 的重要度排名。隨後的 m 行每行是用空格隔開的兩個正整數 ai 和 bi,表示一開始就存 在一座連接島 ai 和島 bi 的橋。後面剩下的部分描述操作,該部分的第一行是一個正整數 q, 表示一共有 q 個操作,接下來的 q 行依次描述每個操作,操作的格式如上所述,以大寫字母 Q 或B 開始,後面跟兩個不超過 n 的正整數,字母與數字以及兩個數字之間用空格隔開。 對於 20%的數據 n≤1000,q≤1000
對於 100%的數據 n≤100000,m≤n,q≤300000
Output
對於每個 Q x k 操作都要依次輸出一行,其中包含一個整數,表 示所詢問島嶼的編號。如果該島嶼不存在,則輸出-1。
Sample Input
5 14 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
-12
5
1
2
分析
可以用並查集維護連通性,這個沒啥好說的。
然後 Splay 啟發式合並,合並時,直接將小的暴力加入另一個。
這個調試了好久qwq。洛谷上還t了最後一個點
code
1 #include<cstdio> 2 #include<algorithm> 3 4 using namespace std; 5 6 const int MAXN = 100100; 7 int fa[MAXN],pa[MAXN],val[MAXN],ch[MAXN][2],siz[MAXN],q[MAXN]; 8 int n,m; 9 char opt[5]; 10 11 int read() 12 { 13 int x = 0, f = 1;char ch = getchar(); 14 while (ch<‘0‘||ch>‘9‘) {if(ch==‘-‘)f=-1; ch=getchar(); } 15 while (ch>=‘0‘&&ch<=‘9‘) {x=x*10+ch-‘0‘; ch=getchar(); } 16 return x*f; 17 } 18 void pushup(int x) 19 { 20 if (!x) return ; 21 siz[x] = siz[ch[x][0]]+siz[ch[x][1]]+1; 22 } 23 int son(int x) 24 { 25 return ch[fa[x]][1]==x;//不是等於1,qwq 26 } 27 void rotate(int x) 28 { 29 int y = fa[x],z = fa[y],b = son(x),c = son(y),a = ch[x][!b]; 30 if (z) ch[z][c] = x;fa[x] = z; 31 if (a) fa[a] = y;ch[y][b] = a; 32 ch[x][!b] = y;fa[y] = x; 33 pushup(y); 34 35 } 36 void splay(int x,int rt) 37 { 38 while (fa[x]!=rt) 39 { 40 int y = fa[x],z = fa[y]; 41 if (z==rt) rotate(x); 42 else 43 { 44 if (son(x)==son(y)) rotate(y),rotate(x); 45 else rotate(x), rotate(x); 46 } 47 } 48 pushup(x); 49 } 50 void insert(int &x,int pre,int id) 51 { 52 if (!x) 53 { 54 x = id;fa[x] = pre;siz[x] = 1; 55 splay(x,0); 56 return ; 57 } 58 if (val[id]<=val[x]) insert(ch[x][0],x,id); 59 else insert(ch[x][1],x,id); 60 pushup(x); 61 } 62 int getkth(int x,int k) 63 { 64 if (k<0 || k>siz[x]) return -1; 65 while (k<=siz[ch[x][0]] || k>siz[ch[x][0]]+1)//x是變的,不能直接l = ch[x][0],用l代替chp[x][0]使用 66 if (k<=siz[ch[x][0]]) x = ch[x][0]; 67 else k -= siz[ch[x][0]]+1, x = ch[x][1];; 68 splay(x,0); 69 return x; 70 } 71 void merge(int x,int y) 72 { 73 splay(x,0);splay(y,0); 74 if (siz[x]>siz[y]) swap(x,y);//啟發式合並 75 int head = 0, tail = 1; 76 q[0] = y,q[1] = x; 77 while (head<tail) 78 { 79 int t = q[++head]; 80 if (ch[t][0]) q[++tail] = ch[t][0]; 81 if (ch[t][1]) q[++tail] = ch[t][1]; 82 ch[t][0] = ch[t][1] = 0; 83 insert(q[head-1],0,t); 84 } 85 } 86 int find(int x) 87 { 88 return pa[x]==x?x:pa[x]=find(pa[x]); 89 } 90 int main() 91 { 92 n = read(),m = read(); 93 for (int i=1; i<=n; ++i) 94 { 95 val[i] = read(); 96 pa[i] = i;siz[i] = 1; 97 } 98 for (int x,y,i=1; i<=m; ++i) 99 { 100 x = read();y = read(); 101 if (find(x)!=find(y)) 102 { 103 merge(x,y); 104 pa[find(x)] = find(y);//設為find(y),不是y 105 } 106 } 107 int t = read(); 108 while (t--) 109 { 110 scanf("%s",opt); 111 int x = read(), y = read(); 112 if (opt[0]==‘Q‘) 113 { 114 splay(x,0); 115 printf("%d\n",getkth(x,y)); 116 } 117 else 118 { 119 if (find(x)!=find(y)) 120 { 121 merge(x,y); 122 pa[find(x)] = find(y); 123 } 124 } 125 } 126 return 0; 127 }
bzoj2733: [HNOI2012]永無鄉(splay)