http://acm.hdu.edu.cn/showproblem.php?pid=3966
Problem Description
Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges connect them. It is guaranteed
that for any two camps, there is one and only one path connect them. At first Aragorn know the number of enemies in every camp. But the enemy is cunning , they will increase or decrease the number of soldiers in camps. Every time the enemy change the number
of soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on the path from C1 to C2, they will increase or decrease K soldiers to these camps. Now Aragorn wants to know the number of soldiers in some particular camps real-time.
Input
Multiple test cases, process to the end of input.
For each case, The first line contains three integers N, M, P which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤ 100000) operations. The number of camps starts from 1.
The next line contains N integers A1, A2, ...AN(0 ≤ Ai ≤ 1000), means at first in camp-i has Ai enemies.
The next M lines contains two integers u and v for each, denotes that there is an edge connects camp-u and camp-v.
The next P lines will start with a capital letter ‘I‘, ‘D‘ or ‘Q‘ for each line.
‘I‘, followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, increase K soldiers to these camps.
‘D‘, followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, decrease K soldiers to these camps.
‘Q‘, followed by one integer C, which is a query and means Aragorn wants to know the number of enemies in camp C at that time.
Output
For each query, you need to output the actually number of enemies in the specified camp.
Sample Input
3 2 5 1 2 3 2 1 2 3 I 1 3 5 Q 2 D 1 2 2 Q 1 Q 3
Sample Output
7 4 8 Hint 1.The number of enemies may be negative. 2.Huge input, be careful.
/** hdu3966 树链剖分(区间更新和单点求值) 题目大意:给定一棵树,对指定的连点之间的点的权进行更新,过程中询问指定点的当前值 解题思路:树链剖分,区间更新维护的题目 */ #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; const int maxn=50005; typedef long long LL; int n,m,q,z; LL a[maxn],sum[maxn*4],col[maxn*4]; int fa[maxn],Hash[maxn],dep[maxn],son[maxn],siz[maxn]; int num[maxn],top[maxn]; int head[maxn],ip; void init() { memset(head,-1,sizeof(head)); memset(col,0,sizeof(col)); memset(sum,0,sizeof(sum)); ip=0; } struct note { int v,next; }edge[maxn*4]; void addedge(int u,int v) { edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++; } void dfs(int u,int pre) { son[u]=0,siz[u]=1,dep[u]=dep[pre]+1,fa[u]=pre; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==pre)continue; dfs(v,u); if(siz[son[u]]<siz[v]) { son[u]=v; } siz[u]+=siz[v]; } ///printf("%d fa,dep,siz,son %d %d %d %d\n",u,fa[u],dep[u],siz[u],son[u]); } void set_que(int u,int tp) { top[u]=tp,num[u]=++z,Hash[z]=u; if(son[u]) { set_que(son[u],tp); } for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==fa[u]||v==son[u])continue; set_que(v,v); } /// printf("%d top num %d %d\n",u,top[u],num[u]); } void push_up(int root) { sum[root]=sum[root<<1]+sum[root<<1|1]; } void build(int root,int l,int r) { col[root]=0; if(l==r) { sum[root]=a[Hash[l]]; return; } int mid=(l+r)>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r); push_up(root); } void push_down(int root,int len) { if(col[root]) { col[root<<1]+=col[root]; col[root<<1|1]+=col[root]; sum[root<<1]+=col[root]*(len-(len>>1)); sum[root<<1|1]+=col[root]*(len>>1); col[root]=0; } } void update(int root,int l,int r,int x,int y,int z) { if(l>y||r<x)return; if(x<=l&&r<=y) { col[root]+=z; sum[root]+=(r-l+1)*z; return; } push_down(root,r-l+1); int mid=(l+r)>>1; update(root<<1,l,mid,x,y,z); update(root<<1|1,mid+1,r,x,y,z); push_up(root); } LL query(int root,int l,int r,int loc) { if(l>loc||r<loc)return 0; if(l==r) { return sum[root]; } push_down(root,r-l+1); int mid=(l+r)>>1; if(loc<=mid) return query(root<<1,l,mid,loc); else return query(root<<1|1,mid+1,r,loc); } int main() { //freopen("data.txt","r",stdin); while(~scanf("%d%d%d",&n,&m,&q)) { init(); for(int i=1;i<=n;i++) { scanf("%I64d",&a[i]); } for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } int root=(n+1)>>1; z=0,dep[0]=0,siz[0]=0; dfs(root,0); set_que(root,root); build(1,1,z); while(q--) { char s[20]; int x,y,zz; scanf("%s",s); if(s[0]=='Q') { scanf("%d",&x); printf("%I64d\n",query(1,1,z,num[x])); } else { scanf("%d%d%d",&x,&y,&zz); if(s[0]=='D')zz=-zz; int f1=top[x],f2=top[y]; while(f1!=f2) { if(dep[f1]<dep[f2]) { swap(f1,f2); swap(x,y); } update(1,1,z,num[f1],num[x],zz); x=fa[f1],f1=top[x]; } if(dep[x]>dep[y]) { swap(x,y); } update(1,1,z,num[x],num[y],zz); } } } return 0; }