1012: [JSOI2008]最大数maxnumber
Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 3050 Solved: 1336
[ 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
【分析】:
用线段树维护。设一个变量now记录当前数列中的元素个数,执行A操作时,将now加1,表示向数列中新加入一元素,并向now位置加入(T+N) mod D,执行Q操作时,对[now-L+1,now]的区间求最大值即可,程序用ZKW线段树实现
【代码】:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
#define MAX 1200001
int N,D,T=0,M,now=0,a[MAX];
void add(int where,int value)
{
for(a[where+=M]=value,where>>=1;where;where>>=1)
a[where]=a[2*where]>a[2*where+1]?a[2*where]:a[2*where+1];
}
int getmax(int L,int R)
{
int nowmax=0;
for(L+=M-1,R+=M+1;L^R^1;L>>=1,R>>=1)
{
if(~L&1) nowmax=max(nowmax,a[L^1]);
if(R&1) nowmax=max(nowmax,a[R^1]);
}
return nowmax;
}
int main()
{
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
scanf("%d%d",&N,&D);
scanf("\n");
for(M=1;M<=N+1;M<<=2);
for(int i=1;i<=N;i++)
{
char ch;
int num;
scanf("%c%d",&ch,&num);
scanf("\n");
if(ch=='A')
{
now++;
add(now,(T+num)%D);
}
if(ch=='Q')
{
T=getmax(now-num+1,now);
printf("%d\n",T);
}
}
//system("pause");
return 0;
}
转载注明出处:
http://blog.csdn.net/u011400953