1012: [JSOI2008]最大数maxnumber
Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 13575 Solved: 5877
[ Submit][ Status][ Discuss]
Description
现在请求你维护一个数列,要求提供以下两种操作:1、 查询操作。语法:Q L 功能:查询当前数列中末尾L
个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。2、 插入操作。语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。注意:初始时数列是空的,没有一个数。
Input
第一行两个整数,M和D,其中M表示操作的个数(M <= 200,000),D如上文中所述,满足D在longint内。接下来M行,查询操作或者插入操作。
Output
对于每一个询问操作,输出一行。该行只有一个数,即序列中最后L个数的最大数。
Sample Input
5 100
A 96
Q 1
A 97
Q 1
Q 2
A 96
Q 1
A 97
Q 1
Q 2
Sample Output
96
93
96
93
96
题解
这道题啊,用单调栈就可以轻松搞定
代码
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
int top,len,a[200001],num[200001],n,d,t;
int main()
{
int x;char ch[1];
scanf("%d%d",&n,&d);
while(n--){
scanf("%s%d",ch,&x);
if(ch[0]=='A'){
x=(x+t)%d;
num[++len]=x;
while(top && num[a[top]]<=x) top--;
a[++top]=len;
}
else{
int y=lower_bound(a+1,a+top+1,len-x+1)-a;
t=num[a[y]];
printf("%d\n",t=num[a[y]]);
}
}
return 0;
}