【动态主席树,带lazy标记的区间更新求和】HDU - 4348 To the moon

【动态主席树,带lazy标记的区间更新求和】HDU - 4348 To the moon

【vj链接】


题目

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

注意事项

查询时不要下传lazy标记,会MLE,回到之前的时间的时候要把那些用不到的时间的结点回收利用,不然也会MLE。。。


AC代码

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

const LL maxn=100007;

int n,cnt;///m为hash后的数据范围
int a[maxn];///初始数据,从1开始
int tree[maxn];///tree[i]为第i个版本的根节点
int tot;///用来表示建了多少节点
struct node
{
    LL v;///节点值,在此例中为求和
    int lc,rc;///左右子树
    LL lz;
}t[maxn*26];
int build(int l,int r)
{
    int root=tot++;
    t[root].lz=t[root].lc=t[root].rc=0;
    if(l!=r)
    {
        int mid=(l+r)/2;
        t[root].lc=build(l,mid);
        t[root].rc=build(mid+1,r);
        t[root].v=t[t[root].lc].v+t[t[root].rc].v;
    }
    else t[root].v=a[++cnt];
    return root;
}
void push_down(int root,int li,int ri)
{
    t[root].v+=(LL)(ri-li+1)*t[root].lz;
    int newroot=tot++;
    t[newroot]=t[t[root].lc];
    t[newroot].lz+=t[root].lz;
    t[root].lc=newroot;
    newroot=tot++;
    t[newroot]=t[t[root].rc];
    t[newroot].lz+=t[root].lz;
    t[root].rc=newroot;
    t[root].lz=0;
}
int update(int root,int l,int r,int v,int li,int ri)
{
    //printf("update %d (%d,%d) v=%d (%d,%d)\n",root,l,r,v,li,ri);
    //每次update生成一个新的节点,是从根到需要更新的那个叶子的路径,其他节点与上一个版本完全相同
    int newroot=tot++;
    t[newroot]=t[root];
    if(l==li&&r==ri)
    {
        //printf("t[%d].lz=%d\n",newroot,t[newroot].lz);
        t[newroot].lz+=v;
        //printf("t[%d].lz=%d\n",newroot,t[newroot].lz);
        return newroot;
    }
    t[newroot].v+=(LL)(r-l+1)*v;
    //printf("t[%d].v=%d root=%d \n",newroot,t[newroot].v,root);
    if(t[newroot].lz) push_down(newroot,li,ri);
    int mid=(li+ri)/2;
    if(r<=mid) t[newroot].lc=update(t[newroot].lc,l,r,v,li,mid);
    else if(l>mid) t[newroot].rc=update(t[newroot].rc,l,r,v,mid+1,ri);
    else
    {
        t[newroot].lc=update(t[newroot].lc,l,mid,v,li,mid);
        t[newroot].rc=update(t[newroot].rc,mid+1,r,v,mid+1,ri);
    }
    return newroot;
}
LL query(int root,int l,int r,int li,int ri)
{
    //printf("Q%d (%d,%d) (%d,%d) lz=%d\n",root,l,r,li,ri,t[root].lz);
    ///l和r是所求区间
    ///li和ri只是用来帮助确定节点表示的范围的
    if(l==li&&r==ri) return t[root].v+(LL)(r-l+1)*t[root].lz;
    //if(t[root].lz) push_down(root,li,ri);
    int mid=(li+ri)/2;
    if(r<=mid) return t[root].lz*(r-l+1)+query(t[root].lc,l,r,li,mid);
    else if(l>mid) return t[root].lz*(r-l+1)+query(t[root].rc,l,r,mid+1,ri);
    else return t[root].lz*(r-l+1)+query(t[root].lc,l,mid,li,mid)+query(t[root].rc,mid+1,r,mid+1,ri);
}
char s[5];
int maxt[maxn];
int main()
{
    int q,T,i,j,k,l,r,d;
    LL x,y;
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        tot=0;
        cnt=0;
        tree[0]=build(1,n);
        j=0;
        maxt[0]=tot;
        for(i=1;i<=q;i++)
        {
            scanf("%s",s);
            switch(s[0]){
                case 'C':{
                    scanf("%d%d%d",&l,&r,&d);
                    tree[j+1]=update(tree[j],l,r,d,1,n);
                    j++;
                    maxt[j]=tot;
                    break;
                }
                case 'Q':{
                    scanf("%d%d",&l,&r);
                    x=query(tree[j],l,r,1,n);
                    printf("%lld\n",x);
                    break;
                }
                case 'H':{
                    scanf("%d%d%d",&l,&r,&d);
                    x=query(tree[d],l,r,1,n);
                    printf("%lld\n",x);
                    break;
                }
                default:{
                    scanf("%d",&d);
                    j=d;
                    tot=maxt[j]+1;
                    break;
                }
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值