HDU 4348 To the moon(可持久化线段树)

To the moon

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 7293    Accepted Submission(s): 1700

Problem Description
Background
To The Moon is a independent game released in November 2011, it is a role-playing adventure game powered by RPG Maker.
The premise of To The Moon is based around a technology that allows us to permanently reconstruct the memory on dying man. In this problem, we'll give you a chance, to implement the logic behind the scene.

You‘ve been given N integers A [1], A [2],..., A [N]. On these integers, you need to implement the following operations:
1. C l r d: Adding a constant d for every {A i | l <= i <= r}, and increase the time stamp by 1, this is the only operation that will cause the time stamp increase.
2. Q l r: Querying the current sum of {A i | l <= i <= r}.
3. H l r t: Querying a history sum of {A i | l <= i <= r} in time t.
4. B t: Back to time t. And once you decide return to a past, you can never be access to a forward edition anymore.
.. N, M ≤ 10 5, |A [i]| ≤ 10 9, 1 ≤ l ≤ r ≤ N, |d| ≤ 10 4 .. the system start from time 0, and the first modification is in time 1, t ≥ 0, and won't introduce you to a future state.
 

Input
n m
A 1 A 2 ... A n
... (here following the m operations. )
 

Output
... (for each query, simply print the result. )
 

Sample Input
 
  
10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4 2 4 0 0 C 1 1 1 C 2 2 -1 Q 1 2 H 1 2 1
 

Sample Output
 
  
4 55 9 15 0 1


        大致题意:支持Q、C、H、B四种操作,分别对应查询当前区间[l,r]的和,修改区间[l,r]并使得时间加一,查询第x个时刻区间[l,r]的区间和,回到第x个时刻。
        纯数据结构题目,支持回到、查询历史版本,显然是一个可持久化数据结构,而支持查询区间和以及区间修改,首选的就是可持久化线段树了。这里有一个时间的定义,所以我们对每一个时间动态建立一个线段树,每个线段树继承之前时间的信息。对于两个查询操作,直接在对应时刻版本的线段树里面查询即可,我们用rt[i]表示第i个时刻对应线段树的根。关键就在于这个C更新操作了。

        对于区间更新操作,在普通线段树中,我们用的是找到对应区间,然后打lazy标记,在下次访问该点的时候再下传lazy标记的方式来进行的。但是,如果考虑在主席树种这么操作,我每一次的下传lazy标记又相当于是一次更新,理论上说需要再新开logN个节点来存储变化。或者说如果不需要新开节点,在处理种也会比较麻烦,所以我们考虑不下传lazy标记。其实这种操作在普通线段树中也有,只是我们有用过而已。对于每个修改,我累加lazy的数值,然后在询问的时候,如果询问区间小于当前区间,那么最后的答案就要累加上当前区间的lazy数值。这样从上到下就会累积每一个区间修改的变化,得到正确的结果。

        对于需要维护区间极值的问题,我们同样也是可以用这种不下传lazy标记的形式进行。具体见代码:

#include<bits/stdc++.h>
#define LL long long
#define N 150010
using namespace std;

int a[N],b[N],rt[N<<4];

struct Persistent_SegTree
{
    struct node{int l,r;LL sum,lazy;} T[N<<4];
    int cnt; void init(){cnt=0;}

    void build(int &i,int l,int r)
    {
        i=++cnt;
        T[i]=node{0,0,0,0};
        if (l==r)
        {
            T[i].sum=a[l];
            return;
        }
        int mid=(l+r)>>1;
        build(T[i].l,l,mid);
        build(T[i].r,mid+1,r);
        T[i].sum=T[T[i].l].sum+T[T[i].r].sum;
    }

    void update(int &i,int old,int l,int r,int L,int R,LL x)
    {
        i=++cnt; T[i]=T[old];                                    //继承old的信息
        T[i].sum+=1LL*(r-l+1)*x;
        if (l==L&&r==R)
        {
            T[i].lazy+=x; return;
        }
        int mid=(L+R)>>1;
        if (r<=mid) update(T[i].l,T[old].l,l,r,L,mid,x);
        else if (l>mid) update(T[i].r,T[old].r,l,r,mid+1,R,x);
        else
        {
            update(T[i].l,T[old].l,l,mid,L,mid,x);
            update(T[i].r,T[old].r,mid+1,r,mid+1,R,x);
        }
    }

    LL getsum(int i,int l,int r,int L,int R)
    {
        if (l==L&&R==r) return T[i].sum;
        int mid=(L+R)>>1;
        LL ans=T[i].lazy*1LL*(r-l+1);                            //每次要累加所有的区间变化
        if (r<=mid) ans+=getsum(T[i].l,l,r,L,mid);
        else if (l>mid) ans+=getsum(T[i].r,l,r,mid+1,R);
        else ans+=getsum(T[i].l,l,mid,L,mid)+getsum(T[i].r,mid+1,r,mid+1,R);
        return ans;
    }

} Persist;

int main()
{
    int n,q;
    while(~scanf("%d%d",&n,&q))
    {
        int t=0;
        Persist.init();
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        Persist.build(rt[0],1,n);
        while(q--)
        {
            char op[10];
            int l,r; LL x;
            scanf("%s%d",op,&l);
            if (op[0]=='Q')
            {
                scanf("%d",&r);
                printf("%lld\n",Persist.getsum(rt[t],l,r,1,n));
            }
            if (op[0]=='C')
            {
                ++t; scanf("%d%lld",&r,&x);
                Persist.update(rt[t],rt[t-1],l,r,1,n,x);
            }
            if (op[0]=='H')
            {
                scanf("%d%lld",&r,&x);
                printf("%lld\n",Persist.getsum(rt[x],l,r,1,n));
            }
            if (op[0]=='B') t=l;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值