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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| #include<bits/stdc++.h> using namespace std; const int MAXn = 1e5;
template <typename T> inline void read(T &a) { char c;for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar());bool f = c == '-';T x = f ? 0 : (c ^ '0');for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) {x = x * 10 + (c ^ '0');}a = f ? -x : x; } template <typename T, typename ...Argv> inline void read(T &a, Argv &...argv) { read(a), read(argv...); }
struct UniSet { int fa[MAXn + 10]; void init(int n) { for (int i = 1; i <= n; ++i) { fa[i] = i; } } int anc(int x) { return fa[x] = ((fa[x] == x) ? x : anc(fa[x])); } void changeanc(int x, int y) { fa[x] = y; fa[y] = y; } }; UniSet us;
int ls[MAXn + 10], rs[MAXn + 10]; int hig[MAXn + 10]; bool isdel[MAXn + 10]; pair<int, int> val[MAXn + 10]; void init() { hig[0] = -1; } inline void pushup(int id) { hig[id] = hig[rs[id]] + 1; } int merge(int x, int y) { if (x == 0 || y == 0) return x + y; if (val[x] > val[y]) swap(x, y); rs[x] = merge(rs[x], y); if (hig[ls[x]] < hig[rs[x]]) swap(ls[x], rs[x]); pushup(x); return x; } int pop(int x) { isdel[x] = 1; return merge(ls[x], rs[x]); }
int n, m; signed main() { read(n, m); for (int i = 1; i <= n; ++i) { read(val[i].first); val[i].second = i; } us.init(n); for (int i = 1, opt, x, y; i <= m; ++i) { read(opt, x); if (opt == 1) { read(y); if (isdel[x] || isdel[y]) continue; int ancx = us.anc(x), ancy = us.anc(y); if (ancx == ancy) continue; if (merge(ancx, ancy) == ancx) { us.fa[ancy] = ancx; } else { us.fa[ancx] = ancy; } } else { if (isdel[x]) { puts("-1"); continue; } int ancx = us.anc(x); printf("%d\n", val[ancx].first); us.changeanc(ancx, pop(ancx)); } } return 0; }
|