这是能过的代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#define ll long long
using namespace std;
const int maxn = 200005;
ll d[maxn << 2];
void pushup(int p) {
d[p] = max(d[p << 1], d[p << 1 | 1]);
}
void update(int l, int r, int s, int t, ll c, int p) {
if (l <= s && t <= r) {
d[p] += (t - s + 1) * c;
return;
}
int mid = (s + t) >> 1;
if (l <= mid) update(l, r, s, mid, c, p << 1);
if (mid < r) update(l, r, mid + 1, t, c, p << 1 | 1);
pushup(p);
}
ll query(int l, int r, int s, int t, int p) {
if (l <= s && t <= r) {
return d[p];
}
int mid = (s + t) >> 1;
ll res = 0;
if (l <= mid) res = max(res, query(l, r, s, mid, p << 1));
if (mid < r) res = max(res, query(l, r, mid + 1, t, p << 1 | 1));
return res;
}
int main() {
ll m, mod;
cin >> m >> mod;
ll t = 0, len = 0;
while (m--) {
char opt;
int x;
cin >> opt >> x;
if (opt == 'Q') {
t = query(len - x + 1, len, 1, maxn, 1);
cout << t << endl;
} else {
len++;
update(len, len, 1, maxn, (x + t) % mod, 1);
}
}
return 0;
}
而如果把插入改成
} else {
update(++len, len, 1, maxn, (x + t) % mod, 1);
}
就会MLE抱铃 (悲