HDOJ 题目3966 Aragorn's Story(树链剖分,树状数组)

Aragorn's Story

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


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
 

Recommend
We have carefully selected several similar problems for you:   3965  3962  3963  3967  3968 
 比动态树好写多了,,只记得这道题,写动态树的时候蛋疼了好久。。。
ac代码
150835092015-10-12 14:39:46Accepted39661123MS10176K2349 BG++Who_you?

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define max(a,b) (a>b?a:b)
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define N 50010
int head[N],cnt,val[N];
int top[N],p[N],fa[N],deep[N],son[N],fp[N],num[N];
int pos;
struct s
{
	int u,v,next;
}edge[N<<1];
void add(int u,int v)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
void init()
{
	memset(head,-1,sizeof(head));
	memset(son,-1,sizeof(son));
	pos=0;
	cnt=0;
}
void dfs(int u,int pre,int d)
{
	deep[u]=d;
	fa[u]=pre;
	num[u]=1;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		if(v==pre)
			continue;
		dfs(v,u,d+1);
		num[u]+=num[v];
		if(son[u]==-1||num[v]>num[son[u]])
			son[u]=v;
	}
}
void getpos(int u,int sp)
{
	top[u]=sp;
	if(son[u]!=-1)
	{
		p[u]=++pos;
		fp[p[u]]=u;
		getpos(son[u],sp);
	}
	else
	{
		p[u]=++pos;
		fp[p[u]]=u;
		return;
	}
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		if(v!=son[u]&&v!=fa[u])
			getpos(v,v);
	}
}
int res[N];
int lowbit(int x)
{
	return x&(-x);
}
void update(int x,int q)
{
	while(x>0)
	{
		res[x]+=q;
		x-=lowbit(x);
	}
}
int sum(int x)
{
	int ans=0;
	while(x<=pos)
	{
		ans+=res[x];
		x+=lowbit(x);
	}
	return ans;
}
void change(int u,int v,int w)
{
	int f1=top[u];
	int f2=top[v];
	int temp=0;
	while(f1!=f2)
	{
		if(deep[f1]<deep[f2])
		{
			swap(f1,f2);
			swap(u,v);
		}
		update(p[u],w);
		update(p[f1]-1,-w);
		u=fa[f1];
		f1=top[u];
	}
//	if(u==v)
//		return;
	if(deep[u]>deep[v])
	{
		swap(u,v);
	}
	update(p[v],w);
	update(p[u]-1,-w);
}
int main()
{
	int n,m,q;
	while(scanf("%d%d%d",&n,&m,&q)!=EOF)
	{
		int i;
		init();
		memset(res,0,sizeof(res));
		for(i=1;i<=n;i++)
			scanf("%d",&val[i]);
		for(i=0;i<m;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			add(a,b);
			add(b,a);
		}
		dfs(1,0,0);
		getpos(1,1);
		for(i=1;i<=n;i++)
		{
			update(p[i],val[i]);
			update(p[i]-1,-val[i]);
		}
		while(q--)
		{
			char op[2];
			scanf("%s",op);
			if(op[0]=='Q')
			{
				int x;
				scanf("%d",&x);
				printf("%d\n",sum(p[x]));
			}
			else
			{
				int a,b,c;
				scanf("%d%d%d",&a,&b,&c);
				if(op[0]=='D')
					c=-c;
				change(a,b,c);
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值