hdu3966 点权模板-树链部分

时间:2021-12-03 17:52:39

Aragorn's Story

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

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
/*
hdu3966 点权模板-树链部分
Q为查询某点情况,I为增加x->y的值,D为减少x->y的值
树链部分--点权修改,树链就相当于把树的边进行hash,然后基于线段树进行修改
hhh-2016-02-02 00:23:11
*/ #include <functional>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <Map>
using namespace std;
typedef long long ll;
typedef long double ld; using namespace std; const int maxn = 50005; struct node
{
int to,next;
} edge[maxn*2];
int head[maxn];
int top[maxn]; //链的顶端节点
int far[maxn]; //父亲
int dep[maxn]; //深度
int num[maxn]; //表示以x为根的子树的节点数
int p[maxn]; //p[u]表示边u所在的位置
int fp[maxn]; //与p相对应
int son[maxn]; //重儿子
int tot,pos;
void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot ++;
} void dfs(int u,int fa,int d) //先处理出重儿子、dep、far、num
{
dep[u] = d;
far[u] = fa;
num[u] = 1;
for(int i = head[u];i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(v != fa)
{
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;
p[u] = pos++;
fp[p[u]] = u;
if(son[u] == -1) return ;
getpos(son[u],sp);
for(int i = head[u];i != -1;i = edge[i].next)
{
int v = edge[i].to;
if(v != far[u] && v != son[u])
getpos(v,v);
}
} int c[maxn],a[maxn];
int n;
int lowbis(int x)
{
return x&(-x);
} int sum(int i)
{
int s = 0;
while(i > 0)
{
s += c[i];
i -= lowbis(i);
}
return s;
} void add(int x,int val)
{
while(x <= n)
{
c[x] += val;
x += lowbis(x);
}
} void ini()
{
tot = 0;
pos = 1;
memset(head,-1,sizeof(head));
memset(son,-1,sizeof(son));
memset(c,0,sizeof(c));
} void change(int u,int v,int k)
{
int f1 = top[u];
int f2 = top[v];
while(f1 != f2)
{
if(dep[f1] < dep[f2])
{
swap(f1,f2);swap(u,v);
}
add(p[f1],k);
add(p[u]+1,-k);
u = far[f1];f1 = top[u];
}
if(dep[u] > dep[v]) swap(u,v);
add(p[u],k);
add(p[v]+1,-k);
} int main()
{
int m,t;
while(scanf("%d%d%d",&n,&m,&t) != EOF)
{
ini();
int u,v,k,x;
char ch[10]; for(int i = 1;i <= n;i++)
scanf("%d",&a[i]);
for(int i = 1;i <= m;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs(1,0,0);
getpos(1,1);
for(int i = 1;i <= n;i++)
{
add(p[i],a[i]);
add(p[i]+1,-a[i]);
}
while(t--)
{
scanf("%s",ch);
if(ch[0] == 'Q')
{
scanf("%d",&x);
printf("%d\n",sum(p[x]));
}
else
{
scanf("%d%d%d",&u,&v,&k);
if(ch[0] == 'D')
k = -k;
change(u,v,k);
}
}
}
return 0;
}