Bzoj 1012(线段树)

BZOJ传送门
水题一道,线段树维护即可。(省选题出模板?)

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#define ms(i,j) memset(i,j, sizeof i);
using namespace std;
const int MAXM = 200000+5;
int m,d;
#define lc (o<<1)
#define rc (o<<1|1)
#define M ((l+r)>>1)
struct st
{
int maxv[MAXM*4];
void pushup(int o)
{
maxv[o] = max(maxv[lc], maxv[rc]);
}
void build(int o, int l, int r)
{
maxv[o] = 0;
if (l==r) return ;
build(lc, l, M);
build(rc, M+1,r);
}
void update(int o, int l, int r, int p, int 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);
}
int query(int o, int l, int r, int x, int y)
{
if (x<=l&&r<=y)
{
return maxv[o];
}
int ans = -1;
if (x<=M) ans = max(query(lc, l, M, x, y), ans);
if (M<y) ans = max(query(rc, M+1, r, x, y), ans);
return ans;
}
}tree;
int getc()
{
char ans;
while (true)
{
ans = getchar();
if (ans=='A') return 0;
else if (ans=='Q') return 1;
}
}
int main()
{
scanf("%d%d", &m, &d);
tree.build(1,1,m);
int last = 0;
int cnt = 0;
for (int i=1;i<=m;i++)
{
int opt = getc();
int x;
scanf("%d", &x);
if (opt==0)
{
cnt++;
x = (x+last)%d;
tree.update(1,1,m,cnt, x);
} else
{
printf("%d\n", last = tree.query(1,1,m,cnt-x+1,cnt));
}
}
return 0;
}

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