# 描述

## 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.

# 思路

• 可以想到用可持久化线段树做区间更新。这样每次更新的时候，没有更新的结点就可以引用历史版本。
• 但是在查询的时候，懒惰标记下传会更新子树。在可持久化线段树中，我们的子树是历史版本的引用，固然不能更新；而如果新建新的结点，就会造成大量的空间开销。
• 因此在可持久化线段树中，我们要把懒惰标记永久化。
• 直接把当前区间的懒惰标记用一个参数传下去，然后找到要求和的区间时，直接把从上到下的懒惰标记累加和乘以区间长度再加上这段区间原本的和就可以了。
• 由于懒惰标记没有下传，所以在pushup的时候，也需要考虑标记造成的影响。
• 在版本回退的时候，可以把空间回收。
• 具体实现看代码，感觉有时候指针版本还是比较好看好写的。

0%