Aragorn's Story HDU - 3966(树链剖分)

本文通过《指环王》中阿拉贡王子抵御敌人入侵的故事背景,介绍了一种树链剖分算法的具体实现过程。该算法能高效地处理树形结构上的路径增删操作及查询特定节点的实时值,适用于解决大规模数据集的问题。

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
树链剖分,和之前不同的是,值是在边上,这个点权,还稍微简单一些

#include<cstdio>
#include<cstring>
#include<algorithm>
#include <iostream>
#include<cmath>
#include <string>
#define N 50010
using namespace std;
const int INF=1<<30;
int n,m,t;
int siz[N],dep[N],fa[N],son[N];
int tid[N],top[N];
int head[N],to[N<<1],nex[N<<1],edge;
int total;
void addEdge(int u,int v)
{
    to[edge]=v,nex[edge]=head[u],head[u]=edge++;
    to[edge]=u,nex[edge]=head[v],head[v]=edge++;
}
void dfs(int u,int last,int d )
{
    dep[u]=d;
    siz[u]=1;
    fa[u]=last;
    for(int i=head[u];i;i=nex[i])
    {
        int v=to[i];
        if(v==last)
            continue;
        dfs(v,u,d+1);
        siz[u]+=siz[v];
        if(siz[v]>siz[son[u]])
            son[u]=v;
    }
}
void dfs(int u,int tp)
{
    top[u]=tp;
    tid[u]=++total;
    if(!son[u])
        return;
    dfs(son[u],tp);
    for(int i=head[u];i;i=nex[i])
    {
        int v=to[i];
        if(v==son[u]||v==fa[u])
            continue;
        dfs(v,v);
    }
}
int num[N],a[N];
int sum[N<<2],add[N<<2];
void pushUp(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushUp(rt);
}
void change(int l,int r ,int rt,int L,int R,int val)
{
    if(L<=l&&r<=R)
    {
        sum[rt]+=val*(l-r+1);
        add[rt]+=val;
        return ;
    }
    int mid=(r+l)>>1;
    if(mid>=L)
        change(l,mid,rt<<1,L,R,val);
    if(mid<R)
        change(mid+1,r,rt<<1|1,L,R,val);
}
int ask(int l,int r,int rt,int L,int R)
{
    if(L<=l&&r<=R)
        return sum[rt];
    int mid=(l+r)>>1;
    if(add[rt])
    {
        sum[rt<<1]+=(mid-l+1)*add[rt];
        add[rt<<1]+=add[rt];
        sum[rt<<1|1]+=(r-mid)*add[rt];
        add[rt<<1|1]+=add[rt];
        add[rt]=0;
    }
    int ans=0;
    if(mid>=L)ans+=ask(l,mid,rt<<1,L,R);
    if(mid<R)ans+=ask(mid+1,r,rt<<1|1,L,R);
    return ans;
}
void  change(int x,int y,int val)
{

    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]])swap(x,y);
        change(1,n,1,tid[top[x]],tid[x],val);
        x=fa[top[x]];
    }
    if(dep[x]<dep[y])swap(x,y);
    change(1,n,1,tid[y],tid[x],val);
}
int ask(int x,int y)
{
    int ans=0;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]])swap(x,y);
        ans+=ask(1,n,1,tid[top[x]],tid[x]);
    }
    if(dep[x]<dep[y])swap(x,y);
    ans+=ask(1,n,1,tid[y],tid[x]);
    return ans;
}
void init()
{
    memset(head,0,sizeof(head));
    memset(son,0,sizeof(son));
    memset(add,0,sizeof(add));
    edge=1;
    total=0;
}
int main()
{
    int x,y,c;
    char op[5];
    while(scanf("%d%d%d",&n,&m,&t)==3)
    {
        init();
        for(int i=1;i<=n;i++)
            scanf("%d",a+i);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            addEdge(x,y);
        }
        dfs(1,1,1);
        dfs(1,1);
        for(int i=1;i<=n;i++)
            num[tid[i]]=a[i];
        build(1,n,1);
        //cout<<"haha"<<endl;
        //cout<<t<<endl;
        while(t--)
        {
            scanf("%s",op);
            if(op[0]=='Q')
            {
                scanf("%d",&x);
                printf("%d\n",ask(x,x));
            }
            else
            {
                scanf("%d%d%d",&x,&y,&c);
                if(op[0]=='I')
                    change(x,y,c);
                else
                    change(x,y,-c);
            }
        }
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值