Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5304 Accepted Submission(s): 1387
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.
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 8Hint1.The number of enemies may be negative. 2.Huge input, be careful.
题意:给你一棵树,每次操作让u到v上每个点的值增加或减少给定数目,每次询问某一个点的权值。
思路:用树链刨分去建树,然后内部用树状数组维护即可。另外注意dfs会爆栈,要加上扩栈语句。
AC代码如下:
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node
{
int u,v,next;
}edge[100010];
int T,t,n,m,Head[50010],tot,tot2;
int fa[50010],depth[50010],siz[50010],son[50010],top[50010],p[50010];
ll num[50010],tree[50010];
char s[10];
void add(int u,int v)
{
edge[tot].u=u;
edge[tot].v=v;
edge[tot].next=Head[u];
Head[u]=tot++;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int num)
{
for(;x<=tot2;x+=lowbit(x))
tree[x]+=num;
}
ll query(int x)
{
ll ret=0;
for(;x>0;x-=lowbit(x))
ret+=tree[x];
return ret;
}
void dfs1(int u)
{
int i,j,k,v;
siz[u]=1;
son[u]=0;
for(i=Head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v==fa[u])
continue;
fa[v]=u;
depth[v]=depth[u]+1;
dfs1(v);
if(siz[v]>siz[son[u]])
son[u]=v;
siz[u]+=siz[v];
}
}
void dfs2(int u,int f)
{
int i,j,k,v;
p[u]=++tot2;
top[u]=f;
if(son[u]!=0)
dfs2(son[u],f);
for(i=Head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v==fa[u] || v==son[u])
continue;
dfs2(v,v);
}
}
void solve(int u,int v,ll val)
{
int f1=top[u],f2=top[v],i,j,k;
while(f1!=f2)
{
if(depth[f1]<depth[f2])
{
swap(f1,f2);
swap(u,v);
}
update(p[f1],val);
update(p[u]+1,-val);
u=fa[f1];
f1=top[u];
}
if(depth[u]>depth[v])
swap(u,v);
update(p[u],val);
update(p[v]+1,-val);
}
int main()
{
int i,j,k,u,v,q;
ll val;
while(~scanf("%d%d%d",&n,&m,&q))
{
memset(Head,-1,sizeof(Head));
memset(tree,0,sizeof(tree));
tot=tot2=0;
for(i=1;i<=n;i++)
scanf("%I64d",&num[i]);
for(i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
depth[1]=1;
dfs1(1);
dfs2(1,0);
/*for(i=1;i<=n;i++)
printf("%d ",p[i]);
printf("\n");
printf("tot2=%d\n",tot2);*/
for(i=1;i<=n;i++)
{
update(p[i],num[i]);
update(p[i]+1,-num[i]);
}
for(i=1;i<=q;i++)
{
scanf("%s",s+1);
if(s[1]=='I')
{
scanf("%d%d%I64d",&u,&v,&val);
solve(u,v,val);
}
else if(s[1]=='D')
{
scanf("%d%d%I64d",&u,&v,&val);
solve(u,v,-val);
}
else
{
scanf("%d",&u);
printf("%I64d\n",query(p[u]));
}
}
}
}