# Aragorn's Story

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17156    Accepted Submission(s): 4524

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.

Source

2011 Multi-University Training Contest 13 - Host by HIT

Recommend

We have carefully selected several similar problems for you:  3964 3965 3962 3963 3967

【题意】

【分析】

【代码-线段树】

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX=5e4+5;
const int INF=0x3f3f3f3f;

/*********** 图 ***********/
struct node{
int t,next;
}edge[MAX*2];
void init(int n)
{
}
{
}

/************ 树链剖分 **************/
int top[MAX],rk[MAX],id[MAX];
void dfs1(int u,int pre)
{
siz[u]=1; //u本身
son[u]=-1;
int maxsiz=0; //最大的儿子子树大小
{
int v=edge[i].t;
if(v==pre)continue;
dep[v]=dep[u]+1;
dfs1(v,u); //注意dfs的位置
siz[u]+=siz[v];
if(maxsiz<siz[v])
{
maxsiz=siz[v];
son[u]=v;
}
}
}
void dfs2(int u,int fat,int &tag) //结点u，重链顶端
{
top[u]=fat;
rk[u]=++tag; //时间戳
id[tag]=u; //tag对应在树上的结点号
if(son[u]==-1)return; //没有孩子
dfs2(son[u],fat,tag); //优先重链dfs
{
int v=edge[i].t;
dfs2(v,v,tag);
}
}
void cuttree(int root) //将root为根的树链剖分
{
int tag=0;
dep[root]=0;  //根的深度和父结点做一个初始化
dfs1(root,root);
dfs2(root,root,tag);
}

/************* 线段树 ****************/
ll sum[MAX<<2],mark[MAX<<2];
void build(int root,int l,int r) //初始线段树
{
if(l==r)//叶子节点
{
mark[root]=0;
sum[root]=0;
return;
}
int mid=(l+r)/2;
build(root*2,l,mid);   //左子树的创建
build(root*2+1,mid+1,r); //右子树
sum[root]=sum[root*2]+sum[root*2+1];
mark[root]=0;
}
void nodeupdate(int root,int l,int r,ll num)
{
mark[root] +=num;  //=
sum[root]+=num*(r-l+1);  //=
}
void pushdown(int root,int l,int r)//传递给两孩子
{
if(mark[root]==0)return;
int mid=(l+r)/2;
nodeupdate(root*2,l,mid,mark[root]);
nodeupdate(root*2+1,mid+1,r,mark[root]);
mark[root]=0;
}
void update(int left,int right,ll num, int root,int l,int r)//区间[kl,kr]修改
{
if(left<=l&&r<=right){
nodeupdate(root,l,r,num);
return;
}
pushdown(root,l,r);
int mid=(l+r)/2;
if(left<=mid)
update(left,right,num,root*2,l,mid);
if(mid<right)
update(left,right,num,root*2+1,mid+1,r);
sum[root]=sum[root*2]+sum[root*2+1];
}
ll Qsum(int left,int right, int root,int l,int r)//区间和
{
if(left<=l&&r<=right)
return sum[root];
pushdown(root,l,r);
int mid=(l+r)/2;
ll res=0;
if(left<=mid)
res+=Qsum(left,right,root*2,l,mid);//区间在左子树
if(right>mid)
res+=Qsum(left,right,root*2+1,mid+1,r);//在右子树
return res;
}

/************** 树链->线段树 *********/
void modify(int u,int v,ll num, int limit) //修改
{
int fu=top[u],fv=top[v];
while(fu!=fv) //uv不在同一条链上
{
if(dep[fu]>=dep[fv]) //u的深度大
{
update(rk[fu],rk[u],num,1,1,limit);
}
else
{
update(rk[fv],rk[v],num,1,1,limit);
}
} //循环结束时，fu-fv在同一链
if(dep[u]>dep[v])swap(u,v);
update(rk[u],rk[v],num,1,1,limit);
}
ll query(int u,int v, int limit) //查询u-v路径
{
ll res=0;
int fu=top[u],fv=top[v];
while(fu!=fv) //uv不在同一条链上
{
if(dep[fu]>=dep[fv]) //u的深度大
{
res+=Qsum(rk[fu],rk[u],1,1,limit);
}
else
{
res+=Qsum(rk[fv],rk[v],1,1,limit);
}
} //循环结束时，fu-fv在同一链
if(dep[u]>dep[v])swap(u,v);
res+=Qsum(rk[u],rk[v],1,1,limit);
return res;
}

