# hdu3699

http://www.elijahqi.win/archives/833
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.

#include<cstdio>
#include<cstring>
#define N 110000
#define LL long long
#include<algorithm>
using namespace std;
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct node{
int y,next;
}data[N<<1];
struct node1{
int l,r,left,right;LL sum,lazy;
}tree[N<<2];
int num,size[N],dep[N],fa[N],w[N],id[N],a[N],son[N],h[N],tp[N],n,m,root,p;
void dfs1(int x){
size[x]=1;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (fa[x]==y) continue;
fa[y]=x;dep[y]=dep[x]+1;dfs1(y);size[x]+=size[y];
if (size[y]>size[son[x]]) son[x]=y;
}
}
void dfs2(int x,int top){
id[x]=++num;tp[x]=top;w[num]=a[x];
if (son[x]) dfs2(son[x],top);
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (y==fa[x]||y==son[x]) continue;
dfs2(y,y);
}
}
inline void update(int x){
int l=tree[x].left,r=tree[x].right;
tree[x].sum=tree[l].sum+tree[r].sum;
}
void build(int &x,int l,int r){
x=++num;tree[x].l=l;tree[x].r=r;tree[x].lazy=0;
if (l==r) {tree[x].sum=w[l];return;}
int mid=l+r>>1;
build(tree[x].left,l,mid);build(tree[x].right,mid+1,r);
update(x);
}
inline void pushdown(int x){
if (!tree[x].lazy) return;
int l=tree[x].left,r=tree[x].right;
LL lazy=tree[x].lazy;
tree[l].lazy+=lazy;tree[r].lazy+=lazy;
tree[l].sum+=(LL)(tree[l].r-tree[l].l+1)*lazy;
tree[r].sum+=(LL)(tree[r].r-tree[r].l+1)*lazy;
tree[x].lazy=0;
}
void change(int x,int l1,int r1,int v){
if (l1<=tree[x].l&&r1>=tree[x].r){
tree[x].sum+=(LL)(tree[x].r-tree[x].l+1)*v;tree[x].lazy+=v;
return;
}
int mid=(tree[x].l+tree[x].r)>>1;pushdown(x);
if (l1<=mid) change(tree[x].left,l1,r1,v);
if (r1>mid) change(tree[x].right,l1,r1,v);
update(x);
}
LL query(int x,int l1,int r1){
if (l1<=tree[x].l&&r1>=tree[x].r) return tree[x].sum;
int mid=(tree[x].l+tree[x].r)>>1;pushdown(x);LL tmp1=0,tmp2=0;
if (l1<=mid) tmp1=query(tree[x].left,l1,r1);
if (r1>mid) tmp2=query(tree[x].right,l1,r1);
return tmp1+tmp2;
}
int main(){
freopen("hdu3966.in","r",stdin);
while(~scanf("%d%d%d",&n,&m,&p)){
for (int i=1;i<n;++i){
data[++num].y=y;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].next=h[y];h[y]=num;
}dep[1]=1;num=0;
dfs1(1);dfs2(1,1);num=0;
build(root,1,n);
for (int i=1;i<=p;++i){
char s1[2];scanf("%s",s1);
if (s1[0]=='D'){
while (tp[x]!=tp[y]){
if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
change(root,id[tp[x]],id[x],-z);
x=fa[tp[x]];
}
if (id[x]>id[y]) swap(x,y);
change(root,id[x],id[y],-z);
}
if (s1[0]=='I'){
while (tp[x]!=tp[y]){
if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
change(root,id[tp[x]],id[x],z);
x=fa[tp[x]];
}
if (id[x]>id[y]) swap(x,y);
change(root,id[x],id[y],z);
}
if (s1[0]=='Q'){
printf("%lld\n",query(root,id[x],id[x]));
}
}
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120