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
|
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " is " << x << endl
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 5;
struct Node {
int l, r, dat;
}tr[N<<2];
int m, p;
void push_up(int u) {
tr[u].dat = max(tr[u<<1].dat, tr[u<<1|1].dat);
}
void build(int u, int l, int r) {
tr[u].l = l, tr[u].r = r;
if (l == r) return;
int mid = (l + r) >> 1;
build(u<<1, l, mid);
build(u<<1|1, mid + 1, r);
}
void update(int u, int x, int v) {
if (tr[u].l == tr[u].r) { tr[u].dat = v; return; }
int mid = (tr[u].l + tr[u].r) >> 1;
if (x <= mid) update(u<<1, x, v);
else update(u<<1|1, x, v);
push_up(u);
}
int query(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r) return tr[u].dat;
int mid = (tr[u].l + tr[u].r) >> 1;
int res = 0;
if (l <= mid) res = query(u<<1, l, r);
if (r > mid) res = max(res, query(u<<1|1, l, r));
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> m >> p;
build(1, 1, m);
char op;
int x, last = 0, n = 0;
while (m--) {
cin >> op >> x;
if (op ==
'Q') {
last = query(1, n - x + 1, n);
cout << last << '\n';
}
else if (op == 'A') {
update(1, n + 1, (1ll * last + x) % p);
n++;
}
}
return 0;
}
|