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

Aragorn's Story

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

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 

Recommend

We have carefully selected several similar problems for you:  3964 3965 3962 3963 3967 

Statistic | Submit | Discuss | Note

【题意】

有一棵树n点m=n-1边,每个点有个权值。有三种操作:“I”/“D” u v k 对u到v的路径上所有点的权值 增加/减少 k。“Q” u 查询u点当前值。

【分析】

树链剖分模板即可。(这题完全可以再增加点趣味性,查询u到v路径上的点权和)

我分别用线段树和树状数组维护。线段树耗时大概是树状数组的两倍。

【代码-线段树】

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX=5e4+5;
const int INF=0x3f3f3f3f;

/*********** 图 ***********/
struct node{
    int t,next;
}edge[MAX*2];
int head[MAX],cnt;
void init(int n)
{
    cnt=0; memset(head,-1,sizeof(head[0])*(n+1));
}
void addedge(int u,int v)
{
    edge[cnt]=node{v,head[u]};
    head[u]=cnt++;
}

/************ 树链剖分 **************/
int dep[MAX],dad[MAX],siz[MAX],son[MAX];
int top[MAX],rk[MAX],id[MAX];
void dfs1(int u,int pre)
{
    siz[u]=1; //u本身
    son[u]=-1;
    int maxsiz=0; //最大的儿子子树大小
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].t;
        if(v==pre)continue;
        dep[v]=dep[u]+1;
        dad[v]=u;
        dfs1(v,u); //注意dfs的位置
        siz[u]+=siz[v];
        if(maxsiz<siz[v])
        {
            maxsiz=siz[v];
            son[u]=v;
        }
    }
}
void dfs2(int u,int fat,int &tag) //结点u,重链顶端
{
    top[u]=fat;
    rk[u]=++tag; //时间戳
    id[tag]=u; //tag对应在树上的结点号
    if(son[u]==-1)return; //没有孩子
    dfs2(son[u],fat,tag); //优先重链dfs
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].t;
        if(v==dad[u]||v==son[u])continue;
        dfs2(v,v,tag);
    }
}
void cuttree(int root) //将root为根的树链剖分
{
    int tag=0;
    dep[root]=0;  //根的深度和父结点做一个初始化
    dad[root]=root;   //这是一个假设,根的父结点还是根
    dfs1(root,root);
    dfs2(root,root,tag);
}

/************* 线段树 ****************/
ll sum[MAX<<2],mark[MAX<<2];
void build(int root,int l,int r) //初始线段树
{
    if(l==r)//叶子节点
    {
        mark[root]=0;
        sum[root]=0;
        return;
    }
    int mid=(l+r)/2;
    build(root*2,l,mid);   //左子树的创建
    build(root*2+1,mid+1,r); //右子树
    sum[root]=sum[root*2]+sum[root*2+1];
    mark[root]=0;
}
void nodeupdate(int root,int l,int r,ll num)
{
    mark[root] +=num;  //=
    sum[root]+=num*(r-l+1);  //=
}
void pushdown(int root,int l,int r)//传递给两孩子
{
    if(mark[root]==0)return;
    int mid=(l+r)/2;
    nodeupdate(root*2,l,mid,mark[root]);
    nodeupdate(root*2+1,mid+1,r,mark[root]);
    mark[root]=0;
}
void update(int left,int right,ll num, int root,int l,int r)//区间[kl,kr]修改
{
    if(left<=l&&r<=right){
        nodeupdate(root,l,r,num);
        return;
    }
    pushdown(root,l,r);
    int mid=(l+r)/2;
    if(left<=mid)
        update(left,right,num,root*2,l,mid);
    if(mid<right)
        update(left,right,num,root*2+1,mid+1,r);
    sum[root]=sum[root*2]+sum[root*2+1];
}
ll Qsum(int left,int right, int root,int l,int r)//区间和
{
    if(left<=l&&r<=right)
        return sum[root];
    pushdown(root,l,r);
    int mid=(l+r)/2;
    ll res=0;
    if(left<=mid)
        res+=Qsum(left,right,root*2,l,mid);//区间在左子树
    if(right>mid)
        res+=Qsum(left,right,root*2+1,mid+1,r);//在右子树
    return res;
}

/************** 树链->线段树 *********/
void modify(int u,int v,ll num, int limit) //修改
{
    int fu=top[u],fv=top[v];
    while(fu!=fv) //uv不在同一条链上
    {
        if(dep[fu]>=dep[fv]) //u的深度大
        {
            update(rk[fu],rk[u],num,1,1,limit);
            u=dad[fu]; fu=top[u];
        }
        else
        {
            update(rk[fv],rk[v],num,1,1,limit);
            v=dad[fv]; fv=top[v];
        }
    } //循环结束时,fu-fv在同一链
    if(dep[u]>dep[v])swap(u,v);
    update(rk[u],rk[v],num,1,1,limit);
}
ll query(int u,int v, int limit) //查询u-v路径
{
    ll res=0;
    int fu=top[u],fv=top[v];
    while(fu!=fv) //uv不在同一条链上
    {
        if(dep[fu]>=dep[fv]) //u的深度大
        {
            res+=Qsum(rk[fu],rk[u],1,1,limit);
            u=dad[fu]; fu=top[u];
        }
        else
        {
            res+=Qsum(rk[fv],rk[v],1,1,limit);
            v=dad[fv]; fv=top[v];
        }
    } //循环结束时,fu-fv在同一链
    if(dep[u]>dep[v])swap(u,v);
    res+=Qsum(rk[u],rk[v],1,1,limit);
    return res;
}

