http://www.lydsy.com/JudgeOnline/problem.php?id=1103
树剖边权转点权,vector存图卡一下午RE
气炸。。
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <string> #include <vector> using namespace std; const int N = 250011; #define yxy getchar() #define lson jd << 1 #define rson jd << 1 | 1 #define RR freopen("gg.in", "r", stdin) int top[N], deep[N], fa[N], size[N], son[N], tree[N]; int now = 1, n, Ty, Tim, ans; vector <int> vec[N << 2]; int W[N << 2]; int head[N]; struct Node {int u, v, nxt;} G[N << 1]; void add(int u, int v) { G[now].v = v; G[now].nxt = head[u]; head[u] = now ++; } inline int read() { int x = 0; char c = yxy; while(c < ‘0‘ || c > ‘9‘) c = yxy; while(c >= ‘0‘ && c <= ‘9‘) x = x * 10 + c - ‘0‘, c = yxy; return x; } void Dfs_son(int u, int f_, int dep) { fa[u] = f_; deep[u] = dep; size[u] = 1; for(int i = head[u]; ~ i; i = G[i].nxt) { int v = G[i].v; if(v != f_) { Dfs_son(v, u, dep + 1); size[u] += size[v]; if(size[v] > size[son[u]]) son[u] = v; } } } void Dfs_un(int u, int tp) { top[u] = tp; tree[u] = ++ Tim; if(!son[u]) return ; Dfs_un(son[u], tp); for(int i = head[u]; ~ i; i = G[i].nxt) { int v = G[i].v; if(v != fa[u] && v != son[u]) Dfs_un(v, v); } } void Build_tree(int l, int r, int jd) { if(l == r) { if(l != 1) W[jd] ++; return ; } int mid = (l + r) >> 1; Build_tree(l, mid, lson); Build_tree(mid + 1, r, rson); W[jd] = W[lson] + W[rson]; } void Sec_A(int l, int r, int jd, int x, int y) { if(x <= l && r <= y) { ans += W[jd]; return ; } int mid = (l + r) >> 1; if(x <= mid) Sec_A(l, mid, lson, x, y); if(y > mid) Sec_A(mid + 1, r, rson, x, y); } inline int Sec_A_imp(int x) { int tp1 = top[x], ret(0); while(tp1 != 0) { ans = 0; Sec_A(1, n, 1, tree[tp1], tree[x]); ret += ans; x = fa[tp1]; tp1 = top[x]; } return ret; } void Poi_G(int l, int r, int jd, int x) { if(l == r) { W[jd] = 0; return ; } int mid = (l + r) >> 1; if(x <= mid) Poi_G(l, mid, lson, x); else Poi_G(mid + 1, r, rson, x); W[jd] = W[lson] + W[rson]; } int main() { n = read(); for(int i = 1; i <= n; i ++) head[i] = -1; for(int i = 1; i < n; i ++) { int u = read(), v = read(); if(u > v) swap(u, v); add(u, v); } Dfs_son(1, 0, 1); Dfs_un(1, 1); Build_tree(1, n, 1); Ty = read(); Ty += (n - 1); while(Ty --) { char c[15]; scanf("%s", c); if(c[0] == ‘W‘) { int x = read(); printf("%d\n", Sec_A_imp(x)); } else { int x = read(), y = read(); if(x < y) swap(x, y); Poi_G(1, n, 1, tree[x]); } } return 0; }