题解:
单调队列维护
查找的时候二分一下
代码:
#include<bits/stdc++.h> using namespace std; const int N=2e5+5; int n,m,c,a[N],b[N],t; char s[2]; int main() { scanf("%d%d",&n,&m); int l=0,r=0; while (n--) { scanf("%s%d",&s,&c); if (s[0]=='A') { l++; while (r>0&&a[r]<(c+t)%m)r--; a[++r]=(c+t)%m;b[r]=l; } else { int le=1,ri=r; while (le<ri) { int mid=(le+ri)/2; if (b[mid]+c<=l)le=mid+1; else ri=mid; } t=a[le]; printf("%d\n",a[le]); } } }