维护单调栈,每次加一个数之前,把栈顶不大于它的数弹出。

查询时二分就可以了。

#include<cstdio>
#include<algorithm>
using namespace std;
int s[200001],t=0;
int a[200001],siz=0;
int n,clz,x,las;
int main(){
    char op[5];
    scanf("%d%d",&n,&clz);
    for(int i=1;i<=n;i++){
        scanf("%s%d",op,&x);
        if(op[0]=='A'){
            x=(x+las)%clz;
            a[++siz]=x;
            while(t&&a[s[t]]<=x) t--;
            s[++t]=siz;
        }
        else{
            printf("%d\n",las=a[s[lower_bound(s+1,s+t+1,siz-x+1)-s]]);
        }
    }
}

 

posted on 2017-08-17 10:59  nzher  阅读(111)  评论(0编辑  收藏  举报