【题目描述】
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
HINT
数据如下http://pan.baidu.com/s/1i4JxCH3
Source
【题解】
任意一个支持单点修改,区间查询的数据结构都可以通过此题。
笔者使用的是树状数组。
/* --------------
user Vanisher
problem bzoj-1012
----------------*/
# include <bits/stdc++.h>
# define ui unsigned int
# define L 200000
using namespace std;
ui read(){
ui tmp=0, fh=1; char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') fh=-1; ch=getchar();}
while (ch>='0'&&ch<='9'){tmp=tmp*10+ch-'0'; ch=getchar();}
return tmp*fh;
}
ui h[L+10];
ui lowbit(ui x){
return x&(-x);
}
void modify(ui x, ui k){
while (x<=L){
h[x]=max(h[x],k);
x=x+lowbit(x);
}
}
ui query(ui x){
ui num=0;
while (x>0){
num=max(num,h[x]);
x=x-lowbit(x);
}
return num;
}
int main(){
ui n=read(), P=read(), now=L+1, las=0,k;
char opt;
for (ui i=1; i<=n; i++){
scanf("\n%c",&opt); k=read();
if (opt=='A'){
k=(k+las)%P;
modify(--now,k);
}
else {
las=query(now+k-1);
printf("%u\n",las);
}
}
return 0;
}