题目链接:​​传送门​

时隔一年再写一遍

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
struct node {
int l, r, w;
}tree[A];
int n, m, a, b, c, ad, mod, now;
void build(int k, int l, int r) {
tree[k].l = l; tree[k].r = r;
if (l == r) {
tree[k].w = 0;
return;
}
int m = (l + r) >> 1;
build(k << 1, l, m);
build(k << 1 | 1, m + 1, r);
tree[k].w = max(tree[k << 1].w, tree[k << 1 | 1].w);
}
void change(int k, int pos, int val) {
if (tree[k].l == tree[k].r) {
tree[k].w += val;
return;
}
int m = (tree[k].l + tree[k].r) >> 1;
if (pos <= m) change(k << 1, pos, val);
else change(k << 1 | 1, pos, val);
tree[k].w = max(tree[k << 1].w, tree[k << 1 | 1].w);
}
int ask(int k, int l, int r) {
if (tree[k].l >= l and tree[k].r <= r) return tree[k].w;
int m = (tree[k].l + tree[k].r) >> 1, ans = 0;
if (l <= m) ans = max(ans, ask(k << 1, l, r));
if (r > m) ans = max(ans, ask(k << 1 | 1, l, r));
return ans;
}

int main(int argc, char const *argv[]) {
cin >> n >> mod; build(1, 1, n);
for (int i = 1; i <= n; i++) {
char opt; scanf(" %c %d", &opt, &a);
if (opt == 'A') {
a += ad; now++;
change(1, now, a % mod);
}
else {
ad = ask(1, now - a + 1, now);
ad %= mod;
printf("%d\n", ad);
}
}
return 0;
}