/************** solve **************/
int a[MAX];
bool solve()
{
int n,m,P,u,v,k;
if(scanf("%d%d%d",&n,&m,&P)==EOF)return 0;
init(n); //初始图
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
}
cuttree(1); //树链剖分
build(1,1,n); //建立线段树
for(int i=1;i<=n;i++)modify(i,i,a[i],n);
char op[3];
while(P--)
{
scanf("%s",op);
if(op[0]=='Q')
{
scanf("%d",&u);
ll ans=query(u,u,n); //查询
printf("%lld\n",ans);
}
else
{
scanf("%d%d%d",&u,&v,&k);
if(op[0]=='D')k=-k;
modify(u,v,k,n);
}
}
return 1;
}
int main()
{
while(solve());
}


【代码-树状数组】

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX=5e4+5;
const int INF=0x3f3f3f3f;

/*********** 图 ***********/
struct node{
int t,next;
}edge[MAX*2];
void init(int n)
{
}
{
}

/************ 树链剖分 **************/
int top[MAX],rk[MAX],id[MAX];
void dfs1(int u,int pre)
{
siz[u]=1; //u本身
son[u]=-1;
int maxsiz=0; //最大的儿子子树大小
{
int v=edge[i].t;
if(v==pre)continue;
dep[v]=dep[u]+1;
dfs1(v,u); //注意dfs的位置
siz[u]+=siz[v];
if(maxsiz<siz[v])
{
maxsiz=siz[v];
son[u]=v;
}
}
}
void dfs2(int u,int fat,int &tag) //结点u，重链顶端
{
top[u]=fat;
rk[u]=++tag; //时间戳
id[tag]=u; //tag对应在树上的结点号
if(son[u]==-1)return; //没有孩子
dfs2(son[u],fat,tag); //优先重链dfs
{
int v=edge[i].t;
dfs2(v,v,tag);
}
}
void cuttree(int root) //将root为根的树链剖分
{
int tag=0;
dep[root]=0;  //根的深度和父结点做一个初始化
dfs1(root,root);
dfs2(root,root,tag);
}

/************* bit tree ****************/
ll bit[MAX];  //区间增减+单点查询
{
for(;k<=limit;k+=k&-k)
bit[k]+=num;
}
{
ll res=0;
for(;k;k-=k&-k)
res+=bit[k];
return res;
}
void update(int left,int right,ll num,int limit)
{
}

/************** 树链-> bit tree *********/
void modify(int u,int v,ll num,int limit) //修改uv之间都增加num
{
int fu=top[u],fv=top[v];
while(fu!=fv) //uv不在同一条链上
{
if(dep[fu]>=dep[fv]) //u的深度大
{
update(rk[fu],rk[u],num,limit);
}
else
{
update(rk[fv],rk[v],num,limit);
}
} //循环结束时，fu fv在同一链
if(dep[u]>dep[v])swap(u,v);
update(rk[u],rk[v],num,limit);
}

/************** solve **************/
int a[MAX];
bool solve()
{
int n,m,P,u,v,k;
if(scanf("%d%d%d",&n,&m,&P)==EOF)return 0;
init(n); //初始图
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
}
cuttree(1); //树链剖分
for(int i=1;i<=n;i++)bit[i]=0;
for(int i=1;i<=n;i++)update(rk[i],rk[i],a[i],n);
char op[3];
while(P--)
{
scanf("%s",op);
if(op[0]=='Q')
{
scanf("%d",&u);
printf("%lld\n",ans);
}
else
{
scanf("%d%d%d",&u,&v,&k);
if(op[0]=='D')k=-k;
modify(u,v,k,n);
}
}
return 1;
}
int main()
{
while(solve());
}