代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, d, t,cnt;
int a[2000001];
struct node {
int left, right, lazy;
int mx;
} tree[2000001];
void updata(int p) {
tree[p].left = tree[2 * p].left;
tree[p].right = tree[2 * p + 1].right;
tree[p].mx = max(tree[2 * p].mx, tree[2 * p + 1].mx);
}
void pushdown(int p) {
tree[p * 2].lazy += tree[p].lazy;
tree[p * 2 + 1].lazy += tree[p].lazy;
tree[p * 2].mx += tree[p].lazy;
tree[p * 2 + 1].mx += tree[p].lazy;
tree[p*2].mx%=d;
tree[p*2+1].mx%=d;
tree[p].lazy = 0;
}
void build(int p, int l, int r) {
if (l == r) {
tree[p].left = l;
tree[p].right = r;
tree[p].mx = a[l]%d;
return;
}
int mid = (l + r) / 2;
build(2 * p, l, mid);
build(2 * p + 1, mid + 1, r);
updata(p);
}
void change(int p, int x, int y) {
if (x == tree[p].left && x == tree[p].right) {
a[x] += y;
tree[p].mx += y;
tree[p].mx%=d;
return;
}
if (tree[p].lazy != 0)
pushdown(p);
if (x <= tree[2 * p].right)
change(2 * p, x, y);
if (x >= tree[2 * p + 1].left)
change(2 * p + 1, x, y);
updata(p);
}
int searchmx(int p, int l, int r) {
if (l == tree[p].left && r == tree[p].right) {
return tree[p].mx;
}
if (tree[p].lazy) {
pushdown(p);
}
if (r <= tree[2 * p].right) {
return searchmx(2 * p, l, r);
} else if (l >= tree[2 * p + 1].left) {
return searchmx(2 * p + 1, l, r);
} else {
return max(searchmx(2 * p, l, tree[2 * p].right), searchmx(2 * p + 1, tree[2 * p + 1].left, r));
}
}
signed main() {
cin >> n >> d;
for (int i = 1; i <= n; i++) {
char op;
int x, y;
cin >> op;
if (op == 'A') {
cin >> x;
a[++cnt]=(x+t)%d;
build(1,cnt-1,cnt);
} else {
cin >> x;
if(x==0)
{
t=0;
}
else if(x==1)
{
t=a[cnt];
}
else
{
t=searchmx(1,cnt-x+1,cnt);
}
cout << t<< endl;
}
}
}