HDU3966 Aragorn's Story 树链剖分

这篇博客探讨了在《指环王》背景下的Aragorn故事,通过裸地与树链剖分的概念,阐述了在复杂环境下敌人营地数量变化的数学模型与算法解决方案。

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

裸地不能在裸的树链剖分了,col忘记清空,狂wa一小时。

本来想学LINK CUT TREE的,考试接踵,做题状态全无。== ==暂时顺延了


Aragorn's Story

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


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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>

using namespace std;

#define INF 0x3ffffff
#define MAXN 100010

struct node
{
    int to,next;
}edge[9999999];

int head[MAXN],en;
int belong[MAXN],idx[MAXN],qid[MAXN],fi[MAXN];
int top[MAXN],len[MAXN];
int dep[MAXN],fat[MAXN],size[MAXN];
int Q[MAXN],vis[MAXN];
int n,m,k,cnt;
int a[MAXN],f[MAXN];

void add(int u,int v)
{
    edge[en].to=v;
    edge[en].next=head[u];
    head[u]=en++;
}

void split()
{
    int l,r;
    memset(dep,-1,sizeof(dep));
    l=0;
    dep[Q[r=1]=1]=0;
    fat[1]=-1;
    while(l<r)
    {
        int x=Q[++l];
        vis[x]=0;
        for(int y=head[x];y!=-1;y=edge[y].next)
            if(dep[edge[y].to]==-1)
            {
                dep[Q[++r]=edge[y].to]=dep[x]+1;
                fat[edge[y].to]=x;
            }
    }
    cnt=0;
    for(int i=n;i;i--)
    {
        int x=Q[i],p=-1;
        size[x]=1;
        for(int y=head[x];y!=-1;y=edge[y].next)
            if(vis[edge[y].to])
            {
                size[x]+=size[edge[y].to];
                if(p==-1||size[edge[y].to]>size[p])
                    p=edge[y].to;
            }
        if(p==-1)
        {
            idx[x]=len[++cnt]=1;
            belong[top[cnt]=x]=cnt;
        }
        else
        {
            idx[x]=++len[belong[x]=belong[p]];
            top[belong[x]]=x;
        }
        vis[x]=true;
    }
}

void getqid()
{
    fi[1]=1;
    for(int i=2;i<=cnt;i++)
        fi[i]=fi[i-1]+len[i-1];
    for(int i=1;i<=n;i++)
    {
        int blo=belong[i];
        qid[i]=fi[blo]+len[blo]-idx[i];
        f[qid[i]]=i;
    }
}

void update(int l,int r,int root,int L,int R,int d);
void change(int x,int y,int d)
{
    while(belong[x]!=belong[y])
    {
        if(dep[top[belong[x]]]<dep[top[belong[y]]])
            swap(x,y);
        update(1,n,1,qid[top[belong[x]]],qid[x],d);
        x=fat[top[belong[x]]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    update(1,n,1,qid[x],qid[y],d); //其他数据结构维护区间值
}



#define lson l,m,root<<1
#define rson m+1,r,root<<1|1

int node[MAXN<<2],col[MAXN<<2];

void push_up(int root)
{
    node[root]=max(node[root<<1],node[root<<1|1]);
}

void push_down(int root,int m)
{
    if(col[root])
    {
        col[root<<1|1]+=col[root];
        col[root<<1]+=col[root];
        node[root<<1]=node[root<<1]+(m-(m>>1))*col[root];
        node[root<<1|1]=node[root<<1|1]+(m>>1)*col[root];
        col[root]=0;
    }

}

void build(int l,int r,int root)
{
    col[root]=0;
    if(l==r)
    {
        node[root]=a[f[l]];
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(root);
}

void update(int l,int r,int root,int L,int R,int d)
{
    if(l>=L&&r<=R)
    {
        node[root]+=(r-l+1)*d;
        col[root]+=d;
        return ;
    }
    int m=(l+r)>>1;
    push_down(root,r-l+1);
    if(L<=m) update(lson,L,R,d);
    if(R>m)  update(rson,L,R,d);
    push_up(root);
}

int query(int l,int r,int root,int a)
{
    if(l==r) return node[root];
    int m=(l+r)>>1;
    push_down(root,r-l+1);
    int ret=0;
    if(a<=m)
        ret= query(lson,a);
    else
        ret= query(rson,a);
    push_up(root);
    return ret;
}

int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        int u,v,x;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(head,-1,sizeof(head));en=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        split();
        getqid();
        build(1,n,1);
        char str[4];
        for(int i=0;i<k;i++)
        {
            scanf("%s",str);
            if(str[0]=='I')
            {
                scanf("%d%d%d",&u,&v,&x);
                change(u,v,x);
            }
            else if(str[0]=='D')
            {
                scanf("%d%d%d",&u,&v,&x);
                change(u,v,-x);
            }
            else
            {
                scanf("%d",&u);
                printf("%d\n",query(1,n,1,qid[u]));
            }
           // for(int j=1;j<=n;j++)
           //     cout<<query(1,n,1,qid[j])<<" ";
           // cout<<endl;
        }

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值