BZOJ 1012 单调队列+二分

思路:
维护一个单减的序列 序号是单增的 每回二分查找第一个比询问的大的值
我手懒 用得lower_bound

//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
#define int long long
int m,mod,top,jy,ans,tot;
char ch[3];
struct Node{int pos,weight;}node[200050],t;
bool operator < (Node a,Node b){
    return a.pos<b.pos;
}
signed main(){
    scanf("%lld%lld",&m,&mod);
    for(int i=1;i<=m;i++){
        scanf("%s%lld",ch,&jy);
        if(ch[0]=='A'){
            tot++,jy=(jy+ans)%mod;
            while(top&&node[top-1].weight<=jy)top--;
            node[top].pos=tot,node[top++].weight=jy;
        }
        else{
            t.pos=tot-jy+1;
            ans=lower_bound(node,node+top,t)->weight;
            printf("%lld
",ans);
        }
    }
}

这里写图片描述

原文地址:https://www.cnblogs.com/SiriusRen/p/6532235.html