Aragorn’s Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11761 Accepted Submission(s): 3122
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
题解:
显然这道题就是裸的树链剖分,本来是为了复习一下链剖的,我用的是线段树维护,剖出来修改查询就行了,然而Hdu好坑啊,我debug了好久,原来是不能define一个MAXN来赋数组,改成const就A了,啊啊啊好坑啊,伤心,三个小时没了
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
const int MAXN=50000+50;
using namespace std;
int head[MAXN],tail;
int sum[MAXN*4],add[MAXN*4];
int num[MAXN],size[MAXN],top[MAXN],son[MAXN],dep[MAXN],tid[MAXN],rankk[MAXN],fa[MAXN];
int timer,n,m,P;
struct Line{
int to,nxt;
}line[MAXN*2];
int readin(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void pushup(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void init(){
memset(son,-1,sizeof(son));
memset(head,0,sizeof(head));
timer=tail=0;
}
void dfs2(int u,int tp){
tid[u]=++timer;
rankk[tid[u]]=u;
top[u]=tp;
if(son[u]==-1) return;
dfs2(son[u],tp);
for(register int i=head[u];i;i=line[i].nxt){
int v=line[i].to;
if(son[u]!=v&&v!=fa[u]) dfs2(v,v);
}
}
void dfs1(int u,int father,int d){
fa[u]=father;
dep[u]=d;
size[u]=1;
for(register int i=head[u];i;i=line[i].nxt){
int v=line[i].to;
if(v!=father){
dfs1(v,u,d+1);
size[u]+=size[v];
if(son[u]==-1||size[son[u]]<size[v]) son[u]=v;
}
}
}
void add_line(int from,int to){
tail++;
line[tail].to=to;
line[tail].nxt=head[from];
head[from]=tail;
}
void build(int l,int r,int rt){
add[rt]=0;
if(l==r){
sum[rt]=num[rankk[l]];
return;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
void pushdown(int rt,int len){
if(add[rt]){
add[rt<<1]+=add[rt];
add[rt<<1|1]+=add[rt];
sum[rt<<1]+=(len-(len>>1))*add[rt];
sum[rt<<1|1]+=(len>>1)*add[rt];
add[rt]=0;
}
}
void modify(int L,int R,int value,int l,int r,int rt){
if(L<=l&&R>=r){
add[rt]+=value;
sum[rt]+=value*(r-l+1);
return;
}
pushdown(rt,r-l+1);
int mid=(l+r)>>1;
if(L<=mid) modify(L,R,value,l,mid,rt<<1);
if(R>mid) modify(L,R,value,mid+1,r,rt<<1|1);
pushup(rt);
}
void update(int from,int to,int value){
while(top[from]!=top[to]){
if(dep[top[from]]<dep[top[to]]) swap(from,to);
modify(tid[top[from]],tid[from],value,1,n,1);
from=fa[top[from]];
}
if(dep[from]>dep[to]) swap(from,to);
modify(tid[from],tid[to],value,1,n,1);
}
int query(int l,int r,int rt,int loc){
if(l==r) return sum[rt];
pushdown(rt,r-l+1);
int mid=(l+r)>>1;
int temp;
if(loc<=mid) temp=query(l,mid,rt<<1,loc);
else temp=query(mid+1,r,rt<<1|1,loc);
pushup(rt);
return temp;
}
int main(){
while(~scanf("%d%d%d",&n,&m,&P)){
init();
int a,b,from,to,ad,te;
char temp[10];
for(register int i=1;i<=n;i++) scanf("%d",&num[i]);
for(register int i=1;i<=m;i++){
scanf("%d%d",&a,&b);
add_line(a,b);add_line(b,a);
}
dfs1(1,0,0);
dfs2(1,1);
build(1,n,1);
while(P--){
scanf("%s",&temp);
if(temp[0]=='Q'){
scanf("%d",&te);
printf("%d\n",query(1,n,1,tid[te]));
}else{
scanf("%d%d%d",&from,&to,&ad);
if(temp[0]=='D')ad=-ad;
update(from,to,ad);
}
}
}
return 0;
}