Aragorn’s Story
傳送門1
傳送門2
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
The next line contains
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
‘D’, followed by three integers C1, C2 and
‘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
- The number of enemies may be negative.
- Huge input, be careful.
題意
給一棵樹,並給定各個點權的值,然後有3種操作:
I C1 C2 K: 把C1與C2的路徑上的所有點權值加上K
D C1 C2 K: 把C1與C2的路徑上的所有點權值減去K
Q C: 查詢節點編號爲C的權值
分析
樹鏈剖分+線段樹.
CODE
#include<cstdio>
#include<memory.h>
#define N 50005
inline int max(int x,int y) {return x>y?x:y;}
int n,m,Q;
int head[N],to[N<<1],Next[N<<1],tot;
int dep[N],fa[N],sz[N],top[N],son[N],tot2;
int sgID[N],Rank[N],num[N];
void addedge(int u,int v) {
to[tot]=v,Next[tot]=head[u],head[u]=tot++;
to[tot]=u,Next[tot]=head[v],head[v]=tot++;
}
void dfs1(int x,int f,int d) {
fa[x]=f;
dep[x]=d;
sz[x]=1;
for(int i=head[x]; ~i; i=Next[i]) {
int y=to[i];
if(y!=f) {
dfs1(y,x,d+1);
sz[x]+=sz[y];
if(son[x]==-1||sz[y]>sz[son[x]])
son[x]=y;
}
}
}
void dfs2(int x,int tp) {
top[x]=tp;
sgID[x]=++tot2;
Rank[tot2]=x;
if(son[x]==-1) return;
dfs2(son[x],tp);
for(int i=head[x]; ~i; i=Next[i]) {
int y=to[i];
if(y!=son[x]&&y!=fa[x])
dfs2(y,y);
}
}
struct SegMent {
int sum[N<<2],col[N<<2];
void UP(int p) {
sum[p]=max(sum[p<<1],sum[p<<1|1]);
}
void Down(int p,int m) {
if(col[p]) {
col[p<<1]+=col[p];
col[p<<1|1]+=col[p];
sum[p<<1]+=(m-(m>>1))*col[p];
sum[p<<1|1]+=(m>>1)*col[p];
col[p]=0;
}
}
void Build(int l,int r,int p) {
col[p]=0;
if(l==r) {
sum[p]=num[Rank[l]];
return;
}
int mid=(l+r)>>1;
Build(l,mid,p<<1);
Build(mid+1,r,p<<1|1);
UP(p);
}
void Update(int L,int R,int v,int l,int r,int p) {
if(L<=l&&R>=r) {
col[p]+=v;
sum[p]+=v*(r-l+1);
return;
}
Down(p,r-l+1);
int mid=(l+r)>>1;
if(L<=mid)Update(L,R,v,l,mid,p<<1);
if(R>mid)Update(L,R,v,mid+1,r,p<<1|1);
UP(p);
}
int Query(int l,int r,int p,int val) {
if(l==r)return sum[p];
Down(p,r-l+1);
int mid=(l+r)>>1;
int ret=0;
if(val<=mid)ret=Query(l,mid,p<<1,val);
else ret=Query(mid+1,r,p<<1|1,val);
UP(p);
return ret;
}
} T;
void Change(int x,int y,int val) {
while(top[x]!=top[y]) {
if(dep[top[x]]<dep[top[y]]) {
T.Update(sgID[top[y]],sgID[y],val,1,n,1);
y=fa[top[y]];
} else {
T.Update(sgID[top[x]],sgID[x],val,1,n,1);
x=fa[top[x]];
}
}
if(dep[x]>dep[y])T.Update(sgID[y],sgID[x],val,1,n,1);
else T.Update(sgID[x],sgID[y],val,1,n,1);
}
int main() {
char oper[5];
int a,b,c;
while(~scanf("%d%d%d",&n,&m,&Q)) {
memset(head,-1,sizeof head);
memset(son,-1,sizeof son);
tot2=0;
tot=0;
for(int i=1; i<=n; i++)
scanf("%d",&num[i]);
for(int i=1; i<=m; i++) {
scanf("%d%d",&a,&b);
addedge(a,b);
}
dfs1(1,0,0);
dfs2(1,1);
T.Build(1,n,1);
while(Q--) {
scanf("%s",oper);
if(oper[0]=='Q') {
scanf("%d",&a);
printf("%d\n",T.Query(1,n,1,sgID[a]));
} else {
scanf("%d%d%d",&a,&b,&c);
if(oper[0]=='D') c=-c;
Change(a,b,c);
}
}
}
return 0;
}