To the moon(可持久化线段树区间更新)

Problem Description

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 {Ai | 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 {Ai | l <= i <= r}.
  3. H l r t: Querying a history sum of {Ai | 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 ≤ 105, |A[i]| ≤ 109, 1 ≤ l ≤ r ≤ N, |d| ≤ 104 … 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
A1 A2 … An
… (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.C l r d:将l到r之间所有的数+d,同时时间戳+1。
2.Q l r:查询当前时间戳下l到r的所有数的总和。
3.H l r t:.查询时间戳t下的l到r的所有数的和。
4.B t:将时间戳返回至t。

题目分析

时间戳可以看作不同时期的版本,因为可持久化线段树是只改一条通路,即只改logn个点,然后他访问元素的时候是要访问以前版本的点的,但是这个区间更新的话有lazy标记,他会波及到以前的版本,所以说lazy标记一定要有,但是又不能波及到以前的版本,那咱们就能想到lazy标记当参数传递,然后区间更新的时候没有pushdown,在pushup的时候加上这一点的lazy标记,所以说就能解决这个不同版本的问题了,现在请看代码


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100010;
typedef long long LL;
struct node{
	int l,r,lazy;
	LL sum;
}; 
int idx , root[N];
node tr[N*40];
void pu(int k,int l,int r){
	tr[k].sum = tr[tr[k].l].sum + tr[tr[k].r].sum + tr[k].lazy*(r-l+1);
}
int build(int l,int r){
	int q = ++idx;
	tr[q].lazy = 0;
	if(l == r){
		scanf("%lld",&tr[q].sum);
		return q;
	}
	int mid = l + r >> 1;
	tr[q].l = build(l,mid);
	tr[q].r = build(mid+1,r);
	pu(q,l,r);
	return q;
}
int update(int k,int l,int r,int L,int R,LL val){
	int q = ++idx;
	tr[q] = tr[k];
	if(L <= l && r <= R){
		tr[q].sum += (r-l+1)*val;
		tr[q].lazy += val;
		return q;
	}
	int mid = l + r >> 1;
	if(mid >= R) tr[q].l = update(tr[k].l,l,mid,L,R,val);
	else if(mid < L)  tr[q].r = update(tr[k].r,mid+1,r,L,R,val);
	else tr[q].l = update(tr[k].l,l,mid,L,mid,val) , tr[q].r = update(tr[k].r,mid+1,r,mid+1,R,val);
	pu(q,l,r);
	return q;
}
LL query(int p,int l,int r,int L,int R,LL lz){
	if(l == L && r == R) return tr[p].sum + lz*(r-l+1);
	int mid = l + r >> 1;
	int ans = 0;
	lz += tr[p].lazy;
	if(mid >= R) return query(tr[p].l,l,mid,L,R,lz);
	else if(mid < L) return query(tr[p].r,mid+1,r,L,R,lz);
	else return query(tr[p].l,l,mid,L,mid,lz) + query(tr[p].r,mid+1,r,mid+1,R,lz);
}
int main(){
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		idx = 0;
		root[0] = build(1,n);
		int tim = 0;
		while(m--){
			char op[2];
			scanf("%s",op);
			if(op[0] == 'C'){
				int l,r;
				LL x;
				scanf("%d%d%lld",&l,&r,&x);
				tim++;
				root[tim] = update(root[tim-1],1,n,l,r,x);
			}
			else if(op[0] == 'Q'){
				int l,r;
				scanf("%d%d",&l,&r);
				printf("%lld\n",query(root[tim],1,n,l,r,0));
			}
			else if(op[0] == 'H'){
				int l,r,t;
				scanf("%d%d%d",&l,&r,&t);
				printf("%lld\n",query(root[t],1,n,l,r,0));
			}
			else{
				int t;
				scanf("%d",&t);
				tim = t;
			}
		}
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宇智波一打七~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值