hdu 3966 Aragorn's Story 树链剖分

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 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
树链剖分模版题;
看了acdreamer的;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=2e5+10,M=1e6+10,inf=1e9+10;
const ll INF=1e18+10,mod=2147493647;
struct edge{
    int v,next;
}edge[N<<1];
int head[N<<1],edg,id,n;
int fa[N],dep[N],son[N],siz[N];
int a[N],ran[N],top[N],tid[N];
void init()
{
    memset(son,-1,sizeof(son));
    memset(head,-1,sizeof(head));
    edg=0;
    id=0;
}
void add(int u,int v)
{
    edg++;
    edge[edg].v=v;
    edge[edg].next=head[u];
    head[u]=edg;
}
void dfs1(int u,int fath,int deep)
{
    fa[u]=fath;
    siz[u]=1;
    dep[u]=deep;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==fath)continue;
        dfs1(v,u,deep+1);
        siz[u]+=siz[v];
        if(son[u]==-1||siz[v]>siz[son[u]])
                son[u]=v;
    }
}
void dfs2(int u,int tp)
{
    tid[u]=++id;
    top[u]=tp;
    ran[tid[u]]=u;
    if(son[u]==-1)return;
    dfs2(son[u],tp);
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==fa[u])continue;
        if(v!=son[u])
            dfs2(v,v);
    }
}
int maxx[N<<2],lazy[N<<2];
void pushup(int pos)
{
    maxx[pos]=max(maxx[pos<<1],maxx[pos<<1|1]);
}
void pushdown(int pos)
{
    if(lazy[pos])
    {
        lazy[pos<<1]+=lazy[pos];
        lazy[pos<<1|1]+=lazy[pos];
        maxx[pos<<1]+=lazy[pos];
        maxx[pos<<1|1]+=lazy[pos];
        lazy[pos]=0;
    }
}
void build(int l,int r,int pos)
{
    lazy[pos]=0;
    if(l==r)
    {
        maxx[pos]=a[ran[l]];
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,pos<<1);
    build(mid+1,r,pos<<1|1);
    pushup(pos);
}
void update(int L,int R,int c,int l,int r,int pos)
{
    if(L<=l&&r<=R)
    {
        maxx[pos]+=c;
        lazy[pos]+=c;
        return;
    }
    pushdown(pos);
    int mid=(l+r)>>1;
    if(L<=mid)
        update(L,R,c,l,mid,pos<<1);
    if(R>mid)
        update(L,R,c,mid+1,r,pos<<1|1);
    pushup(pos);
}
int query(int p,int l,int r,int pos)
{
    if(l==r)return maxx[pos];
    pushdown(pos);
    int mid=(l+r)>>1;
    if(p<=mid)
        return query(p,l,mid,pos<<1);
    else
        return query(p,mid+1,r,pos<<1|1);
}
void up(int l,int r,int x)
{
    while(top[l]!=top[r])
    {
        if(dep[top[l]]<dep[top[r]])swap(l,r);
        update(tid[top[l]],tid[l],x,1,n,1);
        l=fa[top[l]];
    }
    if(dep[l]<dep[r])swap(l,r);
    //cout<<tid[r]<<" "<<tid[l]<<" "<<x<<endl;
    update(tid[r],tid[l],x,1,n,1);
}
char s[10];
int main()
{
    int m,q;
    while(~scanf("%d%d%d",&n,&m,&q))
    {
        init();
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        dfs1(1,-1,1);
        dfs2(1,1);
        build(1,n,1);
        while(q--)
        {
            scanf("%s",s);
            if(s[0]=='Q')
            {
                int x;
                scanf("%d",&x);
                printf("%d\n",query(tid[x],1,n,1));
            }
            else
            {
                int l,r,x;
                scanf("%d%d%d",&l,&r,&x);
                if(s[0]=='D')
                    x=-x;
                up(l,r,x);
            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jhz033/p/6351829.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值