Description
现在请求你维护一个数列,要求提供以下两种操作:1、 查询操作。语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。2、 插入操作。语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。注意:初始时数列是空的,没有一个数。
题解
想怎么写都可以,裸的数据结构题【可用Splay、线段树、树状数组等各种数据结构】,这里写的是线段树版本。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 200006
#define INF -1e9
using namespace std;
struct tree{
int l,r,key,max;
}a[maxn*4];
int n,len,ans,tt;
int _read(){
char ch=getchar();int sum=0;
while(!(ch>='0'&&ch<='9'))ch=getchar();
while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=getchar();
return sum;
}
void make_tree(int p,int l,int r){
a[p].l=l;a[p].r=r;a[p].key=0;a[p].max=0;
if(l==r)return;
int mid=(l+r)>>1;
make_tree(p<<1,l,mid);make_tree(p<<1|1,mid+1,r);
}
void update(int p,int t,int k){
if(a[p].l>t||a[p].r<t)return;
if(a[p].l==a[p].r){
a[p].key=k;a[p].max=k;
return;
}
update(p<<1,t,k);update(p<<1|1,t,k);
a[p].max=max(a[p<<1].max,a[p<<1|1].max);
}
int query(int p,int l,int r){
if(a[p].l>r||a[p].r<l)return INF;
if(a[p].l>=l&&a[p].r<=r)return a[p].max;
return max(query(p<<1,l,r),query(p<<1|1,l,r));
}
int main(){
freopen("maxnum.in","r",stdin);
freopen("maxnum.out","w",stdout);
n=_read();tt=_read();
make_tree(1,1,n);
while(n--){
char ch=getchar();int x=_read();
if(ch=='Q')ans=query(1,len-x+1,len),printf("%d\n",ans);
else update(1,++len,(ans+x)%tt);
}
return 0;
}