树链剖分--Aragorn‘s Story--HDU3966

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

树链剖分就是树结构线性化。将一颗树划分为一系列链,对于树上任意两点间路径节点的操作可以分治为若干条链的操作,而这每条链的dfs序是连续的,故可以用线段树等结构来维护,之所以要用重链来划分,是因为这样可以将划分次数将到最低,具体证明

#define mid ((l+r)>>1)若是没有外面的括号就会一直RE

Code

#include <iostream>
#include <stdio.h>
#include <vector>
#define lson o<<1
#define rson o<<1|1
#define lnode o<<1,l,(l+r)>>1
#define rnode o<<1|1,((l+r)>>1)+1,r
#define mid ((l+r)>>1)
#define rt 1,1,n
using namespace std;

const int MAXN=5e4+500;
vector<int> e[MAXN];
int size[MAXN],dep[MAXN]={0},fa[MAXN],son[MAXN],top[MAXN],inx[MAXN],rev[MAXN],num[MAXN];

int a[MAXN<<2],lazy[MAXN<<2];
inline void pushup(int o){
    a[o]=a[lson]+a[rson];
}

inline void pushdown(int o,int len){
    if(lazy[o]){
        lazy[lson]+=lazy[o];a[lson]+=(len+1)/2*lazy[o];
        lazy[rson]+=lazy[o];a[rson]+=len/2*lazy[o];
        lazy[o]=0;
    }
}

void build(int o,int l,int r){
    lazy[o]=0;
    if(l==r){
        a[o]=num[rev[l]];
        return;
    }
    build(lnode);build(rnode);
    pushup(o);
}

void update(int o,int l,int r,int i,int j,int k){
    if(l==i&&r==j){
        lazy[o]+=k;
        a[o]+=k*(r-l+1);
        return;
    }
    pushdown(o,r-l+1);
    if(j<=mid) update(lnode,i,j,k);
    else if(i>mid) update(rnode,i,j,k);
    else{
        update(lnode,i,mid,k);update(rnode,mid+1,j,k);
    }
    pushup(o);
}

int query(int o,int l,int r,int i){
    if(l==r) return a[o];
    pushdown(o,r-l+1);
    if(i<=mid) return query(lnode,i);
    else return query(rnode,i);
}

//树链剖分准备,计算size,dep,fa,son数组
void dfs1(int u,int pre){
    fa[u]=pre;
    dep[u]=dep[pre]+1;
    son[u]=0;
    size[u]=1;
    for(int i=0;i<e[u].size();i++){
        int v=e[u][i];
        if(v==pre) continue;
        dfs1(v,u);
        size[u]+=size[v];
        if(son[u]==0||size[son[u]]<size[v]) son[u]=v;
    }
}

//开始剖分,计算top,inx,rev数组
int seq=1;
void dfs2(int u){
    inx[u]=seq;
    rev[seq++]=u;
    if(son[fa[u]]==u) top[u]=top[fa[u]];
    else top[u]=u;
    if(!son[u]) return;
    dfs2(son[u]);
    for(int i=0;i<e[u].size();i++){
        int v=e[u][i];
        if(v==fa[u]||v==son[u]) continue;
        dfs2(v);
    }
}

int n,m,p;
void data_input(){
    for(int i=1;i<=n;i++){
        scanf("%d",num+i);
        e[i].clear();
    }
    while(m--){
        int u,v;scanf("%d %d",&u,&v);
        e[u].push_back(v);
        e[v].push_back(u);
    }
}

void solve(){
    dfs1(1,0);
    seq=1;
    dfs2(1);
    build(rt);
    while(p--){
        char op[2];scanf("%s",op);
        if(op[0]=='Q'){
            int u;scanf("%d",&u);
            printf("%d\n",query(rt,inx[u]));
        }
        else{
            int u,v,k;scanf("%d %d %d",&u,&v,&k);
            if(op[0]=='D') k=-k;
            while(top[u]!=top[v]){
                if(dep[top[u]]<dep[top[v]]) swap(u,v);
                update(rt,inx[top[u]],inx[u],k);
                u=fa[top[u]];
            }
            if(dep[u]>dep[v]) swap(u,v);
            update(rt,inx[u],inx[v],k);
        }
    }
}

int main(){
    //freopen("/Users/guoyu/Desktop/algorithm/in.txt","r",stdin);
    while(~scanf("%d %d %d",&n,&m,&p)){
        data_input();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值