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
| #include <cstdio> #include <algorithm> using namespace std; const int N = 1e6 + 10; int n, m, p[N]; struct Node { bool vis; int v, id, d, ls, rs; bool operator>(const Node &_) const { return v > _.v || (v == _.v && id > _.id); } } tr[N]; int fa(int x) { return x == p[x] ? x : p[x] = fa(p[x]); } int merge(int x, int y) { if (!x || !y) return x | y; if (tr[x] > tr[y]) swap(x, y); tr[x].rs = merge(tr[x].rs, y); if (tr[tr[x].rs].d > tr[tr[x].ls].d) swap(tr[x].ls, tr[x].rs); tr[x].d = tr[tr[x].rs].d + 1; return x; } void remove(int x) { tr[x].vis = true; p[tr[x].ls] = p[tr[x].rs] = p[x] = merge(tr[x].ls, tr[x].rs); tr[x].ls = tr[x].rs = tr[x].d = 0; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &tr[i].v); p[i] = tr[i].id = i; } scanf("%d", &m); for (char op[2]; m; m--) { scanf("%s", op); if (op[0] == 'M') { int x, y; scanf("%d%d", &x, &y); if (tr[x].vis || tr[y].vis) continue; x = fa(x), y = fa(y); if (x != y) p[x] = p[y] = merge(x, y); } if (op[0] == 'K') { int x; scanf("%d", &x); if (tr[x].vis) { puts("0"); continue; } x = fa(x); printf("%d\n", tr[x].v); remove(x); } } return 0; }
|