您的位置:首页 > 编程语言 > Go语言

HDU 3966 Aragorn's Story (树链剖分)

2017-02-13 01:50 375 查看
大体题意:

给你一棵树,给你三种操作,u到v 结点之间的结点 加上K,减去k ,查询结点u的权值?

思路:

数据比较大,肯定不能直接暴力。

通过这个题目学习到了树链剖分(可以百度一下)。

这个题目用树状数组就足够了。

简单记录一下 树链剖分。

第一个dfs  记录每个结点深度和 重儿子(子孙最多的儿子)。

第二个dfs 就开始把重边化成重链了。并且重新编号(因为树状数组和线段树只能查询连续的区间查询)。

最后修改的话,只需要让顶端点 v 加上k 和尾端点u  减去v。  可以画个图 很巧妙的方式用树状数组。

这个题算是裸树链剖分了。

详细见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define ps push_back
#define Siz(x) (int)x.size()
using namespace std;

const int maxn = 50000 + 7;
vector<int>g[maxn];
int deep[maxn], p[maxn], a[maxn],c[maxn], son[maxn], num[maxn], fa[maxn], top[maxn];
int n, m, q, cnt;
char cmd[10];
int lowbit(int x){
return x&-x;
}
void add(int i,int o){
while(i <= n){
c[i] += o;
i += lowbit(i);
}
}
int sum(int i){
int ans = 0;
while(i){
ans += c[i];
i -= lowbit(i);
}
return ans;
}
void dfs(int cur,int pre,int dep){
deep[cur] = dep;
if (Siz(g[cur]) == 0){
num[cur] = 1;
return;
}
for (int i = 0; i < Siz(g[cur]); ++i){
int v = g[cur][i];
if (v != pre){
dfs(v,cur,dep+1);
fa[v] = cur;
if (son[cur] == -1 || num[v] > num[cur]){
son[cur] = v;
num[cur] = num[v] + 1;
}
}
}
}
void link(int cur,int o){
p[cur] = cnt++;
top[cur] = o;
if (son[cur] == -1) return;
link(son[cur],o);
for (int i = 0; i < Siz(g[cur]); ++i){
int v = g[cur][i];
if (v != fa[cur] && v != son[cur]) link(v, v);
}
}
void deal(int u,int v,int val){
int t1 = top[u], t2 = top[v];
while(t1 != t2){
if (deep[t1] < deep[t2]){
swap(t1, t2);
swap(u,v);
}
add(p[t1],val);
add(p[u] + 1,-val);
u = fa[t1];
t1 = top[u];
}
if (deep[u] > deep[v]) {
swap(u,v);
}
add(p[u],val);
add(p[v] + 1,-val);
}
void init(){
cnt = 1;
memset(c,0,sizeof c);
memset(son,-1,sizeof son);
memset(num,0,sizeof num);
for (int i = 1; i <= n; ++i) g[i].clear();
}
int main(){
while(~scanf("%d %d %d",&n, &m, &q)){
init();
for (int i = 1; i <= n; ++i){
scanf("%d",a+i);
}
for (int i = 0; i < m; ++i){
int u,v;
scanf("%d %d",&u, &v);
g[u].ps(v);
g[v].ps(u);
}
dfs(1,-1,0);
link(1,1);
for (int i = 1; i <= n; ++i){
add(p[i], a[i]);
add(p[i]+1, -a[i]);
}

while(q--){
scanf("%s",cmd);
if (cmd[0] == 'Q'){
int u;
scanf("%d",&u);
printf("%d\n",sum(p[u]));
}
else {
int u,v,val;
scanf("%d %d %d",&u, &v, &val);
if (cmd[0] == 'D') val = -val;
deal(u,v,val);
}
}
}
return 0;
}


Aragorn's Story

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 10613    Accepted Submission(s): 2780


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 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: