思路:在单调队列不更新列首。由于查询区间大小不确定,所以不能保证下次是否还用到它

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 222222
#define ll long long

int que[N];
ll m,d;
ll a[N];
int cnt;
char op[2];
int num;

int main(){
    scanf("%d%lld",&m,&d);
    cnt = 0;
    ll t = 0;
    int l = 0;
    int r = -1;
    while(m--){

        scanf("%s%d",op,&num);
        if(op[0]=='Q'){
            int h = l;
            while(h<=r&&que[h]<=cnt-num) h++;
            t = a[que[h]];
            printf("%lld\n",t);
         }
        else{
            a[++cnt] = (num%d+t%d)%d;
            while(l<=r&&a[que[r]]<a[cnt]) --r;
            que[++r] = cnt;
        }
    }
}