You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 200010;
int m; // m个操作
int p; // mod p
// 线段树求最大值模板
#define ls (u << 1)
#define rs (u << 1 | 1)
#define mid ((l + r) >> 1)
struct Node {
int l, r, len;
int v;
} tr[N << 2];
void pushup(int u) {
tr[u].v = max(tr[ls].v, tr[rs].v);
}
void build(int u, int l, int r) {
tr[u].l = l, tr[u].r = r;
if (l == r) return;
build(ls, l, mid), build(rs, mid + 1, r);
}
int query(int u, int L, int R) {
int l = tr[u].l, r = tr[u].r;
if (l >= L && r <= R) return tr[u].v;
if (l > R || r < L) return 0;
return max(query(ls, L, R), query(rs, L, R));
}
void modify(int u, int x, int v) {
int l = tr[u].l, r = tr[u].r;
if (l == r) {
tr[u].v = v;
return;
}
if (x <= mid)
modify(u << 1, x, v);
else
modify(u << 1 | 1, x, v);
pushup(u);
}
int n, last;
int main() {
// 加快读入
ios::sync_with_stdio(false), cin.tie(0);
cin >> m >> p;
// 单点修改,求区间最大值
// ① 初始化线段树,最多m次操作最多m个数区间范围[1, m]
build(1, 1, m);
int x;
char op;
while (m--) {
cin >> op >> x;
if (op == 'A') {
// 对于节点++n进行修改,值=(最后一次的last查询值 + x )%p
modify(1, ++n, ((LL)last + x) % p);
} else {
// u = 1:从根节点开始查询
// 查询序列中最后x个数的最大数: 查询[n - x + 1, n]内的最大值
last = query(1, n - x + 1, n);
printf("%d\n", last);
}
}
return 0;
}