【题目链接】
【算法】
很明显,我们可以用线段树解决此题
只需维护区间最值就可以了
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXM 200000
int M,D,x,l,lastans,len;
int Max[MAXM*4];
char opt;
template <typename T> inline void read(T &x) {
int f = 1; x = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
x *= f;
}
template <typename T> inline void write(T x) {
if (x < 0) { putchar('-'); x = -x; }
if (x > 9) write(x/10);
putchar(x%10+'0');
}
template <typename T> inline void writeln(T x) {
write(x);
puts("");
}
inline void push_up(int index) {
Max[index] = max(Max[index<<1],Max[index<<1|1]);
}
inline int query_max(int index,int l,int r,int ql,int qr) {
int mid;
if (l == ql && r == qr) return Max[index];
else {
mid = (l + r) >> 1;
if (mid >= qr) return query_max(index<<1,l,mid,ql,qr);
else if (mid + 1 <= ql) return query_max(index<<1|1,mid+1,r,ql,qr);
else return max(query_max(index<<1,l,mid,ql,mid),query_max(index<<1|1,mid+1,r,mid+1,qr));
}
}
inline void ins(int index,int l,int r,int pos,int val) {
int mid;
if (l == r) {
Max[index] = val;
return;
}
mid = (l + r) >> 1;
if (mid >= pos) ins(index<<1,l,mid,pos,val);
else ins(index<<1|1,mid+1,r,pos,val);
push_up(index);
}
int main() {
read(M); read(D);
while (M--) {
opt = getchar();
if (opt == 'Q') {
read(l);
writeln(lastans = query_max(1,1,MAXM,len-l+1,len));
} else {
read(x);
x = (x + lastans) % D;
ins(1,1,MAXM,++len,x);
}
}
return 0;
}