hdu 3966(树链剖分)

Aragorn's Story

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


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.
 


 //树链剖分

<span style="font-size:18px;">#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <bits/stdc++.h>
using namespace std;
const int N=5e5+5;

int num[N],index,ans;//点值 线段树下标 深度

vector<int>G[N];
/*--------------树链剖分部分--------------------*/

int Sz[N],dth[N],son[N],top[N],fa[N],lgt[N],flgt[N];
/*
    Sz[i] 以i节点为根的子树节点数
    dth[i] 节点i的深度
    sun[i] 节点i的重儿子
    top[i] i所在链顶端的节点
    fa[i] i节点的父亲
    lgt[i] i节点对应在线段树的编号
    flgt[i] 编号为i的对应节点
*/

void dfs1(int u,int f)
{
    int tol=1;
    for(auto it:G[u])
    {
        if(it!=f)
        {
            dth[it]=dth[u]+1,fa[it]=u;
            dfs1(it,u);
            tol+=Sz[it];
            if(Sz[son[u]]<Sz[it])son[u]=it;
        }
    }
    Sz[u]=tol;
}

void dfs2(int u,int tp)
{
    lgt[u]=++index;
    flgt[lgt[u]]=u;
    top[u]=tp;
    if(son[u])dfs2(son[u],top[u]);
    for(auto it:G[u])
        if(it!=fa[u]&&it!=son[u])dfs2(it,it);
}
/*-----------线段树部分--------------*/
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r

struct Node
{
    int l,r,val,mark;
}seg[N*4];

void pushdown(int rt)
{
    seg[2*rt].val+=seg[rt].mark,seg[2*rt+1].val+=seg[rt].mark;
    seg[2*rt].mark+=seg[rt].mark,seg[2*rt+1].mark+=seg[rt].mark;
    seg[rt].mark=0;
}

void build(int rt,int l,int r)
{
    seg[rt].l=l,seg[rt].r=r,seg[rt].mark=0;
    int mid=(l+r)>>1;
    if(l==r)seg[rt].val=num[flgt[l]];
    else build(lson),build(rson);
}

void update(int rt,int l,int r,int k)
{
    if(seg[rt].l==l&&seg[rt].r==r)
    {
        seg[rt].val+=k,seg[rt].mark+=k;
        return;
    }
    pushdown(rt);
    int mid=(seg[rt].l+seg[rt].r)>>1;
    if(r<=mid)update(2*rt,l,r,k);
    else if(l>mid)update(2*rt+1,l,r,k);
    else update(lson,k),update(rson,k);
}

void query(int rt,int k)
{
    if(seg[rt].l==seg[rt].r&&seg[rt].l==k)
    {
        ans=seg[rt].val;
        return;
    }
    pushdown(rt);
    int mid=(seg[rt].l+seg[rt].r)>>1;
    if(k<=mid)query(2*rt,k);
    else if(k>mid)query(2*rt+1,k);
}

void change(int u,int v,int k)
{
    int tp1=top[u],tp2=top[v];
    while(tp1!=tp2)
    {
        if(dth[tp1]<dth[tp2])
            swap(tp1,tp2),swap(u,v);
        update(1,lgt[tp1],lgt[u],k);
        u=fa[tp1],tp1=top[u];
    }
    if(dth[u]>dth[v])swap(u,v);
    update(1,lgt[u],lgt[v],k);
}

int main()
{
    int n,m,p,u,v;
    while(~scanf("%d%d%d",&n,&m,&p))
    {
        for(int i=0;i<=n;i++) //初始化
        {
            G[i].clear();
            lgt[i]=flgt[i]=Sz[i]=son[i]=dth[i]=fa[i]=top[i]=0;
        }
        for(int i=1;i<=n;i++)scanf("%d",num+i);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        fa[1]=dth[1]=1;//线段树下标 深度
        index=0;
        dfs1(1,-1);
        dfs2(1,1);
        build(1,1,n);
        while(p--)
        {
            char s[2];
            int l,r,k;
            scanf("%s",s);
            if(s[0]=='I')
            {
                scanf("%d%d%d",&l,&r,&k);
                change(l,r,k);
            }
            else if(s[0]=='D')
            {
                scanf("%d%d%d",&l,&r,&k);
                change(l,r,-k);
            }
            else
            {
                ans=0;
                scanf("%d",&k);
                query(1,lgt[k]);
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}
</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值