【可持久化线段树】I - To the moon HDU - 4348

I - To the moon HDU - 4348 

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

学习了很多版本还是这个版本最适合我

a数组存下所有前缀和,所以记得开long long

然后建树只保留增加的内容

用到了主席树的知识

因为会查询某个时间或返回之前时态,所以得用到主席树

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+10;
int T[maxn],tot,now;
ll a[maxn];

struct node
{
    int l,r;
    ll sum,add;
}tree[maxn*44];

void update(int &rt,int pre,int ql,int qr,int l,int r,int d)
{
    rt=++tot;
    tree[rt]=tree[pre];
    tree[rt].sum+=(min(r,qr)-max(l,ql)+1)*d;
    if(ql<=l&&qr>=r)
    {
        tree[rt].add+=d;
        return;
    }
    int mid=(l+r)/2;
    if(ql<=mid) update(tree[rt].l,tree[pre].l,ql,qr,l,mid,d);
    if(qr>mid) update(tree[rt].r,tree[pre].r,ql,qr,mid+1,r,d);
}

ll query(int &rt,int ql,int qr,int l,int r)
{
    int mid=(l+r)/2;
    ll ans=(min(r,qr)-max(l,ql)+1)*tree[rt].add;
    if(ql<=l&&qr>=r)
    {
        return tree[rt].sum;
    }
    if(ql<=mid) ans+=query(tree[rt].l,ql,qr,l,mid);
    if(qr>mid) ans+=query(tree[rt].r,ql,qr,mid+1,r);
    return ans;
}

int main()
{
    int n,m,flag=0;
    while(~scanf("%d%d",&n,&m))
    {
        tot=now=0;
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            a[i]=a[i-1]+x;
        }
        while(m--)
        {
            if(!flag)
                printf("\n");
            char cp[2];
            scanf("%s",cp);
            if(cp[0]=='C')
            {
                now++;
                int l,r,d;
                scanf("%d%d%d",&l,&r,&d);
                update(T[now],T[now-1],l,r,1,n,d);
            }
            else if(cp[0]=='H')
            {
                int l,r,t;
                scanf("%d%d%d",&l,&r,&t);
                ll ans=a[r]-a[l-1];
                ans+=query(T[t],l,r,1,n);
                printf("%lld\n",ans);
            }
            else if(cp[0]=='Q')
            {
                int l,r;
                scanf("%d%d",&l,&r);
                ll ans=a[r]-a[l-1];
                ans+=query(T[now],l,r,1,n);
                printf("%lld\n",ans);
            }
            else
            {
                int x;
                scanf("%d",&x);
                now=x;
                tot=T[now+1];
            }
            flag++;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值