hdoj3966Aragorn's Story【树链刨分】



Aragorn's Story

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


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.
 

题意:给你一些营地和一些营地是相连的现在给出a,b是a,b之间所有连接的营地都增加减少给定数量的兵查询第i个营地的兵数量

树链刨分这道题做的我真难受连续做了三天,刚开始是不输出结果老是死循环,经过漫长的检查终于输出了结果却不对,然后继续改好不容易把测试样例过了兴奋的我马上提交结果TLE了然后用laze标记结果又回到了原点结果一直错好不容易该对了提交wa了然后改依旧不能过最后看着别人的ac代码一点一点改依旧wa最后之后将ac代码一个一个的和我的代码换着提交终于ac了仅仅是一个小小的错误太马虎了。以后一定要小心认真。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=50010;
struct Node{
    int to,v,next;
}A[maxn<<1];
int root,edge,totw,N,M,Q;
int head[maxn],fa[maxn],dep[maxn],pos[maxn],add[maxn<<2];
int son[maxn],top[maxn],tree[maxn<<2],size[maxn],num[maxn];
void pushup(int rt){
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void pushdown(int l, int r, int rt){
    int mid=(l+r)>>1;
    if(add[rt]){
        tree[rt<<1]+=(mid-l+1)*add[rt];
        tree[rt<<1|1]+=(r-mid)*add[rt];
        add[rt<<1]+=add[rt];
        add[rt<<1|1]+=add[rt];
        add[rt]=0;
    }
}
void insert(int a,int b){
    A[edge].to=b;
    A[edge].next=head[a];
    head[a]=edge++;
}
void dfs(int v){
    int k;size[v]=1;son[v]=0;
    for(k=head[v];k!=-1;k=A[k].next){
        if(A[k].to!=fa[v]){
            fa[A[k].to]=v;
            dep[A[k].to]=dep[v]+1;
            dfs(A[k].to);
            size[v]+=size[A[k].to];
            if(size[A[k].to]>size[son[v]])son[v]=A[k].to;
            
        }
    }
}
void buildtree(int v,int tp){
    pos[v]=++totw;top[v]=tp;
    if(son[v])buildtree(son[v],top[v]);
    for(int k=head[v];k!=-1;k=A[k].next){
        if(A[k].to!=son[v]&&A[k].to!=fa[v]){
            buildtree(A[k].to,A[k].to);
        }
    }
}
void update(int rt,int lo,int hi,int lloc,int rloc,int x){
    if(lo>=lloc&&hi<=rloc){
        add[rt]+=x;  
        tree[rt]+=x*(hi-lo+1);
        return;
    } 
    int mid=(lo+hi)>>1;
    pushdown(lo,hi,rt);
    int ls=(rt<<1);int rs=(rt<<1|1);
    if(lloc<=mid)update(ls,lo,mid,lloc,rloc,x);
    if(rloc>mid)update(rs,mid+1,hi,lloc,rloc,x);
    pushup(rt);
}
void change(int va,int vb,int v){
    int f1=top[va],f2=top[vb];  
    while(f1!=f2){
        if(dep[f1]<dep[f2]){
            swap(f1,f2);
            swap(va,vb);
        }
        update(1,1,totw,pos[f1],pos[va],v);
        va=fa[f1];f1=top[va];
    }
    if(dep[va]<dep[vb])swap(va,vb); 
    update(1,1,totw,pos[vb],pos[va],v);
} 
int find(int p,int rt,int lo,int hi){
    if(lo==hi){
        return tree[rt];
    }
    int mid=(lo+hi)>>1;
    pushdown(lo,hi,rt);
    if(p<=mid)
       return find(p,rt<<1,lo,mid);
    else
       return find(p,rt<<1|1,mid+1,hi);
}
int main()
{
    int i,j,k;
    char str[10];
    while(scanf("%d%d%d",&N,&M,&Q)!=EOF){
        for(i=1;i<=N;++i){
            scanf("%d",&num[i]);
        }
        memset(head,-1,sizeof(head));
        memset(size,0,sizeof(size));
        memset(tree,0,sizeof(tree));
        memset(add,0,sizeof(add));
        memset(dep,0,sizeof(dep));
        totw=edge=0;
        int a,b,c;
        for(i=0;i<M;++i){
            scanf("%d%d",&a,&b);
            insert(a,b);
            insert(b,a);
        }
        dfs(1);
        buildtree(1,1);
        for(i=1;i<=N;++i){
            update(1,1,totw,pos[i],pos[i],num[i]);
        } 
        for(i=0;i<Q;++i){
            scanf("%s",str);
            if(str[0]=='I'){
                scanf("%d%d%d",&a,&b,&c);
                change(a,b,c);
            }
            else if(str[0]=='D'){
                scanf("%d%d%d",&a,&b,&c);
                change(a,b,-c);
            }
            else {
                scanf("%d",&a);
                printf("%d\n",find(pos[a],1,1,totw));
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值