可并堆模板
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define rd read() 5 #define R register 6 using namespace std; 7 8 const int N = 1e5 + 1e4; 9 10 int ch[N][2], dis[N], val[N], f[N]; 11 int n, m; 12 13 int read() { 14 int X = 0, p = 1; char c = getchar(); 15 for(; c > '9' || c < '0'; c = getchar()) if(c == '-') p = -1; 16 for(; c >= '0' && c <= '9'; c = getchar()) X = X * 10 + c - '0'; 17 return X * p; 18 } 19 20 inline void sw(int &a, int &b) { 21 a ^= b, b ^= a, a ^= b; 22 } 23 24 int merge(int x, int y) { 25 if(!x || !y) return x + y; 26 if(val[x] > val[y] || (val[x] == val[y] && x > y)) sw(x, y); 27 ch[x][1] = merge(ch[x][1], y); 28 f[ch[x][1]] = x; 29 if(dis[ch[x][1]] > dis[ch[x][0]]) sw(ch[x][1], ch[x][0]); 30 dis[x] = dis[ch[x][1]] + 1; 31 return x; 32 } 33 34 void pop(int x) { 35 val[x] = -1; 36 f[ch[x][1]] = f[ch[x][0]] = 0; 37 merge(ch[x][1], ch[x][0]); 38 } 39 40 int get(int x) { 41 while(f[x]) x = f[x]; 42 return x; 43 } 44 45 int main() 46 { 47 n = rd; m = rd; 48 for(int i = 1; i <= n; ++i) val[i] = rd; 49 for(; m; --m) { 50 int k = rd; 51 if(k == 1) { 52 int x = rd, y = rd; 53 if(val[x] == -1 || val[y] == -1) continue; 54 x = get(x); y = get(y); 55 if(x == y) continue; 56 merge(x, y); 57 } 58 if(k == 2) { 59 int x = rd; 60 if(val[x] == -1) {puts("-1"); continue;} 61 x = get(x); 62 printf("%d\n", val[x]); 63 pop(x); 64 } 65 } 66 }