# HDU 4348 SPOJ 11470 To the moon

## Vjudge题面

Time limit 2000 ms
Memory limit 65536 kB
OS Windows
Source 2012 Multi-University Training Contest 5

## SPOJ原版题面

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

### Description

You‘ve been given N integers A[1], A[2],..., A[N]. On these integers, you need to implement the following operations:

• C l r d: Adding a constant d for every {Ai | l <= i <= r}, and increase the timestamp by 1, this is the only operation that will cause the timestamp increase.
• Q l r: Querying the current sum of {Ai | l <= i <= r}.
• H l r t: Querying a history sum of {Ai | l <= i <= r} in time t.
• 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
A1 A2 ... An...
(here following the m operations. )


### Output

... (for each query, simply print the result. )


### Example

Input 1:

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

Output 1:

4
55
9
15

Input 2:

2 4
0 0
C 1 1 1
C 2 2 -1
Q 1 2
H 1 2 1

Output 2:

0
1

## 解题思路

[SP11470]TTM-To the moon——主席树区间修改/差分主席树里面的法二，用了两棵主席树，没太看懂，先留坑不填了

## 源代码

#include <stdio.h>

int T;
int n, m;
long long a[100010];

struct Node
{
int lson, rson;
long long sum, lazy;
} t[4000010];
int cnt = 1, root[100010], timer;
void build(int &x, int l, int r)
{
x = cnt++;
t[x].lazy = 0;
if (l == r)
{
t[x].sum = a[l];
return;
}
int mid = l + r >> 1;
build(t[x].lson, l, mid);
build(t[x].rson, mid + 1, r);
t[x].sum = t[t[x].lson].sum + t[t[x].rson].sum;
}

void add(int &x, int last, int l, int r, int al, int ar, int d)
{
if (al > r || ar < l)
return;
x = cnt++;
t[x] = t[last];
if (al <= l && r <= ar)
{
t[x].lazy += d;
t[x].sum += (r - l + 1) * d;
return;
}
int mid = l + r >> 1;
add(t[x].lson, t[last].lson, l, mid, al, ar, d);
add(t[x].rson, t[last].rson, mid + 1,r, al, ar, d);
t[x].sum = t[t[x].lson].sum + t[t[x].rson].sum + t[x].lazy * (r - l + 1);
}

long long que(int x, int l, int r, int ql, int qr, long long la)
{
if (ql > r || qr < l)
return 0;
if (ql <= l && r <= qr)
return t[x].sum + la * (r - l + 1);
la += t[x].lazy;
int mid = l + r >> 1;
return que(t[x].lson, l, mid, ql, qr, la) + que(t[x].rson, mid + 1, r, ql, qr, la);
}

int main()
{
freopen("test.in", "r", stdin);
//scanf("%d", &T);
//T=1;
//while (T--)
while (~scanf("%d%d", &n, &m))
{
cnt = 1;
timer = 0;
for (int i = 1; i <= n; i++)
scanf("%lld", a + i);
build(root[0], 1, n);
char opt[2];
int l, r, t;
long long d;
while (m--)
{
scanf("%s", opt);
if (opt[0] == 'C')
{
scanf("%d%d%lld", &l, &r, &d);
add(root[timer + 1], root[timer], 1, n, l, r, d);
timer++;
}
else if (opt[0] == 'Q')
{
scanf("%d%d", &l, &r);
printf("%lld\n", que(root[timer], 1, n, l, r, 0));
}
else if (opt[0] == 'H')
{
scanf("%d%d%d", &l, &r, &t);
printf("%lld\n", que(root[t], 1, n, l, r, 0));
}
else
{
scanf("%d", &t);
timer = t;
}
}
//puts("");//hdu样例假了，这句不能要
}
return 0;
}

posted @ 2019-07-25 15:23  wawcac  阅读(193)  评论(0编辑  收藏  举报