/************** solve **************/
int a[MAX];
bool solve()
{
    int n,m,P,u,v,k;
    if(scanf("%d%d%d",&n,&m,&P)==EOF)return 0;
    init(n); //初始图
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&u,&v);
        addedge(u,v);
        addedge(v,u);
    }
    cuttree(1); //树链剖分
    build(1,1,n); //建立线段树
    for(int i=1;i<=n;i++)modify(i,i,a[i],n);
    char op[3];
    while(P--)
    {
        scanf("%s",op);
        if(op[0]=='Q')
        {
            scanf("%d",&u);
            ll ans=query(u,u,n); //查询
            printf("%lld\n",ans);
        }
        else
        {
            scanf("%d%d%d",&u,&v,&k);
            if(op[0]=='D')k=-k;
            modify(u,v,k,n);
        }
    }
    return 1;
}
int main()
{
    while(solve());
}

【代码-树状数组】

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX=5e4+5;
const int INF=0x3f3f3f3f;

/*********** 图 ***********/
struct node{
    int t,next;
}edge[MAX*2];
int head[MAX],cnt;
void init(int n)
{
    cnt=0; memset(head,-1,sizeof(head[0])*(n+1));
}
void addedge(int u,int v)
{
    edge[cnt]=node{v,head[u]};
    head[u]=cnt++;
}

/************ 树链剖分 **************/
int dep[MAX],dad[MAX],siz[MAX],son[MAX];
int top[MAX],rk[MAX],id[MAX];
void dfs1(int u,int pre)
{
    siz[u]=1; //u本身
    son[u]=-1;
    int maxsiz=0; //最大的儿子子树大小
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].t;
        if(v==pre)continue;
        dep[v]=dep[u]+1;
        dad[v]=u;
        dfs1(v,u); //注意dfs的位置
        siz[u]+=siz[v];
        if(maxsiz<siz[v])
        {
            maxsiz=siz[v];
            son[u]=v;
        }
    }
}
void dfs2(int u,int fat,int &tag) //结点u,重链顶端
{
    top[u]=fat;
    rk[u]=++tag; //时间戳
    id[tag]=u; //tag对应在树上的结点号
    if(son[u]==-1)return; //没有孩子
    dfs2(son[u],fat,tag); //优先重链dfs
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].t;
        if(v==dad[u]||v==son[u])continue;
        dfs2(v,v,tag);
    }
}
void cuttree(int root) //将root为根的树链剖分
{
    int tag=0;
    dep[root]=0;  //根的深度和父结点做一个初始化
    dad[root]=root;   //这是一个假设,根的父结点还是根
    dfs1(root,root);
    dfs2(root,root,tag);
}

/************* bit tree ****************/
ll bit[MAX];  //区间增减+单点查询
void add(int k,ll num,int limit)
{
    for(;k<=limit;k+=k&-k)
        bit[k]+=num;
}
ll read(int k)
{
    ll res=0;
    for(;k;k-=k&-k)
        res+=bit[k];
    return res;
}
void update(int left,int right,ll num,int limit)
{
    add(left,num,limit);
    add(right+1,-num,limit);
}

/************** 树链-> bit tree *********/
void modify(int u,int v,ll num,int limit) //修改uv之间都增加num
{
    int fu=top[u],fv=top[v];
    while(fu!=fv) //uv不在同一条链上
    {
        if(dep[fu]>=dep[fv]) //u的深度大
        {
            update(rk[fu],rk[u],num,limit);
            u=dad[fu]; fu=top[u];
        }
        else
        {
            update(rk[fv],rk[v],num,limit);
            v=dad[fv]; fv=top[v];
        }
    } //循环结束时,fu fv在同一链
    if(dep[u]>dep[v])swap(u,v);
    update(rk[u],rk[v],num,limit);
}

/************** solve **************/
int a[MAX];
bool solve()
{
    int n,m,P,u,v,k;
    if(scanf("%d%d%d",&n,&m,&P)==EOF)return 0;
    init(n); //初始图
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&u,&v);
        addedge(u,v);
        addedge(v,u);
    }
    cuttree(1); //树链剖分
    for(int i=1;i<=n;i++)bit[i]=0;
    for(int i=1;i<=n;i++)update(rk[i],rk[i],a[i],n);
    char op[3];
    while(P--)
    {
        scanf("%s",op);
        if(op[0]=='Q')
        {
            scanf("%d",&u);
            ll ans=read(rk[u]); //单点查询
            printf("%lld\n",ans);
        }
        else
        {
            scanf("%d%d%d",&u,&v,&k);
            if(op[0]=='D')k=-k;
            modify(u,v,k,n);
        }
    }
    return 1;
}
int main()
{
    while(solve());
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪的期许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值