BZOJ 1012, 最大数

来源:互联网 发布:如何还原手机网络设置 编辑:程序博客网 时间:2024/05/16 08:18

Problem

传送门

Mean

编写一个支持查询末尾L个数中最大数和在数列末尾插入的数据结构。

Analysis

乍一看觉得得写Splay,其实只是利用单调性就可以解决了。
当然这题做法很多,线段树什么的都可搞。但是弱渣写不动数据结构。
开两个数组,一个作为栈,一个记录栈中元素在数列中的位置。
每新加一个数字,就将栈顶小于它的元素弹出后加入栈(因为若栈顶元素小于新数,那么栈顶元素一定不是最大值)。

Code

#include<cstdio>const int N=200005;int m,d,n,t,top,cnt,q[N],f[N];void read(int &x){    char c;    while((c=getchar())<'0' || c>'9');    x=c-'0';    while((c=getchar())>='0' && c<='9') x=x*10+c-'0';}int main(){    read(m),read(d);    while(m--){        if(getchar()=='A'){            read(n);            int tmp=(n+t)%d;            while(top && q[top]<tmp) top--;            q[++top]=tmp,f[top]=++cnt;        }else{            read(n);            int l=1,r=top,p=cnt-n+1,ans;            while(l<=r){                int mid=l+r>>1;                if(f[mid]>=p) ans=q[mid],r=mid-1;                else l=mid+1;            }            printf("%d\n",t=ans);        }    }    return 0;}
0 0