HDU - 4348 To 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

背景:
To The Moon是一款2011年11月发布的独立游戏,它是一款由RPG Maker提供支持的角色扮演冒险游戏。
到月球的前提是基于一种技术,使我们能够永久地重建对垂死的人的记忆。在这个问题上,我们会给你一个机会,来实现幕后的逻辑。

你已经得到了N个整数A [1],A [2],…,A [N]。在这些整数上,您需要执行以下操作:
1. C l r d:为每个{A i |增加一个常数d l <= i <= r},并将时间戳增加1,这是导致时间戳增加的唯一操作。
2.Q l r:查询{A i |的当前总和l <= i <= r}。
3. H l r t:查询{A i |的历史总和在时间t内,l <= i <= r}。
B t:回到时间t。而一旦你决定回到过去,你永远不可能获得前进版本了。
N,M≤10 5,| A [i] | ≤10^9,1≤l≤r≤N,| d | ≤104,系统从时间0开始,第一次修改在时间1,t≥0,不会引入你未来的状态。
输入:
n
A 1 A 2 … A n
…(这里是下面的操作)
输出:
…(对于每个查询,只需打印结果。)

题解:一道看起来特别和谐友好的可持续化线段树模板题目(๑•́ ₃ •̀๑),然后我卡了一个晚上的内存°(°ˊДˋ°) °,在线段树懒标记下传的时候,,,会有海量的新节点生成 (๑>m<๑) ,然后内存就“bong”,看过题解之后发现了某种神奇的黑科技。。(⁄•⁄ω⁄•⁄)可以将标记留在节点上一直不下传,查询的时候累加就行了,只需sum[x]=sum[lson]+sum[rson]+add[lson] * len1+add[rson] * len2。同时每次B操作后可将tot的值赋值为rt[t+1]因为t后面的状态已经没用啦(๑Ő௰Ő๑)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<climits>
#define mid ((l+r)>>1)
#define lson(x) tree[x].lc
#define rson(x) tree[x].rc
using namespace std;
typedef long long ll;
const int N=100000+10;
inline void getint(int&num){
    char c;bool flag=0;num=0;
    while((c=getchar())<'0'||c>'9')if(c=='-')flag=1;
    while(c>='0'&&c<='9'){num=num*10+c-48;c=getchar();}
    if(flag) num=-num;
}
inline void getint(ll&num){
    char c;bool flag=0;num=0;
    while((c=getchar())<'0'||c>'9')if(c=='-')flag=1;
    while(c>='0'&&c<='9'){num=num*10+c-48;c=getchar();}
    if(flag) num=-num;
}
struct node{
    int lc,rc;ll sum,add;
}tree[N*30];
int n,T,l,r,K,tot,tim,rt[N];char op[3];
void pushup(int x,int l,int r){
    int len1=mid-l+1,len2=r-mid;
    tree[x].sum=tree[lson(x)].sum+tree[rson(x)].sum+tree[lson(x)].add*len1+tree[rson(x)].add*len2;
}
void insert(int&x,int l,int r,int ql,int qr,ll val){
    if(!x||l>qr||r<ql) return ;
    tree[++tot]=tree[x],x=tot;
    if(l>=ql&&r<=qr) tree[x].add+=val;
    else{
        insert(tree[x].lc,l,mid,ql,qr,val);
        insert(tree[x].rc,mid+1,r,ql,qr,val);
        pushup(x,l,r);
    }
}
ll getsum(int x,int l,int r,int ql,int qr,ll now){
    if(!x||l>qr||r<ql) return 0;
    else if(l>=ql&&r<=qr)
        return tree[x].sum+(now+tree[x].add)*(r-l+1);
    else return getsum(tree[x].lc,l,mid,ql,qr,now+tree[x].add)
        +getsum(tree[x].rc,mid+1,r,ql,qr,now+tree[x].add);
}
int build(int l,int r){
    int x=++tot;
    tree[x]=(node){0,0,0,0};
    if(l==r) getint(tree[x].sum);
    else{
        tree[x].lc=build(l,mid);
        tree[x].rc=build(mid+1,r);
        pushup(x,l,r);
    }
    return x;
}
int main(){
    getint(n),getint(T);
    rt[1]=rt[0]=build(1,n);
    while(T--){
        scanf("%s",op);
        if(op[0]=='C'){
            tim++;
            getint(l),getint(r),getint(K);
            insert(rt[tim]=rt[tim-1],1,n,l,r,K);
            rt[tim+1]=tot;
        }
        else if(op[0]=='Q'){
            getint(l),getint(r);
            cout<<getsum(rt[tim],1,n,l,r,0)<<'\n';
        }
        else if(op[0]=='H'){
            getint(l),getint(r),getint(K);
            cout<<getsum(rt[K],1,n,l,r,0)<<'\n';
        }
        else if(op[0]=='B'){
            getint(K);
            tim=K;
            tot=rt[tim+1];
        }
    }
    printf("%d\n",tot);
}
/*
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
*/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值