HDU-3966 Aragorn's Story(树剖+线段树)

Aragorn's Story

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


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
参考博客:http://blog.csdn.net/jiangshibiao/article/details/24669751

题意:有n个点权值分别为ai,这n个点由m=n-1条边相连构成一个树,现在有k次操作:①I u v x,将从u到v的点权值加x;②D u v x,将从u到v的点权值减x;③Q u ,输出u的点权值

题解:树链剖分模板题,点权

#include<cstdio>
#include<algorithm>
#include<string.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 5;
struct Edge{
    int v,nxt;
}edge[maxn*2];
int top[maxn];//top[v]表示v所在的重链的顶端节点
int f[maxn]; //父亲节点
int deep[maxn];//深度
int num[maxn];//num[v]表示以v为根的子树的节点数
int p[maxn];//p[v]表示v与其父亲节点的连边在线段树中的位置
int son[maxn];//重儿子
int d[maxn],a[maxn],head[maxn],sum[maxn<<2],add[maxn<<2],tot,totp;
void addedge(int u,int v){
    edge[tot].v=v;
    edge[tot].nxt=head[u];
    head[u]=tot++;
}
int dfs1(int u,int fa,int cnt){
    f[u]=fa;
    deep[u]=cnt;
    num[u]=1;
    son[u]=0;
    int mx=0,tmp;
    for(int i=head[u];~i;i=edge[i].nxt){
        int v=edge[i].v;
        if(v==fa) continue;
        tmp=dfs1(v,u,cnt+1);
        if(tmp>mx) mx=tmp,son[u]=v;
        num[u]+=tmp;
    }
    return num[u];
}
void dfs2(int u,int t){
    top[u]=t;
    p[u]=totp++;
    d[p[u]]=a[u];
    if(son[u]) dfs2(son[u],t);
    else return;
    for(int i=head[u];~i;i=edge[i].nxt){
        int v=edge[i].v;
        if(v==f[u]||v==son[u]) continue;
        dfs2(v,v);
    }
}

void prebuild(){
    totp=1;
    dfs1(1,0,1);
    dfs2(1,1);
}
inline void PushUP(int rt){
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
inline void PushDown(int m,int rt){
    if(add[rt]){
        sum[rt<<1]+=add[rt]*(m-(m>>1));
        sum[rt<<1|1]+=add[rt]*(m>>1);
        add[rt<<1]+=add[rt];
        add[rt<<1|1]+=add[rt];
        add[rt]=0;
    }
}
void build(int l,int r,int rt){
    add[rt]=0;
    if(l==r) {
        sum[rt]=d[l];
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUP(rt);
}
void update(int L,int R,int c,int l,int r,int rt){
    if(L<=l&&R>=r){
        sum[rt]+=c*(r-l+1);
        add[rt]+=c;
        return;
    }
    PushDown(r-l+1,rt);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    PushUP(rt);
}
void solve(int u,int v,int x,int n){
    int f1=top[u],f2=top[v];
    while(f1!=f2){
        if(deep[f1]<deep[f2]){
            swap(f1,f2);
            swap(u,v);
        }
        update(p[f1],p[u],x,1,n,1);
        u=f[f1];f1=top[u];
    }
    if(deep[u]>deep[v]) swap(u,v);
    update(p[u],p[v],x,1,n,1);
}
int query(int p,int l,int r,int rt){
    if(l==r) return sum[rt];
    PushDown(r-l+1,rt);
    int m=(l+r)>>1;
    int ret;
    if(p<=m) ret=query(p,lson);
    else ret=query(p,rson);
    PushUP(rt);
    return ret;
}
int main(){
    int m,k,n,u,v,x;
  //  freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&k)){
        tot=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        while(m--){
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        prebuild();
        build(1,n,1);
        char op[2];
        while(k--){
            scanf("%s",op);
            if(op[0]=='I'){
                scanf("%d%d%d",&u,&v,&x);
                solve(u,v,x,n);

            }
            else if(op[0]=='D'){
                scanf("%d%d%d",&u,&v,&x);
                solve(u,v,-x,n);
            }
            else {
                scanf("%d",&u);
                printf("%d\n",query(p[u],1,n,1));
            }
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值