bzoj1012[JSOI2008]最大数maxnumber

传送门

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

题解

我们每一次新加入一个数的时候,用单调栈保证栈里面所对应的位置上的元素单调递减。查询时用lower_bound函数查询第一个大于等于cnt-x+1的元素的位置,输出这个数即可。其中cnt为插入的数的总数。具体实现看下方程序即可。

程序

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 using namespace std;
 8 char ch[10];
 9 int m,cnt=0,top=0;
10 int sta[200010],num[200010];
11 int d,ans=0;
12 int main(){
13     scanf("%d%d",&m,&d);
14     int i,j,x;
15     for(i=1;i<=m;++i){
16         scanf("%s%d",ch,&x);
17         if(ch[0]=='A'){
18             x=(ans+x)%d;
19             num[++cnt]=x;
20             while(top && num[sta[top]]<=x)  top--;
21             sta[++top]=cnt;
22         }
23         else{
24             int nu=lower_bound(sta+1,sta+1+top,cnt-x+1)-sta;
25             ans=num[sta[nu]];
26             printf("%d\n",ans);
27         }
28     }
29     return 0;
30 }

 

posted @ 2018-03-05 16:20  lazytear  阅读(131)  评论(0编辑  收藏  举报