HDU-3966 树链剖分 树状数组 点权

33 篇文章 0 订阅
21 篇文章 0 订阅

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<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int fa[N];	//每个结点父亲结点
int son[N];	//每个结点的重儿子
int dep[N];	//每个结点的深度
int top[N];	//每个结点所在重链的顶端结点
int siz[N];	//每个结点的子树的结点总数
int pos[N];	//每个结点的父亲到该结点的边在序列中的位置
int fp[N];	//pos逆运算
struct Edge{
	int to,next;
	Edge(){}
	Edge(int to,int next):to(to),next(next){}
}E[N];
int head[N],tot;
inline void addedge(int u,int v){
	E[tot]=Edge(v,head[u]);
	head[u]=tot++;
}
void dfs(int x,int f,int d){
	siz[x]=1,fa[x]=f,dep[x]=d;
	for(int i=head[x];~i;i=E[i].next)if(E[i].to!=f){
		int v=E[i].to;
		dfs(v,x,d+1);
		siz[x]+=siz[v];
		if(son[v]==-1||siz[v]>siz[son[x]])son[x]=v;
	}
}		//求出siz,son,dep
int cnt;	//序列大小
void dfs2(int x,int nowtop){
	pos[x]=++cnt,top[x]=nowtop,fp[pos[x]]=x;
	if(son[x]==-1)return;
	dfs2(son[x],nowtop);//保证重链相邻
	for(int i=head[x];~i;i=E[i].next){
		int v=E[i].to;
		if(v!=fa[x]&&v!=son[x])dfs2(v,v);
	}
}		//求pos,top
int bit[N],n,m,p;
inline int lowbit(int x){return x&-x;}
inline void add(int i,int x){while(i<=n)bit[i]+=x,i+=lowbit(i);}
inline int sum(int i){
	int ans=0;
	while(i)ans+=bit[i],i-=lowbit(i);
	return ans;
}
void init(){
	tot=0,memset(head,-1,sizeof(head));
	cnt=0,memset(son,-1,sizeof(son));
	memset(bit,0,sizeof(bit));
}
void Change(int u,int v,int c){
	int f1=top[u],f2=top[v],tmp=0;
	while(f1^f2){//每次取深度大的点往上爬,否则可能跳过两点的lca
		if(dep[f1]<dep[f2])swap(f1,f2),swap(u,v);
		add(pos[f1],c),add(pos[u]+1,-c);//差分标记
		u=fa[f1],f1=top[u];//爬到该链top的fa
	}//此时两点在同一条链上
	if(dep[u]>dep[v])swap(u,v);
	add(pos[u],c),add(pos[v]+1,-c);
}
int a[N],u,v,k;
char op[3];
int main(){
	while(~scanf("%d%d%d",&n,&m,&p)){
		init();
		for(int i=1;i<=n;++i)scanf("%d",&a[i]);
		while(m--){
			scanf("%d%d",&u,&v);
			addedge(u,v),addedge(v,u);
		}
		dfs(1,0,0);
		dfs2(1,1);
		for(int i=1;i<=n;++i)add(pos[i],a[i]),add(pos[i]+1,-a[i]);
		while(p--){
			scanf("%s",op);
			if(op[0]=='Q')scanf("%d",&u),printf("%d\n",sum(pos[u]));
			else{
				scanf("%d%d%d",&u,&v,&k);
				if(op[0]=='D')k=-k;
				Change(u,v,k);
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值