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): 12638    Accepted Submission(s): 3392

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
 
Recommend
We have carefully selected several similar problems for you:   3964  3965  3962  3963  3967 
思路:树链剖分板子。
写代码时出现的错误:
1.查询时  return 漏下了
2.没注意“Multiple test cases, process to the end of input.”多组数据输入。
3.top[t]=top[x]写成了top[x]=top[t];
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 500000
using namespace std;
int n,m,p,sz,tot;
int w[MAXN];
int to[MAXN],head[MAXN],net[MAXN];
int id[MAXN],top[MAXN],dad[MAXN],size[MAXN],deep[MAXN];
struct nond{
    int l,r;
    int sum,flag;
}tree[MAXN];
void add(int u,int v){
    to[++tot]=v;net[tot]=head[u];head[u]=tot;
    to[++tot]=u;net[tot]=head[v];head[v]=tot;
}
void up(int now){
    tree[now].sum=tree[now*2].sum+tree[now*2+1].sum;
}
void build(int now,int l,int r){
    tree[now].l=l;
    tree[now].r=r;
    if(tree[now].l==tree[now].r)
        return ;
    int mid=(tree[now].l+tree[now].r)/2;
    build(now*2,l,mid);
    build(now*2+1,mid+1,r);
}
void down(int now){
    tree[now*2].flag+=tree[now].flag;
    tree[now*2+1].flag+=tree[now].flag;
    tree[now*2].sum+=tree[now].flag*(tree[now*2].r-tree[now*2].l+1);
    tree[now*2+1].sum+=tree[now].flag*(tree[now*2+1].r-tree[now*2+1].l+1);
    tree[now].flag=0;
}
void change(int now,int l,int r,int k){
    if(tree[now].l==l&&tree[now].r==r){
        tree[now].sum+=k*(tree[now].r-tree[now].l+1);
        tree[now].flag+=k;
        return ;
    }
    if(tree[now].flag)    down(now);
    int mid=(tree[now].l+tree[now].r)/2;
    if(r<=mid)    change(now*2,l,r,k);
    else if(l>mid)    change(now*2+1,l,r,k);
    else{
        change(now*2,l,mid,k);
        change(now*2+1,mid+1,r,k);
    }
    up(now);
}
int query(int now,int x){
    if(tree[now].l==tree[now].r)
        return tree[now].sum;
    if(tree[now].flag)    down(now);
    int mid=(tree[now].l+tree[now].r)/2;
    if(x<=mid)    return query(now*2,x);
    else if(x>mid)    return query(now*2+1,x);
}
void dfs(int x){
    size[x]=1;
    deep[x]=deep[dad[x]]+1;
    for(int i=head[x];i;i=net[i])
        if(dad[x]!=to[i]){
            dad[to[i]]=x;
            dfs(to[i]);
            size[x]+=size[to[i]];
        }
}
void dfs1(int x){
    int t=0;
    id[x]=++sz;
    if(!top[x])    top[x]=x;
    change(1,sz,sz,w[x]);
    for(int i=head[x];i;i=net[i])
        if(dad[x]!=to[i]&&size[t]<size[to[i]])
            t=to[i];
    if(t){
        top[t]=top[x];
        dfs1(t);
    }
    for(int i=head[x];i;i=net[i])
        if(dad[x]!=to[i]&&t!=to[i])
            dfs1(to[i]);
}
void schange(int x,int y,int z){
    while(top[x]!=top[y]){
        if(deep[top[x]]<deep[top[y]])    swap(x,y);
        change(1,id[top[x]],id[x],z);
        x=dad[top[x]];
    }
    if(id[x]>id[y])    swap(x,y);
    change(1,id[x],id[y],z);
}
int main(){
    while(scanf("%d%d%d",&n,&m,&p)!=EOF){
        tot=0;sz=0;
        memset(w,0,sizeof(w));
        memset(id,0,sizeof(id));
        memset(to,0,sizeof(to));
        memset(dad,0,sizeof(dad));
        memset(net,0,sizeof(net));
        memset(top,0,sizeof(top));
        memset(deep,0,sizeof(deep));
        memset(head,0,sizeof(head));
        memset(size,0,sizeof(size));
        memset(tree,0,sizeof(tree));
        for(int i=1;i<=n;i++)
            cin>>w[i];
        for(int i=1;i<=m;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        build(1,1,n);
        dfs(1);
        dfs1(1);
        for(int i=1;i<=p;i++){
            char opt[10];int x,y,z;
            scanf("%s%d",opt,&x);
            if(opt[0]=='I'){
                scanf("%d%d",&y,&z);
                schange(x,y,z);
            } else if(opt[0]=='D'){
                scanf("%d%d",&y,&z);
                schange(x,y,-z);
            } 
            else    printf("%d\n",query(1,id[x]));
        }
    }
}

 

转载于:https://www.cnblogs.com/cangT-Tlan/p/7404881.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值