hdu4348To the moon

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

思路:
1,很明显的主席树的题;
2,这里要用到线段树的成段更新,但是有一点,这里内存限制不允许我们像一般的线段树使用pushdown,因为这样会产生很多新的节点,据说是因为对于可持久化线段树来讲,前一阶段的状态是不可以改变的,如果我们在这里进行了pushdown的话,就要更新旧树被更新节点的子树节点,这样就会很耗内存,那怎样节省内存呢?那就是不进行pushdown,我们在进行query 的时候,将整个路径上的add加起来,这样就可以了;

#include <bits/stdc++.h>
using namespace std;
#define ls l,m
#define rs m+1,r
int n,m;
const int maxn=1e5+9;
long long  sum[maxn<<5];
int lson[maxn<<5],rson[maxn<<5],add[maxn<<5];
int T[maxn],tot;
int build(int l,int r)
{
    int rt=++tot;
    add[rt]=0;
    if(l==r)
    {
        cin>>sum[rt];
        lson[rt]=rson[rt]=0;
        return rt;
    }
    int m=l+r>>1;
    lson[rt]=build(ls);
    rson[rt]=build(rs);
    sum[rt]=sum[rson[rt]]+sum[lson[rt]];
    return rt;
}
int update(int pre,int L,int R,int d,int l,int r)
{
    int rt=++tot;
    sum[rt]=sum[pre],lson[rt]=lson[pre];
    rson[rt]=rson[pre],add[rt]=add[pre];
    sum[rt]+=1LL*(R-L+1)*d;
    if(L==l&&r==R)
    {
        add[rt]+=d;
        return rt;
    }
    int m=l+r>>1;

    if(R<=m) lson[rt]=update(lson[pre],L,R,d,ls);
    else if(L>m) rson[rt]=update(rson[pre],L,R,d,rs);
    else
    {
        lson[rt]=update(lson[pre],L,m,d,ls);
        rson[rt]=update(rson[pre],m+1,R,d,rs);
    }
    return rt;
}
long long query(int pre,int L,int R,int l,int r)
{
    long long ans=1LL*add[pre]*(R-L+1);
    if(L==l&&r==R)
        return sum[pre];
    int m=l+r>>1;
    if(R<=m) ans+=query(lson[pre],L,R,ls);
    else if(m<L) ans+=query(rson[pre],L,R,rs);
    else
    {
        ans+=query(lson[pre],L,m,ls);
        ans+=query(rson[pre],m+1,R,rs);
    }
    return ans;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int now=0;
        tot=0;
        T[0]=build(1,n);
        char op[3];
        while(m--)
        {
            scanf("%s",op);
            int l,r,d;
            if(op[0]=='Q')
            {
                scanf("%d%d",&l,&r);
                cout<<query(T[now],l,r,1,n)<<endl;
            }
            else if (op[0]=='C')
            {
                scanf("%d%d%d",&l,&r,&d);
                T[1+now]=update(T[now],l,r,d,1,n);
                now++;
            }
            else if(op[0]=='H')
            {
                scanf("%d%d%d",&l,&r,&d);
                cout<<query(T[d],l,r,1,n)<<endl;
            }
            else
            {
                scanf("%d",&now);
            }
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值