Files
python/TangDou/AcWing/SegmentTree/1275_XDS.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

75 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}