题意:
1.查询序列后q个数字中的最大值
2.在序列尾部插入(n+t)%d,其中t是上一次查询的结果
题解:
首先想到的是用一个线段树去维护整个后Q个数字的区间。其实并不需要这么做,考虑这样一个问题,如果一个数右面有一个比他大的数,那么这个数永远不会成为最大值,写到这里这题基本上就算完成了,一道单调栈+二分的题目。
/*
ID:Agreement
LANG:C++
*/
// Invincible
#include <bits/stdc++.h>
#define rep( i , l , r ) for( int i = (l) ; i <= (r) ; ++i )
#define per( i , r , l ) for( int i = (r) ; i >= (l) ; --i )
#define erep( i , u ) for( int i = head[(u)] ; ~i ; i = e[i].nxt )
using namespace std;
inline int _read(){
register int x = 0 , f = 1;
register char ch = getchar();
while( ch > '9' || ch < '0' ) { if( ch == '-' ) f = -1; ch = getchar(); }
while( ch >= '0' && ch <= '9' ){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int maxn = 2 * 1e5 + 5;
int N , MOD , t , top , l , x;
int s[maxn] , a[maxn];
char ch[20];
int main(){
scanf("%d %d" , &N , &MOD);
while( N-- ){
scanf("%s%d" , ch , &x);
if( ch[0] == 'A' ){
x = (x + t) % MOD;
s[++l] = x;
while( top && s[a[top]] < x ) top--;
a[++top] = l;
}else{
int y = lower_bound( a + 1 , a + 1 + top , l - x + 1 ) - a;
printf("%d\n" , t = s[a[y]]);
}
}
return 0;
}