这个题目是一个比较裸的树剖题,很好写。
http://acm.hdu.edu.cn/showproblem.php?pid=3966
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <algorithm> #include <cstdlib> #include <vector> #include <stack> #include <map> #include <string> #define inf 0x3f3f3f3f #define inf64 0x3f3f3f3f3f3f3f3f using namespace std; typedef long long ll; const int maxn = 5e4 + 10; int f[maxn];//f 保存u的父亲节点 int dep[maxn];//dep保存节点u 的深度 int siz[maxn];//siz保存以u为根的子节点的个数 int son[maxn];//son 保存u的重儿子 int rk[maxn];//rk当前dfs序在树中所对应的节点 int top[maxn];// top保存当前结点所在链的顶端结点 int id[maxn];//dfs的执行顺序 int a[maxn]; int n; ll sum[maxn * 4], lazy[maxn * 4]; //------------------线段树部分---------------// void push_up(int id) { sum[id] = sum[id << 1] + sum[id << 1 | 1]; // printf("sum[%d]=%d sum[%d]=%d ", id << 1, sum[id << 1], id << 1 | 1, sum[id << 1 | 1]); // printf("sum[%d]=%d ", id, sum[id]); } void build(int id, int l, int r) { lazy[id] = 0; if (l == r) { sum[id] = a[rk[l]]; // printf("id=%d sum=%d ", id, sum[id]); return; } int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); push_up(id); } void push_down(int id, int len1, int len2) { if (lazy[id] == 0) return; sum[id << 1] += lazy[id] * len1; lazy[id << 1] += lazy[id]; sum[id << 1 | 1] += lazy[id] * len2; lazy[id << 1 | 1] += lazy[id]; lazy[id] = 0; } void update(int id, int l, int r, int x, int y, int val) { // printf("id=%d l=%d r=%d x=%d y=%d val=%d ", id, l, r, x, y, val); if (x <= l && y >= r) { // printf("id=%d sum=%d ", id, sum[id]); sum[id] += val * (r - l + 1); lazy[id] += val; // printf("%d ", sum[id]); return; } int mid = (l + r) >> 1; push_down(id, mid - l + 1, r - mid); if (x <= mid) update(id << 1, l, mid, x, y, val); if (y > mid) update(id << 1 | 1, mid + 1, r, x, y, val); push_up(id); } ll query(int id, int l, int r, int x, int y) { if (x <= l && y >= r) return sum[id]; int mid = (l + r) >> 1; ll ans = 0; push_down(id, mid - l + 1, r - mid); if (x <= mid) ans = (ans + query(id << 1, l, mid, x, y)); if (y > mid) ans = (ans + query(id << 1 | 1, mid + 1, r, x, y)); return ans; } //------------------------树链剖分-------------------// // int f[maxn];//f 保存u的父亲节点 // int dep[maxn];//dep保存节点u 的深度 // int siz[maxn];//siz保存以u为根的子节点的个数 // int son[maxn];//son 保存u的重儿子 // int rk[maxn];//rk当前dfs序在树中所对应的节点 // int top[maxn];// top保存当前结点所在链的顶端结点 // int id[maxn];//dfs的执行顺序 struct node { int v, nxt; node(int v = 0, int nxt = 0) :v(v), nxt(nxt) {} }ex[maxn]; int head[maxn], cnt = 0, tot; void init() { cnt = 0, tot = 0; memset(son, 0, sizeof(son)); memset(head, -1, sizeof(head)); } void add(int u, int v) { ex[cnt] = node(v, head[u]); head[u] = cnt++; ex[cnt] = node(u, head[v]); head[v] = cnt++; } void dfs1(int u, int fa, int depth) { f[u] = fa; dep[u] = depth; siz[u] = 1; for (int i = head[u]; i != -1; i = ex[i].nxt) { int v = ex[i].v; if (v == fa) continue; dfs1(v, u, depth + 1); siz[u] += siz[v]; if (siz[v] > siz[son[u]]) son[u] = v; } } void dfs2(int u, int t) { top[u] = t; id[u] = ++tot;//标记dfs序 rk[tot] = u;//序号tot对应的结点u if (!son[u]) return; dfs2(son[u], t); /*我们选择优先进入重儿子来保证一条重链上各个节点dfs序连续, 一个点和它的重儿子处于同一条重链,所以重儿子所在重链的顶端还是t*/ for (int i = head[u]; i != -1; i = ex[i].nxt) { int v = ex[i].v; if (v != son[u] && v != f[u]) dfs2(v, v);//一个点位于轻链底端,那么它的top必然是它本身 } } void update2(int x, int y, int z)//修改x到y路径的值 { while (top[x] != top[y])//不在同一条链上 { if (dep[top[x]] < dep[top[y]]) swap(x, y);//x为深度大的链 update(1, 1, n, id[top[x]], id[x], z);//x为深度大的链 x = f[top[x]];//深度大的向上跳 } if (dep[x] > dep[y]) swap(x, y); //这里x和y在同一条链 update(1, 1, n, id[x], id[y], z); //x和y这条链的更新 } ll query2(int x, int y) { ll ret = 0; while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); ret = (ret + query(1, 1, n, id[top[x]], id[x])); x = f[top[x]]; } if (dep[x] > dep[y]) swap(x, y); ret = (ret + query(1, 1, n, id[x], id[y])); return ret; } //------------------树链剖分结束-------------------// int main() { int m, p; while (scanf("%d%d%d", &n, &m, &p) != EOF) { init(); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); add(u, v); } dfs1(1, -1, 1), dfs2(1, 1); build(1, 1, n); while (p--) { char s[10]; int l, r, k; scanf("%s", s); if (s[0] == 'I') { scanf("%d%d%d", &l, &r, &k); update2(l, r, k); } else if (s[0] == 'D') { scanf("%d%d%d", &l, &r, &k); update2(l, r, -k); } else { scanf("%d", &l); ll ans = query(1, 1, n, id[l], id[l]); printf("%lld ", ans); } } } return 0; }