hdu3966(树链剖分)

Aragorn’s Story

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

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

#include <bits/stdc++.h>
using namespace std;
const int N=50005;
int siz[N],top[N];
int son[N],dep[N];
int pre[N],tid[N];
int ran[N];
int val[N];
vector<int>vec[N];
int id;
void init(int n){
    for(int i=1;i<=n;i++){
        vec[i].clear();
    }
    id=0;
    memset(val,0,sizeof(val));
    memset(son,-1,sizeof(son));
    memset(siz,0,sizeof(siz));
    memset(top,0,sizeof(top));
    memset(dep,0,sizeof(dep));
    memset(pre,-1,sizeof(pre));
    memset(tid,0,sizeof(tid));
    memset(ran,0,sizeof(ran));
}
void dfs_1(int u,int p,int d){
    pre[u]=p;
    dep[u]=d;
    siz[u]=1;
    for(int i=0;i<vec[u].size();i++){
        int x=vec[u][i];
        if(x!=p){
            dfs_1(x,u,d+1);
            siz[u]+=siz[x];
            if(son[u]==-1||siz[x]>siz[son[u]]){
                son[u]=x;
            }
        }
    }
}
void dfs_2(int u,int t){
    top[u]=t;
    tid[u]=(++id);
    ran[id]=u;
    if(son[u]==-1){
        return;
    }
    dfs_2(son[u],t);
    for(int i=0;i<vec[u].size();i++){
        int x=vec[u][i];
        if(x!=son[u] && x!=pre[u]){
            dfs_2(x,x);
        }
    }
}
struct node{
    int l,r;
    int add;
    int sum;
}a[4*N];
void build(int l,int r,int node){
    a[node].l=l;
    a[node].r=r;
    a[node].add=0;
    if(l==r){
        a[node].sum=val[ran[l]];
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,2*node);
    build(mid+1,r,2*node+1);
    a[node].sum=max(a[node*2].sum,a[node*2+1].sum);
}
void update(int l,int r,int node,int value){
    if(a[node].l>=l && a[node].r<=r){
        a[node].sum+=(r-l+1)*value;
        a[node].add+=value;
        return;
    }
    if(a[node].add){
        a[2*node].add+=a[node].add;
        a[2*node+1].add+=a[node].add;
        a[2*node].sum+=a[node].add*(a[2*node].r-a[2*node].l+1);
        a[2*node+1].sum+=
            a[node].add*(a[node*2+1].r-a[2*node+1].l+1);
        a[node].add=0;
    }
    int mid=(a[node].l+a[node].r)/2;
    if(r<=mid){
        update(l,r,2*node,value);
    } else if(l>mid){
        update(l,r,2*node+1,value);
    } else {
        update(l,mid,2*node,value);
        update(mid+1,r,2*node+1,value);
    }
    a[node].sum=max(a[node*2].sum,a[node*2+1].sum);
}
int query(int node,int u){
    if(a[node].l==a[node].r && a[node].l==u){
        return a[node].sum;
    }
    if(a[node].add){
        a[2*node].add+=a[node].add;
        a[2*node+1].add+=a[node].add;
        a[2*node].sum+=a[node].add*(a[2*node].r-a[2*node].l+1);
        a[2*node+1].sum+=
            a[node].add*(a[2*node+1].r-a[2*node+1].l+1);
        a[node].add=0;
    }
    int mid=(a[node].l+a[node].r)/2;
    if(u<=mid){
        a[node].sum=max(a[node*2].sum,a[node*2+1].sum);
        return query(2*node,u);
    } else {
        a[node].sum=max(a[node*2].sum,a[node*2+1].sum);
        return query(2*node+1,u);
    }
}
void solve(int u,int v,int value){
    int fu=top[u];
    int fv=top[v];
    while(fu!=fv){
        if(dep[fu]<dep[fv]){
            swap(fu,fv);
            swap(u,v);
        }
        update(tid[fu],tid[u],1,value);
        u=pre[fu];
        fu=top[u];
    }
    if(dep[u]<dep[v]){
        swap(u,v);
    }
    update(tid[v],tid[u],1,value);
}
int main(){
    int n,m,q;
    while(~scanf("%d %d %d",&n,&m,&q)){
        init(n);
        for(int i=1;i<=n;i++){
            scanf("%d",&val[i]);
        }
        char str[12];
        int u,v,value;
        for(int i=1;i<=m;i++){
            scanf("%d %d",&u,&v);
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
        dfs_1(1,-1,0);
        dfs_2(1,1);
        build(1,n,1);
        while(q--){
            scanf("%s",str);
            if(str[0]=='Q'){
                scanf("%d",&u);
                printf("%d\n",query(1,tid[u]));
            } else {
                scanf("%d %d %d",&u,&v,&value);
                if(str[0]=='D'){
                    value=-value;
                }
                solve(u,v,value);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值