P4847

· · 题解

一眼下去 LCT。
原谅我 LCT 调了整整一天。
可以发现,本题其实就是有 n 个点,每个点有一个权值,需要支持以下三种操作:

像这种又断边又连边动态维护树上信息,可以试试 LCT。
由于本题每个节点最多一个孩子(因为是链),所以 LCT 没有虚边。

接下来考虑每一个操作。

需要注意的点:

code

#include <bits/stdc++.h>
#define fq(i,a,b) for (int i = (a); i <= (b); i++)
#define fnq(i,a,b) for (int i = (a); i < (b); i++)
#define nfq(i,a,b) for (int i = (a); i >= (b); i--)
#define nfnq(i,a,b) for (int i = (a); i > (b); i--)
#define fqs(i,a,b,c) for (int i = (a); i <= (b); i += (c))
#define fnqs(i,a,b,c) for (int i = (a); i < (b); i += (c))
#define nfqs(i,a,b,c) for (int i = (a); i >= (b); i -= (c))
#define nfnqs(i,a,b,c) for (int i = (a); i > (b); i -= (c))
#define elif else if
using namespace std;
#define int long long

inline int rd () {
    int f = 1;
    char ch = getchar ();
    while (!isdigit (ch)) (ch == '-' ? (f = -1) : 0), ch = getchar ();
    int num = 0;
    while (isdigit (ch)) num = num * 10 + ch - '0', ch = getchar ();
    return num * f;
}
#define d rd ()

// 不支持虚子树有关的 LCT
const int maxn = 200200;
int ch[maxn][2], f[maxn], sum[maxn], val[maxn], tag[maxn];
#define ls(p) ch[p][0]
#define rs(p) ch[p][1]
void push_up (int p) {sum[p] = sum[ls (p)] + sum[rs (p)] + val[p];}
bool isroot (int p) {return p && ls (f[p]) != p && rs (f[p]) != p;}
void rev (int p) {swap (ls (p), rs (p)); tag[p] ^= 1;}
void push_down (int p) {if (tag[p]) {if (ls (p)) rev (ls (p)); if (rs (p)) rev (rs (p)); tag[p] = 0;}}
int which (int p) {return ch[f[p]][1] == p;}
void connect (int p, int fa, int w) {ch[fa][w] = p, f[p] = fa;}
void rotate (int p) {
    int fa = f[p], ffa = f[fa], wp = which (p), wfa = which (fa), pson = ch[p][wp ^ 1];
    if (!isroot (fa)) connect (p, ffa, wfa); else f[p] = ffa;
    connect (pson, fa, wp); connect (fa, p, wp ^ 1); push_up (fa); push_up (p);
}
void push_all (int p) {if (!isroot (p)) push_all (f[p]); push_down (p);}
void splay (int p) {
    push_all (p);
    for (int fa; fa = f[p], !isroot (p); rotate (p)) if (!isroot (fa))
        rotate (which (fa) == which (p) ? fa : p);
}
int find (int p, int w) {
    splay (p); push_down (p);
    while (ch[p][w]) push_down (ch[p][w]), p = ch[p][w];
    splay (p); return p; 
}
#define findroot(p) find(p, 0)
#define findleaf(p) find(p, 1)
void cut (int p) {splay (p); f[ls (p)] = 0; ls (p) = 0; push_up (p);}
int sm (int p) {splay (p); return sum[ls (p)] + val[p];}
signed main () {
    int n = d, m = d;
    fq (i, 1, n) val[i] = d;
    while (m--) {
        char op[10]; scanf ("%s", op);
        if (op[0] == 'M') {
            int x = d, y = d;
            // link 放 main 里面了
            if (findroot (x) != findroot (y))
                f[findroot (x)] = findleaf (y), rs (findleaf (y)) = findroot (x), push_up (findleaf (y));
        } elif (op[0] == 'D') cut (d); 
        else {
            int x = d, y = d;
            if (findroot (x) != findroot (y)) puts ("-1");
            else {
                int vx = sm (x), vy = sm (y);
                printf ("%lld\n", (vx > vy ? vx - vy + val[y] : vy - vx + val[x]));
            }
        }
    }
    return 0;
}