Codeforces 916E Jamie and Tree 线段树
我们按 1 号点为根建dfs序线段树。即使换了根, 我们也能通过分类讨论去更新求答案。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 5e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, q, a[N]; int idx, dfn[N], in[N], ot[N]; int depth[N], pa[N][20]; vector<int> G[N]; struct SegmentTree { #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 LL sum[N << 2], lazy[N << 2]; inline void pull(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } inline void push(int rt, int l, int r) { if(lazy[rt]) { int mid = l + r >> 1; sum[rt << 1] += lazy[rt] * (mid - l + 1); sum[rt << 1 | 1] += lazy[rt] * (r - mid); lazy[rt << 1] += lazy[rt]; lazy[rt << 1 | 1] += lazy[rt]; lazy[rt] = 0; } } void update(int L, int R, LL val, int l, int r, int rt) { if(R < l || r < L || R < L) return; if(L <= l && r <= R) { sum[rt] += val * (r - l + 1); lazy[rt] += val; return; } push(rt, l, r); int mid = l + r >> 1; update(L, R, val, lson); update(L, R, val, rson); pull(rt); } LL query(int L, int R, int l, int r, int rt) { if(R < l || r < L || R < L) return 0; if(L <= l && r <= R) return sum[rt]; push(rt, l, r); int mid = l + r >> 1; return query(L, R, lson) + query(L, R, rson); } } Tree; void dfs(int u, int fa) { in[u] = ++idx; depth[u] = depth[fa] + 1; pa[u][0] = fa; for(int i = 1; i < 20; i++) pa[u][i] = pa[pa[u][i - 1]][i - 1]; for(auto& v : G[u]) if(v != fa) dfs(v, u); ot[u] = idx; } int getLca(int u, int v) { if(depth[u] < depth[v]) swap(u, v); int dis = depth[u] - depth[v]; for(int i = 19; i >= 0; i--) if(dis >> i & 1) u = pa[u][i]; if(u == v) return u; for(int i = 19; i >= 0; i--) if(pa[u][i] != pa[v][i]) u = pa[u][i], v = pa[v][i]; return pa[u][0]; } int go(int u, int dis) { for(int i = 19; i >= 0; i--) if(dis >> i & 1) u = pa[u][i]; return u; } bool ok(int i, int j) { return in[i] <= in[j] && ot[j] <= ot[i]; } int main() { scanf("%d%d", &n, &q); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 2; i <= n; i++) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } dfs(1, 0); for(int i = 1; i <= n; i++) Tree.update(in[i], in[i], a[i], 1, n, 1); int Rt = 1; while(q--) { int op, u, v, w, x; scanf("%d", &op); if(op == 1) { scanf("%d", &v); Rt = v; } else if(op == 2) { scanf("%d%d%d", &u, &v, &x); if(ok(Rt, u) && ok(Rt, v)) { int lca = getLca(u, v); if(lca != Rt) Tree.update(in[lca], ot[lca], x, 1, n, 1); else Tree.update(1, n, x, 1, n, 1); } else if(ok(Rt, u)) { Tree.update(1, n, x, 1, n, 1); } else if(ok(Rt, v)) { Tree.update(1, n, x, 1, n, 1); } else { w = getLca(u, v); int lca = getLca(Rt, w); if(lca == w) { int p1 = getLca(Rt, u); int p2 = getLca(Rt, v); if(depth[p1] > depth[p2]) w = p1; else w = p2; int who = go(Rt, depth[Rt] - depth[w] - 1); Tree.update(1, in[who] - 1, x, 1, n, 1); Tree.update(ot[who] + 1, n, x, 1, n, 1); } else { Tree.update(in[w], ot[w], x, 1, n, 1); } } } else { scanf("%d", &w); LL ans = 0; if(w == Rt) ans = Tree.query(1, n, 1, n, 1); else { if(ok(w, Rt)) { int who = go(Rt, depth[Rt] - depth[w] - 1); ans += Tree.query(1, in[who] - 1, 1, n, 1); ans += Tree.query(ot[who] + 1, n, 1, n, 1); } else { ans = Tree.query(in[w], ot[w], 1, n, 1); } } printf("%lld\n", ans); } } return 0; } /* */