1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #include<bits/stdc++.h> using namespace std; const int N = 200010; int n; int v[N], dist[N], l[N], r[N]; int p[N]; bool cmp(int x, int y) { if(v[x] != v[y]) return v[x] < v[y]; return x < y; } int find(int x) { if(p[x] != x) return p[x] = find(p[x]); return p[x]; } int merge(int x, int y) { if(!x || !y) return x + y; if(cmp(y, x)) swap(x, y); r[x] = merge(r[x], y); if(dist[r[x]] > dist[l[x]]) swap(l[x], r[x]); dist[x] = dist[r[x]] + 1; return x; } int idx; int main() { scanf("%d", &n); v[0] = 2e9; while(n --) { int t, x, y; scanf("%d%d", &t, &x); if(t == 1) { v[++ idx] = x; p[idx] = idx; dist[idx] = 1; } else if(t == 2) { scanf("%d", &y); x = find(x), y = find(y); if(x != y) { if(cmp(y, x)) swap(x, y); p[y] = x; merge(x, y); } } else if(t == 3) { printf("%d\n", v[find(x)]); } else { x = find(x); if(cmp(r[x], l[x])) swap(l[x], r[x]); p[x] = l[x], p[l[x]] = l[x]; merge(l[x], r[x]); } } return 0; }
|