题意:给一棵树,并给定各个点权的值,然后有3种操作:
I C1 C2 K: 把C1与C2的路径上的所有点权值加上K
D C1 C2 K:把C1与C2的路径上的所有点权值减去K
Q C:查询节点编号为C的权值
析:就是简单的树链剖分,可以用树状数组来维护,然后就OK了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e16; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 50000 + 10; const int mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } struct Edge{ int to, next; }; Edge edges[maxn<<1]; int p[maxn], fp[maxn], son[maxn], top[maxn]; int dp[maxn], fa[maxn], head[maxn], num[maxn]; int pos, cnt; int sum[maxn]; void init(){ pos = 1; cnt = 0; memset(sum, 0, sizeof sum); memset(son, -1, sizeof son); memset(head, -1, sizeof head); } int a[maxn]; void add_edge(int u, int v){ edges[cnt].to = v; edges[cnt].next = head[u]; head[u] = cnt++; } void dfs1(int u, int f, int d){ dp[u] = d; fa[u] = f; num[u] = 1; for(int i = head[u]; ~i; i = edges[i].next){ int v = edges[i].to; if(v == f) continue; dfs1(v, u, d+1); num[u] += num[v]; if(son[u] == -1 || num[v] > num[son[u]]) son[u] = v; } } void dfs2(int u, int sp){ top[u] = sp; p[u] = pos++; fp[p[u]] = u; if(son[u] == -1) return ; dfs2(son[u], sp); for(int i = head[u]; ~i; i = edges[i].next){ int v = edges[i].to; if(v != fa[u] && v != son[u]) dfs2(v, v); } } int lowbit(int x){ return -x&x; } void add(int x, int val){ while(x <= n){ sum[x] += val; x += lowbit(x); } } int getSum(int x){ int ans = 0; while(x){ ans += sum[x]; x -= lowbit(x); } return ans; } void update(int u, int v, int val){ int f1 = top[u], f2 = top[v]; while(f1 != f2){ if(dp[f1] < dp[f2]){ swap(f1, f2); swap(u, v); } add(p[f1], val); add(p[u]+1, -val); u = fa[f1]; f1 = top[u]; } if(dp[u] > dp[v]) swap(u, v); add(p[u], val); add(p[v]+1, -val); } int main(){ int q; while(scanf("%d %d %d", &n, &m, &q) == 3){ for(int i = 1; i <= n; ++i) scanf("%d", a+i); init(); for(int i = 0; i < m; ++i){ int u, v; scanf("%d %d", &u, &v); add_edge(u, v); add_edge(v, u); } dfs1(1, 0, 0); dfs2(1, 1); for(int i = 1; i <= n; ++i){ add(p[i], a[i]); add(p[i]+1, -a[i]); } int u, v, x; while(q--){ char s[5]; scanf("%s", s); if(s[0] == 'Q'){ scanf("%d", &x); printf("%d\n", getSum(p[x])); continue; } scanf("%d %d %d", &u, &v, &x); if(s[0] == 'D') x = -x; update(u, v, x); } } return 0; }