HDU3966:Aragorn's Story(树链剖分)

本文探讨了树状数据结构中的一种高效算法——树剖,通过重链跳转和差分标志的维护,实现了对树上路径操作和查询的快速响应。文章详细介绍了算法的实现过程,包括初始化树结构、进行DFS遍历确定父子关系和深度、计算子树大小等关键步骤,并通过一个具体的编程示例展示了如何使用树剖解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Aragorn's Story

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


 

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

题意:给一棵树,两种操作,路径上的所有点的点权加或减一个数,查询某个节点的点权。

思路:树剖入门题,重链跳log次,用BIT维护重链上的差分标志就行。

# include <bits/stdc++.h>
using namespace std;
const int maxn = 5e4+30;
vector<int>g[maxn];
int siz[maxn], son[maxn], fa[maxn], dep[maxn], top[maxn];
int cnt, id[maxn], rnk[maxn], sum[maxn], a[maxn];
int cal(int pos){
    int res = 0;
    for(int i=pos; i; i-=i&-i)
        res += sum[i];
    return res;
}
void up(int pos, int val){
    for(int i=pos; i<=cnt; i+=i&-i)
        sum[i] += val;
}
void dfs1(int cur, int pre){
    siz[cur] = 1;
    for(int to:g[cur]){
        if(to == pre) continue;
        fa[to] = cur;
        dep[to] = dep[cur] + 1;
        dfs1(to, cur);
        siz[cur] += siz[to];
        if(siz[to] > siz[son[cur]]) son[cur] = to;
    }
}
void dfs2(int cur, int pre){
    top[cur] = pre;
    id[cur] = ++cnt;
    rnk[cnt] = cur;
    if(son[cur]) dfs2(son[cur], pre);
    for(int to:g[cur])
        if(to != fa[cur] && to != son[cur])
            dfs2(to, to);
}
void update(int x, int y, int z){
    while(top[x] != top[y]){
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        up(id[top[x]], z);
        up(id[x]+1, -z);
        x = fa[top[x]];
    }
    if(dep[x] > dep[y]) swap(x, y);
    up(id[x], z);
    up(id[y]+1, -z);
}
int main(){
    int n, m, p, u, v, w;
    char s[5];
    while(~scanf("%d%d%d",&n,&m,&p)){
        cnt = 0;
        for(int i=0; i<=n; ++i){
            g[i].clear();
            fa[i] = dep[i] = son[i] = top[i] = siz[i] = 0;
            id[i] = rnk[i] = sum[i] = 0;
        }
        for(int i=1; i<=n; ++i) scanf("%d",&a[i]);
        for(int i=0; i<m; ++i){
            scanf("%d%d",&u,&v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        dfs1(1,0);
        dfs2(1,1);
        while(p--){
            scanf("%s",s);
            if(s[0] == 'I'){
                scanf("%d%d%d",&u,&v,&w);
                update(u,v,w);
            }
            else if(s[0] == 'D'){
                scanf("%d%d%d",&u,&v,&w);
                update(u,v,-w);
            }
            else{
                scanf("%d",&u);
                printf("%d\n",cal(id[u])+a[u]);
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值