HDU4348 To the moon【主席树】

T o   t h e   m o o n To\ the\ moon To the moon

Time Limit: 4000/2000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 9320Accepted Submission(s): 2206

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

Author
HIT

Source
2012 Multi-University Training Contest 5

TJ:

主席树模板题,要注意内存使用,普通的线段树可以把lazy标记下推,主席树的话为了节省内存要把lazy永久保存,求和的时候记录lazy和就好了

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e5+7;
int root[maxn],seq[maxn];
int n,m,nowt;
struct ljt_Tree{
	struct Node{
		int ls,rs,lazy;						//左右儿子编号
		LL sum;								//区间和 lazy标记
	}node[maxn*25];
	int cnt;
	void init(){cnt = 0;}
	void push_up(int rt,int l,int r){
		node[rt].sum = node[node[rt].ls].sum + node[node[rt].rs].sum + node[rt].lazy*(r-l);
	}
	void build(int l,int r,int &rt){						//初始化线段树 rt为root[0]
		cnt++;
		node[rt=cnt].lazy = 0;
		if(l+1==r){
			node[rt].sum = seq[l];
			return;
		}
		int mid = (l+r)>>1;
		build(l,mid,node[rt].ls);
		build(mid,r,node[rt].rs);
		push_up(rt,l,r);
	}
	void update(int L,int R,int l,int r,int &now,int pre,int c){				//区间[l,r)增加c
		if(r<=L || R<=l) return;
		node[now=++cnt] = node[pre];
		if(l<=L&&R<=r){
			node[now].lazy+=c;
			node[now].sum+=(LL)c*(R-L);
			return;
		}
		int mid = (L+R)>>1;
		update(L,mid,l,r,node[now].ls,node[pre].ls,c);
		update(mid,R,l,r,node[now].rs,node[pre].rs,c);
		push_up(now,L,R);
	}
	LL query(int L,int R,int l,int r,int rt,LL lazysum){
		if(l<=L&&R<=r) return node[rt].sum+lazysum*(R-L);
		int mid = (L+R)>>1;
		if(r<=L || R<=l) return 0;
		lazysum+=node[rt].lazy;
		return query(L,mid,l,r,node[rt].ls,lazysum)+query(mid,R,l,r,node[rt].rs,lazysum);
	}
}Seg_Tree;
int main(){
	while(scanf("%d %d",&n,&m)!=EOF){
		nowt = 0;
		Seg_Tree.init();
		for(int i=1;i<=n;i++) scanf("%d",&seq[i]);
		Seg_Tree.build(1,n+1,root[nowt]);
		for(int i=1;i<=m;i++){
			char op[2];
			scanf("%s",op);
			if(op[0]=='Q'){
				int l,r;
				scanf("%d %d",&l,&r);
				printf("%I64d\n",Seg_Tree.query(1,n+1,l,r+1,root[nowt],0));
			}
			else if(op[0]=='B') scanf("%d",&nowt);
			else if(op[0]=='H'){
				int l,r,t;
				scanf("%d %d %d",&l,&r,&t);
				printf("%I64d\n",Seg_Tree.query(1,n+1,l,r+1,root[t],0));
			}
			else if(op[0]=='C'){
				int l,r,c;
				scanf("%d %d %d",&l,&r,&c);
				nowt++;
				Seg_Tree.update(1,n+1,l,r+1,root[nowt],root[nowt-1],c);
			}
		}
	}
	return 0;
}
``
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值