Bzoj 1012(线段树)

BZOJ 1012
线段树维护区间最大值单点修改即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stack>
#include<set>
#include<queue>
#include<climits>
#include<cmath>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
#define lc (o << 1)
#define rc (o << 1 | 1)
#define M ((l + r) >> 1)
namespace flyinthesky {
const LL MAXN = 200000 + 5;
LL now, n, d;
LL maxv[MAXN * 4];
char s[4];
void pushup(LL o) {maxv[o] = max(maxv[lc], maxv[rc]);}
void build(LL o, LL l, LL r) {
maxv[o] = 0;
if (l == r) return ; else build(lc, l, M), build(rc, M + 1, r);
pushup(o);
}
LL query(LL o, LL l, LL r, LL x, LL y) {
if (x <= l && r <= y) return maxv[o];
LL ans = 0;
if (x <= M) ans = max(ans, query(lc, l, M, x, y));
if (M < y) ans = max(ans, query(rc, M + 1, r, x, y));
return ans;
}
void update(LL o, LL l, LL r, LL p, LL v) {
if (l == r) {maxv[o] = v; return ;}
if (p <= M) update(lc, l, M, p, v); else if (M < p) update(rc, M + 1, r, p, v);
pushup(o);
}
void clean() {
now = 0;
}
int solve() {
scanf("%lld%lld", &n, &d);
clean();
build(1, 1, n);
LL t = 0;
for (LL x, i = 1; i <= n; i++) {
scanf("%s%lld", s, &x);
if (s[0] == 'A') update(1, 1, n, ++now, (x + t) % d);
else printf("%lld\n", t = query(1, 1, n, now - x + 1, now));
}
return 0;
}
}
int main() {
flyinthesky::solve();
return 0;
}

------ 本文结束 ------