1012: [JSOI2008]最大数maxnumber
Time Limit: 3 Sec
Memory Limit: 162 MB
Submit: 10176
Solved: 4468
[
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
Sample Output
96 93 96
HINT
Source
[
Submit][
Status][
Discuss]
题解:这道题解法众多,有线段树,还有平衡树的...当然用单调队列最简单;
贴上代码:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<ctime>
#include<queue>
using namespace std;
const int maxn = 250001;
inline int read()
{
int x = 0 , f = 1; char ch = getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f= -1; ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+(ch-'0');ch=getchar();}
return x * f;
}
int a[maxn],maxnum[maxn];
int m,d,k,t;
int main(){
scanf("%d%d",&m,&d);
for(int i=0; i<m; i++){
char op[2]; scanf("%s",op);
if(op[0] == 'A')
{
int x = read();
a[++t] = (x+k)%d;
for(int i=t; i>0; i--)
if(maxnum[i] < a[t])
maxnum[i] = a[t];
else
break;
}
else
{
int x = read();
k = maxnum[t-x+1];
printf("%d\n",k);
}
}
return 0;
}