Hdu 3966 . Aragorn's Story

本文介绍了一道经典的树链剖分题目,通过区间修改和单点查询的方式解决敌军入侵的问题。文章提供了完整的代码实现,并对关键步骤进行了注释说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

2011 Multi-University Training Contest 13 - Host by HIT

Solution

  • 这题是一道典型的树链剖分题,只需区间修改和单点询问。

  • 有关树链剖分,可以参考这里:

  • 由于只是一道英文题,有几点需要注意:

    1. 本题有多组数据

    2. I 询问是区间增加一个值,D 询问是区间减少一个值,Q 询问是询问一个点的值。

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
#define clr(a) memset(a,0,sizeof(a))
using namespace std;
const int N=50002;
struct data
{
    int l,r,c,n;
}f[N<<2];
int n,m,q,tot,num;
int first[N],nex[N<<1],en[N<<1];
int dep[N],fa[N],size[N];
int son[N],pre[N],tree[N],top[N],a[N];
char s[2];
inline int read()
{
    int data=0; char ch=0;
    while(ch<'0' || ch>'9') ch=getchar();
    while(ch>='0' && ch<='9') data=data*10+ch-'0',ch=getchar();
    return data;
}
inline void insert(int x,int y)
{
    nex[++tot]=first[x];
    first[x]=tot;
    en[tot]=y;
}
inline void dfs1(int x,int y)
{
    dep[x]=dep[fa[x]=y]+1;
    size[x]=1;
    for(int i=first[x];i;i=nex[i])
        if(en[i]!=fa[x])
        {
            fa[en[i]]=x;
            dfs1(en[i],x);
            size[x]+=size[en[i]];
            if(size[en[i]]>size[son[x]]) son[x]=en[i];
        }
}
inline void dfs2(int x,int y)
{
    top[pre[tree[x]=++num]=x]=y;
    if(!son[x]) return;
    dfs2(son[x],y);
    for(int i=first[x];i;i=nex[i])
        if(en[i]!=fa[x] && en[i]!=son[x]) dfs2(en[i],en[i]);
}
inline void make(int v,int l,int r)
{
    f[v].l=l,f[v].r=r;
    if(l==r)
    {
        f[v].n=a[pre[l]];
        return;
    }
    int mid=(l+r)>>1;
    make(v<<1,l,mid);
    make(v<<1|1,mid+1,r);
}
inline void update(int v)
{
    if(f[v].c)
    {
        int ls=v<<1,rs=ls|1;
        f[ls].n+=f[v].c,f[rs].n+=f[v].c;
        f[ls].c+=f[v].c,f[rs].c+=f[v].c;
        f[v].c=0;
    }
}
inline void change(int v,int x,int y,int z)
{
    if(f[v].l==x && f[v].r==y)
    {
        f[v].n+=z;
        f[v].c+=z;
        return;
    }
    update(v);
    int mid=(f[v].l+f[v].r)>>1;
    if(y<=mid) change(v<<1,x,y,z); else
        if(x>mid) change(v<<1|1,x,y,z); else
        {
            change(v<<1,x,mid,z);
            change(v<<1|1,mid+1,y,z);
        }
}
inline int find(int v,int x)
{
    if(f[v].l==f[v].r) return f[v].n;
    update(v);
    int mid=(f[v].l+f[v].r)>>1;
    if(x<=mid) return find(v<<1,x);
    return find(v<<1|1,x);
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&q))
    {
        clr(first),clr(f),clr(son);
        tot=num=0;
        for(int i=1;i<=n;i++) a[i]=read();
        for(int i=1;i<=m;i++)
        {
            int x=read(),y=read();
            insert(x,y);
            insert(y,x);
        }
        dfs1(1,0);
        dfs2(1,1);
        make(1,1,n);
        while(q--)
        {
            scanf("%s",&s);
            int x=read();
            if(s[0]=='Q') printf("%d\n",find(1,tree[x])); else
            {
                int y=read(),z=read();
                if(s[0]=='D') z=-z;
                int f1=top[x],f2=top[y];
                while(f1!=f2)
                {
                    if(dep[f1]<dep[f2]) swap(x,y),swap(f1,f2);
                    change(1,tree[f1],tree[x],z);
                    x=fa[f1],f1=top[x];
                }
                if(dep[x]>dep[y]) swap(x,y);
                change(1,tree[x],tree[y],z);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值