1012: [JSOI2008]最大数maxnumber
Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 4273 Solved: 1918
[ Submit][ Status]
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
HINT
Source
#include<stdio.h>
#define N 200005
struct node
{
int x,y,ma;
}a[N*4];
int max(int a,int b)
{
return a>b?a:b;
}
void build(int x,int y,int t)
{
a[t].x=x; a[t].y=y;
if(x==y)
{
a[t].ma=0;
return ;
}
int mid=(x+y)>>1,temp=t<<1;
build(x,mid,temp);
build(mid+1,y,temp+1);
a[t].ma=max(a[temp].ma,a[temp+1].ma);
}
void update(int t,int k,int val)
{
if(a[t].x==a[t].y&&a[t].x==k)
{
a[t].ma=val;
return ;
}
int mid=(a[t].x+a[t].y)>>1,temp=t<<1;
if(k<=mid)
update(temp,k,val);
else
update(temp+1,k,val);
a[t].ma=max(a[temp].ma,a[temp+1].ma);
}
int query(int t,int x,int y)
{
if(a[t].x==x&&a[t].y==y)
return a[t].ma;
int mid=(a[t].x+a[t].y)>>1,temp=t<<1;
if(x>mid)
return query(temp+1,x,y);
else if(y<=mid)
return query(temp,x,y);
else
return max(query(temp,x,mid),query(temp+1,mid+1,y));
}
int main()
{
int m,d,t,len,k;
char str[10];
while(scanf("%d%d",&m,&d)!=EOF)
{
t=0; len=0;
build(1,N,1);
while(m--)
{
scanf("%s%d",str,&k);
if(str[0]=='Q')
{
t=query(1,len-k+1,len);
printf("%d\n",t);
}
else
{
k=(k+t)%d;
update(1,++len,k);
}
}
}
return 0;
}