HDU 3966 Aragorn's Story (树链剖分 区间更新,点查询)

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.
树状数组有一个骚操作。。查询时logn查询单点,然后logn区间更新。。具体操作看代码。。
也是树链剖分的基础题吧,这个树状数组用的真的骚(学的别人的操作
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 50050, INF = 0x3f3f3f3f;
int fa[maxn], son[maxn], son_num[maxn], deep[maxn], cloc;
int fpos[maxn], pos[maxn], top[maxn];
int a[maxn];
int n, m, q;
struct Edge {
    int v, next;
}edge[maxn * 2];
int head[maxn], total;
int tree[maxn * 2];

inline void init() {
    memset(son, -1, sizeof(son));
    memset(head, -1, sizeof(head));
    memset(tree, 0, sizeof(tree));
    total = 0;
    cloc = 0;
}
inline void add(int u, int v) {
    edge[total] = {v, head[u]};
    head[u] = total++;
}

void dfs(int u, int pre, int de) {
    son_num[u] = 1;
    fa[u] = pre;
    deep[u] = de;
    for (int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].v;
        if (v != pre) {
            dfs(v, u, de + 1);
            son_num[u] += son_num[v];
            if (son[u] == -1 || son_num[son[u]] < son_num[v]) {
                son[u] = v;
            }
        }
    }
}

void get_pos(int u, int sp) {
    pos[u] = ++cloc;
    ::fpos[pos[u]] = u;
    top[u] = sp;
    if (son[u] == -1)
        return;
    get_pos(son[u], sp);
    for (int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].v;
        if (v != son[u] && v != fa[u])
            get_pos(v, v);
    }
}


int low_bit(int x) {
    return x & (-x);
}

void update(int pos, int val) {
    for (int i = pos; i <= maxn * 2; i += low_bit(i)) {
        tree[i] += val;
    }
}

int query(int pos) {
    int sum = 0;
    for (int i = pos; i >= 1; i -= low_bit(i)) {
        sum += tree[i];
    }
    return sum;
}

void change(int u, int v, int val) {
    int fu = top[u], fv = top[v];
    while (fu != fv) {
        if (deep[fu] < deep[fv]) {
            swap(fu, fv);
            swap(u, v);
        }
        update(pos[fu], val);
        update(pos[u] + 1, -val);
        u = fa[fu];
        fu = top[u];
    }
    if (deep[u] > deep[v])
        swap(u, v);
    update(pos[u], val);
    update(pos[v] + 1, -val);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
        //freopen("in.txt", "r", stdin);
    while (cin >> n >> m >> q) {
        init();
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        for (int i = 1; i <= m; ++i) {
            int u, v;
            cin >> u >> v;
            add(u, v);
            add(v, u);
        }
        dfs(1, -1, 1);
        get_pos(1, 1);
        for (int i = 1; i <= n; ++i) {
            update(pos[i], a[i]);
            update(pos[i] + 1, -a[i]);
        }
        
        while (q--) {
            char ch[10];
            cin >> ch;
            if (ch[0] == 'Q') {
                int index;
                cin >> index;
                cout << query(pos[index]) << endl;
            } else {
                int u, v, w;
                cin >> u >> v >> w;
                if (ch[0] == 'D')
                    w = -w;
                change(u, v, w);
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值