HDU4348 To The Moon <带修主席树>

【HDU4348】To The Moon

Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)

Problem Description
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

标签:带修主席树

题目大意:维护一个数据结构,使得其可有四种操作:区间修改,区间求和,某时间的区间和,返回某时间。

很明显这是一道主席树的板子题。不过此题要带区间修改。
普通区间修改需要加tag,并不断下传。而对于主席树,下传意味着新建节点,可能会MLE。所以这里我们暴力一点,直接标记永久化,这样写起来简洁,而且省空间。

直接上代码:

#include <iostream>
#include <cstdio>
#define MAX_N 100000
using namespace std;
struct node {int ls, rs; long long val, tag;} tr[MAX_N*50+5];
int n, m;
int cnt, now, root[MAX_N+5];
void updata(int v, int s, int t) {tr[v].val = tr[tr[v].ls].val+tr[tr[v].rs].val+(long long)(t-s+1)*tr[v].tag;}
void build(int v, int s, int t) {
	tr[v].ls = tr[v].rs = tr[v].tag = tr[v].val = 0;
	if (s == t) {
		scanf("%I64d", &tr[v].val);
		return;
	}
	tr[v].ls = ++cnt, tr[v].rs = ++cnt;
	int mid = s+t>>1;
	build(tr[v].ls, s, mid);
	build(tr[v].rs, mid+1, t);
	updata(v, s, t);
}
void modify(int v, int o, int s, int t, int l, int r, long long x) {
	tr[v] = tr[o];
	if (s >= l && t <= r) {
		tr[v].tag += x;
		tr[v].val += (long long)(t-s+1)*x;
		return;
	}
	int mid = s+t>>1;
	if (l <= mid)	modify(tr[v].ls = ++cnt, tr[o].ls, s, mid, l, r, x);
	if (r >= mid+1)	modify(tr[v].rs = ++cnt, tr[o].rs, mid+1, t, l, r, x);
	updata(v, s, t);
}
long long query(int v, int s, int t, int l, int r, long long tot) {
	if (s >= l && t <= r)	return tr[v].val+(long long)(t-s+1)*tot;
	tot += tr[v].tag;
	int mid = s+t>>1;
	long long ret = 0;
	if (l <= mid)	ret += query(tr[v].ls, s, mid, l, r, tot);
	if (r >= mid+1)	ret += query(tr[v].rs, mid+1, t, l, r, tot);
	return ret;
}
int main() {
	while(scanf("%d%d", &n, &m) != EOF) {
		cnt = now = 0;
		root[now] = ++cnt;
		build(root[now], 1, n);
		while (m--) {
			char ch;
			cin >> ch;
			if (ch == 'C') {
				int l, r;
				long long d;
				scanf("%d%d%I64d", &l, &r, &d);
				now++;
				root[now] = ++cnt;
				modify(root[now], root[now-1], 1, n, l, r, d);
			}
			if (ch == 'Q') {
				int l, r;
				scanf("%d%d", &l, &r);
				printf("%I64d\n", query(root[now], 1, n, l, r, 0LL));
			}
			if (ch == 'H') {
				int l, r, t;
				scanf("%d%d%d", &l, &r, &t);
				printf("%I64d\n", query(root[t], 1, n, l, r, 0LL));
			}
			if (ch == 'B') {
				int t;
				scanf("%d", &t);
				now = t;
			}
		}
	}
	return 0;
}
posted @ 2017-09-20 15:34  Azrael_Death  阅读(230)  评论(0编辑  收藏  举报