Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4189 Accepted Submission(s): 1159
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.
树链剖分是为了解决树上的路径问题,因为我们知道如果暴力从两个点走到他们的LCA,那么将需要O(n)的时间,那么我们想,能否将路径划分成若干段呢,然后按段往LCA走,这样可以降低复杂度,那么这个段应该是多少呢?1段?显然我们考虑的是树上任意两点的信息,一段共用性不强,不可能去维护n^2个段,于是人类开始分解树,所谓树链剖分就是指将一个树划分成若干段,为了区分两种不同段的类型,取了好听的名字:轻边和重链(连续重边连成的一条链),那么为什么没有轻链,因为没必要,我们保证了树上任意一条简单路径都可以拆解成logn条轻边和logn条重链,这样我们维护信息时,按轻边和重链来维护,那么必然需要其他其他数据结构支持,比如BIT和线段树,因为他们有成段更新的特性。
具体剖分过程我给个博客地址,传送门:树链剖分
代码:
加了详细注解,转载请说明出处。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include
#include
#include
#define Maxn 50010
using namespace std;
struct edge{
int to,next;
}p[Maxn<<1];
int head[Maxn];
int tot;
void addedge(int a,int b){
p[tot].to=b;
p[tot].next=head[a];
head[a]=tot++;
}
int n;
int num[Maxn];
int c[Maxn];
void update(int x,int d){
while(x<=n){
c[x]+=d;
x+=x&-x;
}
}
void U(int l,int r,int d){
update(l,d);
update(r+1,-d);
}
int sum(int x){
int res=0;
while(x){
res+=c[x];
x-=x&-x;
}
return res;
}
int fa[Maxn]; //父亲
int son[Maxn]; //重儿子
int sz[Maxn]; //子树节点个数
int dep[Maxn]; //深度
//防止下表越界,请调用dfs1(1,0)
void dfs1(int u,int pre){ //u->[1,n]
sz[u]=1;
fa[u]=pre;
son[u]=0; //初始化为0
dep[u]=dep[pre]+1; //必须保证dep[0]=0
for(int i=head[u];i!=-1;i=p[i].next){
int v=p[i].to;
if(v==pre) continue;
dfs1(v,u);
sz[u]+=sz[v]; //更新sz[u]
if(sz[v]>sz[son[u]]) //必须保证sz[0]=0,这样第一步才能正常更新
son[u]=v; //更新son[u]
}
//叶子节点不做for,因此son[u]=0
}
int tmpdfn;
int top[Maxn]; //重链的靠近树根端,单个点也视为重链
int dfn[Maxn]; //时间戳
void dfs2(int u,int pre){
top[u]=pre;
dfn[u]=++tmpdfn;
if(son[u]) //存在重儿子
dfs2(son[u],pre); //优先搜索重儿子
for(int i=head[u];i!=-1;i=p[i].next){
int v=p[i].to;
if(v!=fa[u]&&v!=son[u]) //搜索轻边
dfs2(v,v); //可能成为重链的top端,父亲是自己
}
}
void init(){
tmpdfn=0;
//sz[0]=dep[0]=0; //全部变量已为0
dfs1(1,0);
dfs2(1,0);
}
void solve(int a,int b,int w){ //a,b为原先的id
while(top[a]!=top[b]){ //a,b在不同的重链上
if(dep[top[a]]dep[b]) swap(a,b); //保证a的深度小(时间戳小)
U(dfn[a],dfn[b],w);
}
int main()
{
char s[2];
int m,a,b,w;
while(~scanf("%d%*d%d",&n,&m)){
for(int i=1;i<=n;i++)
scanf("%d",num+i);
memset(head,-1,sizeof head);
tot=0;
for(int i=1;i