禁抄!!!!!!!!!!!!! #include <bits/stdc++.h> using namespace std; inline int read (void) { int res = 0;bool flag = true;char c = getchar(); while (c < ‘0’ || c > ‘9’) {flag ^= (c == ’-’);c = getchar();} while (c >= ‘0’ && c <= ‘9’) {res = (res << 3) + (res << 1) + (c ^ 48);c = getchar();} return flag ? res : -res; } const int MAXN = 2e5+10; int fa[MAXN],cnt[MAXN]; int find (int x) { return fa[x] = fa[x] == x ? x : find(fa[x]); } void merge (int a,int b) { a = find(a);b = find(b); if (a == b) return; fa[a] = b; cnt[b] += cnt[a]; } struct LCT { int sum[MAXN],ch[MAXN][2],fa[MAXN],tot; bool lazy[MAXN]; #define ls(x) ch[x][0] #define rs(x) ch[x][1] inline int dir (int fa,int x) {return ls(fa) == x ? 0 : 1;} inline bool isroot (int x) {return ls(fa[x]) != x && rs(fa[x]) != x;} inline void push_up (int x) {sum[x] = sum[ls(x)] + sum[rs(x)] + cnt[x];} inline void reverse (int x) {if (x) {swap(ls(x),rs(x));lazy[x] ^= true;}} inline void push_down (int x) { if (lazy[x]) { reverse(ls(x)); reverse(rs(x)); lazy[x] = false; } } void push (int x) { if (!isroot(x)) push(find(fa[x])); push_down(x); } void rotate (int x) { if (isroot(x)) return; int y = fa[x]; int z = fa[y],r = dir(y,x); ch[y][r] = ch[x][r^1]; ch[x][r^1] = y; if (!isroot(y)) ch[z][dir(z,y)] = x; if (ch[y][r]) fa[ch[y][r]] = y; fa[x] = z; fa[y] = x; push_up(y); push_up(x); } void splay (int x) { push(x); int y,z; while (!isroot(x)) { y = fa[x];z = fa[y]; if (!isroot(y)) rotate(dir(z,y) == dir(y,x) ? y : x); rotate(x); } push_up(x); } void access (int y) { for (int x = 0;y;x = y,y = fa[x] = find(fa[x])) { splay(y); rs(y) = x; push_up(y); } } void make_root (int x) { access(x);splay(x);reverse(x); } int find_root (int x) { access(x);splay(x); while (ls(x)) { push_down(x); x = ls(x); } splay(x); return x; } void split(int x,int y) { make_root(x); access(y); splay(y); } void link (int x,int y) { make_root(x);make_root(y); fa[x] = y; } void dfs (int x,int rt) { merge(x,rt); if (ls(x)) dfs(ls(x),rt); if (rs(x)) dfs(rs(x),rt); ls(x) = rs(x) = 0; } } a;
int n,m,tmp[MAXN]; int op,x,y; int main (void) { n = read(); m = read(); for (int i = 1;i <= n;i++) { tmp[i] = cnt[i] = read(); fa[i] = i; } while (m—) { op = read(); x = read(); y = read(); if (op == 2) { int p = find(x); a.make_root(p); cnt[p] += y-tmp[x]; tmp[x] = y; a.push_up(p); } else { x = find(x); y = find(y); if (op == 1) { if (a.find_root(x) != a.find_root(y)) a.link(x,y); else { a.split(x,y); a.dfs(y,y); a.push_up(y); } } else { if (a.find_root(x) != a.find_root(y)) printf (“-1\n”); else { a.split(x,y); printf (“%d\n”,a.sum[y]); } } } } return 0; }