HDU-3966 Aragorn's Story (树链剖分 树状数组 区间修改 点查询)(2011 Multi-University Training Contest 13)

Aragorn's Story

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


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.


#pragma comment(linker, "/STACK:1024000000,1024000000")  //手动开栈
#include <bits/stdc++.h>
using namespace std;
#define maxn 50005
int sz[maxn], dep[maxn], son[maxn], top[maxn], pre[maxn], id[maxn], c[maxn], a[maxn], tot;
vector<vector<int> >g(maxn);
void dfs(int x, int fa, int d){
    sz[x] = 1;
    pre[x] = fa;
    dep[x] = d;
    son[x] = 0;
    int cur;
    for(int i = 0; i < g[x].size(); ++i){
        cur = g[x][i];
        if(cur == fa) continue;
        dfs(cur, x, d + 1);
        sz[x] += sz[cur];
        if(sz[son[x]] < sz[cur]){
            son[x] = cur;
        }
    }
}
void dfs1(int x, int tp){
    id[x] = ++tot;
    top[x] = tp;
    if(son[x]) dfs1(son[x], tp);
    int cur;
    for(int i = 0; i < g[x].size(); ++i){
        cur = g[x][i];
        if(cur == pre[x] || cur == son[x]) continue;
        dfs1(cur, cur);
    }
}
inline int lowbit(int x){
    return x & (-x);
}
void add(int x, int v){
    while(x < maxn){
        c[x] += v;
        x += lowbit(x);
    }
}
int query(int x){
    int ans = 0;
    while(x){
        ans += c[x];
        x -= lowbit(x);
    }
    return ans;
}
void update(int x, int y, int v){
    int tp1 = top[x], tp2 = top[y];
    while(tp1 != tp2){
        if(dep[tp1] < dep[tp2]){
            swap(tp1, tp2);
            swap(x, y);
        }
        add(id[tp1], v);
        add(id[x] + 1, -v);
        x = pre[tp1];
        tp1 = top[x];
    }
    if(dep[x] < dep[y]){
        swap(x, y);
    }
    add(id[y], v);
    add(id[x] + 1, -v);
}
int main(){
    int n, m, p, u, v, x;
    while(scanf("%d %d %d", &n, &m, &p) != EOF){
        for(int i = 1; i <= n; ++i){
            g[i].clear();
        }
        memset(c, 0, sizeof(c));
        for(int i = 1; i <= n; ++i){
            scanf("%d", &a[i]);
        }
        for(int i = 1; i <= m; ++i){
            scanf("%d %d", &u, &v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        tot = 0;
        dfs(1, 0, 1);
        dfs1(1, 1);
        for(int i = 1; i <= n; ++i){
            add(id[i], a[i]);
            add(id[i] + 1, -a[i]);
        }
        char s[4];
        for(int i = 1; i <= p; ++i){
            scanf("%s", s);
            if(s[0] == 'Q'){
                scanf("%d", &u);
                printf("%d\n", query(id[u]));
            }
            else{
                scanf("%d %d %d", &u, &v, &x);
                if(s[0] == 'D') x = -x;
                update(u, v, x);
            }
        }   
    }
}

/*
题意:一棵树5e4个结点,每个节点一个权值,1e5次操作,每次操作要么将两点间所有点的值加或减x,
要么查询某个点的值。

思路:
树链剖分的入门题。这题不需要线段树维护,区间修改点查询的操作用树状数组写很方便。对于区间[L, R]
上的所有点都加x,实际上只要在L处加x,R+1处加-x。对于区间[L, R]内的值求前缀和,都多了一个x,对于R后
面的部分都没变。这样点查询求前缀和即该点的值。
注意路径修改时,注意x,tp1的大小关系。 需要手动开栈。
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值