inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 100007 bool invalid[N]; int son[N][2], f[N], dis[N], w[N];
inlineintfind(int x){return x == f[x] ? x : f[x] = find(f[x]);}
intmerge(int rt1, int rt2){ if (!rt1 || !rt2) return rt1 | rt2; // rt1 be the root if (w[rt1] > w[rt2] || (w[rt1] == w[rt2] && rt1 > rt2)) swap(rt1, rt2); int ls = son[rt1][0], rs = son[rt1][1]; rs = merge(rs, rt2); son[rt1][1] = rs; if (dis[ls] < dis[rs]) {swap(ls, rs); swap(son[rt1][0], son[rt1][1]);} f[ls] = f[rs] = rt1; dis[rt1] = dis[rs] + 1; return rt1; }
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) {w[i] = rd(); f[i] = i;} for (int i = 1; i <= m; ++i) { int op = rd(); if (op == 1) { int x = rd(), y = rd(); if (invalid[x] || invalid[y]) continue; if (find(x) != find(y)) merge(find(x), find(y)); } else { int x = rd(); if (invalid[x]) puts("-1"); elseprintf("%d\n", del(find(x))); } } return0; }
Post title:左偏树
Post author:Eva.Q
Create time:2023-08-06 19:25:15
Post link:https://qyy/2023/08/06/Algorithm/leftist-tree/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.