[HDU3966]Aragorn's Story

Time Limit: 10000/3000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)

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 N 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 C 1 C1 C1 and C 2 C2 C2. Then, for C 1 C1 C1, C 2 C2 C2 and all camps on the path from C 1 C1 C1 to C 2 C2 C2, they will increase or decrease K K 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 N, M, P N,M,P which means there will be N ( 1 ≤ N ≤ 50000 ) N(1 ≤ N ≤ 50000) N(1N50000) camps, M ( M = N − 1 ) M(M = N-1) M(M=N1) edges and P ( 1 ≤ P ≤ 100000 ) P(1 ≤ P ≤ 100000) P(1P100000) operations. The number of camps starts from 1 1 1.

The next line contains N N N integers A 1 , A 2 , . . . A N ( 0 ≤ A i ≤ 1000 ) A_1, A_2, ...A_N(0 ≤ A_i ≤ 1000) A1,A2,...AN(0Ai1000), means at first in c a m p − i camp-i campi has A i A_i Ai enemies.

The next M M M lines contains two integers u and v for each, denotes that there is an edge connects c a m p − u camp-u campu and c a m p − v camp-v campv.

The next P lines will start with a capital letter ‘I’, ‘D’ or ‘Q’ for each line.

‘I’, followed by three integers C 1 C1 C1, C 2 C2 C2 and K ( 0 ≤ K ≤ 1000 ) K( 0≤K≤1000) K(0K1000), which means for camp C 1 C1 C1, C 2 C2 C2 and all camps on the path from C 1 C1 C1 to C 2 C2 C2, increase K K K soldiers to these camps.

‘D’, followed by three integers C 1 C1 C1, C 2 C2 C2 and K ( 0 ≤ K ≤ 1000 ) K( 0≤K≤1000) K(0K1000), which means for camp C 1 C1 C1, C 2 C2 C2 and all camps on the path from C 1 C1 C1 to C 2 C2 C2, decrease K K K soldiers to these camps.

‘Q’, followed by one integer C C C, which is a query and means Aragorn wants to know the number of enemies in camp C C 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
2011 Multi-University Training Contest 13 - Host by HIT

题解:
裸的树链剖分+区间修改

#include<bits/stdc++.h>
#define LiangJiaJun main
using namespace std;
int n,m,p;
struct edge{
    int to,nt;
}e[1000004];
struct tree{
    int l,r,tag,w;
}tr[200004];
int ne,cnt,h[50004],a[50004];
int belong[50004],sz[50004],pos[50004],fa[50004],dep[50004];
void add(int u,int v){
     e[++ne].to=v;e[ne].nt=h[u];
     h[u]=ne;
}
void push(int k){
     if(tr[k].tag==0)return ;
     int x=tr[k].tag;tr[k].tag=0;
     tr[k<<1].tag+=x;
     tr[k<<1|1].tag+=x;
     tr[k<<1].w+=x;
     tr[k<<1|1].w+=x;
}
void build(int k,int l,int r){
     tr[k].l=l;tr[k].r=r;
     tr[k].w=0;tr[k].tag=0;
     if(l==r)return;
     int mid=(l+r)>>1;
     build(k<<1,l,mid);
     build(k<<1|1,mid+1,r);
}
void modify(int k,int a,int b,int v){
     int l=tr[k].l,r=tr[k].r;
     if(l==a&&r==b){
        tr[k].tag+=v;
        tr[k].w+=v;
        return ;
     }
     push(k);
     int mid=(l+r)>>1;
     if(b<=mid)modify(k<<1,a,b,v);
     else if(a>mid)modify(k<<1|1,a,b,v);
     else{
        modify(k<<1,a,mid,v);
        modify(k<<1|1,mid+1,b,v);
     }
}
int query(int k,int x){
    int l=tr[k].l,r=tr[k].r;
    if(l==x&&r==x)return tr[k].w;
    push(k);
    int mid=(l+r)>>1;
    if(x<=mid)return query(k<<1,x);
    else return query(k<<1|1,x);
}
void dfs1(int x){
     sz[x]=1;
     for(int i=h[x];i;i=e[i].nt){
         if(e[i].to==fa[x])continue;
         fa[e[i].to]=x;
         dep[e[i].to]=dep[x]+1;
         dfs1(e[i].to);
         sz[x]+=sz[e[i].to];
     }
     return ;
}
void dfs2(int x,int chain){
     pos[x]=++cnt;
     belong[x]=chain;
     int ntc=0;
     for(int i=h[x];i;i=e[i].nt){
         if(e[i].to==fa[x])continue;
         if(sz[ntc]<sz[e[i].to])ntc=e[i].to;
     }
     if(ntc==0)return ;
     dfs2(ntc,chain);
     for(int i=h[x];i;i=e[i].nt){
         if(e[i].to==fa[x])continue;
         if(e[i].to!=ntc)dfs2(e[i].to,e[i].to);
     }
}
void solve(int x,int y,int v){
     while(belong[x]!=belong[y]){
        if(dep[belong[x]]<dep[belong[y]])swap(x,y);
        modify(1,pos[belong[x]],pos[x],v);
        x=fa[belong[x]];
     }
     if(pos[x]>pos[y])swap(x,y);
     modify(1,pos[x],pos[y],v);
     return ;
}
int w33ha(){
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    cnt=0;ne=0;sz[0]=0;
    memset(h,0,sizeof(h));
    for(int i=1;i<=m;i++){
        int u,v;scanf("%d%d",&u,&v);
        add(u,v);add(v,u);
    }
    build(1,1,n);
    dep[1]=1;
    dfs1(1);
    dfs2(1,1);
    for(int i=1;i<=n;i++)modify(1,pos[i],pos[i],a[i]);
    while(p--){
        char s[4];
        int l,r,x;
        scanf("%s",s+1);
        if(s[1]=='I'){
            scanf("%d%d%d",&l,&r,&x);
            solve(l,r,x);
        }
        if(s[1]=='D'){
            scanf("%d%d%d",&l,&r,&x);
            solve(l,r,-x);
        }
        if(s[1]=='Q'){
            scanf("%d",&x);
            printf("%d\n",query(1,pos[x]));
        }
    }
    return 0;
}
int LiangJiaJun(){
    while(scanf("%d%d%d",&n,&m,&p)!=EOF)w33ha();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值