线段树
重点
1. build,modify,query,pushup,pushdown五种操作
2. 建树的时候,区间大小是确定的,只需要记住区间长度即可
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200010;
int n, m, p;
struct node {
int l, r;
int v;
} tr[N << 2];
void build(int u, int l, int r) {
tr[u] = {l, r};
if (l == r) return ;
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
}
int query(int u, int l, int r) { // 从u结点开始向下找,在区间l,r中的最大值
if (tr[u].l >= l && tr[u].r <= r) return tr[u].v;
int mid = tr[u].l + tr[u].r >> 1;
int v = 0;
if (l <= mid) v = query(u << 1, l, r);
if (r > mid) v = max(v, query(u << 1 | 1, l, r));
return v;
}
void pushup(int u) {
tr[u].v = max(tr[u << 1].v, tr[u << 1 | 1].v);
}
void modify(int u, int x, int v) { //从u节点开始向下找序号为x的叶子结点
if (tr[u].l == x && tr[u].r == x) tr[u].v = v;
else {
int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(u << 1, x, v);
else modify(u << 1 | 1, x, v);
pushup(u);
}
}
int main() {
cin >> m >> p;
build(1, 1, m);
char s[2];
int l, t, last;
while (m--) {
scanf("%s", s);
if (*s == 'Q') {
scanf("%d", &l);
last = query(1, n - l + 1, n);
printf("%d\n", last);
} else {
scanf("%d", &t);
modify(1, n + 1, (t + last) % p);
n++;
}
}
return 0;
}