1012: [JSOI2008]最大数maxnumber
Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 6763 Solved: 2907
[ 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如上文中所述,满足(0
Output
对于每一个查询操作,你应该按照顺序依次输出结果,每个结果占一行。
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
线段树按理说要会啊
可是平时太爱用n当长度变量结果打残了。。。。
注意变量名!!!!!
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 2E5 + 20;
typedef long long LL;
int c[maxn*20],t = 0,mo;
int n,i,j,cur = 0,m = 200000;
int get_ord()
{
char x = getchar();
while (x != 'A' && x != 'Q') x = getchar();
return x == 'A'?1:2;
}
void Insert(int o,int l,int r,int pos,int num)
{
if (l == r) {
c[o] = num;
return;
}
int mid = (l+r) >> 1;
if (pos > mid) Insert(2*o+1,mid+1,r,pos,num);
else Insert(2*o,l,mid,pos,num);
c[o] = max(c[2*o],c[2*o+1]);
}
int query(int o,int l,int r,int ql,int qr)
{
if (ql <= l && r <= qr) return c[o];
int ret = 0;
int mid = (l+r) >> 1;
if (ql <= mid) ret = query(2*o,l,mid,ql,qr);
if (qr > mid) ret = max(ret,query(2*o+1,mid+1,r,ql,qr));
return ret;
}
int get_int()
{
int ret = 0;
char x = getchar();
while (x < '0' || x > '9') x = getchar();
while (x >= '0' && x <= '9') ret = ret*10+x-'0',x = getchar();
return ret;
}
int main()
{
#ifdef YZY
freopen("yzy.txt","r",stdin);
#endif
cin >> n >> mo;
while (n--) {
int ord = get_ord();
if (ord == 1) {
int x;
scanf("%d",&x);
x = (x+t)%mo;
Insert(1,1,m,++cur,x);
}
else {
int x;
scanf("%d",&x);
printf("%d\n",t = query(1,1,m,max(cur-x+1,1),cur));
}
}
return 0;
}