【bzoj1012】[JSOI2008]最大数maxnumber

1012: [JSOI2008]最大数maxnumber

Time Limit: 3 Sec  Memory Limit: 162 MB
Submit: 8339  Solved: 3624
[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如上文中所述,满足D在longint内。接下来
M行,查询操作或者插入操作。

Output

  对于每一个询问操作,输出一行。该行只有一个数,即序列中最后L个数的最大数。

Sample Input

5 100
A 96
Q 1
A 97
Q 1
Q 2

Sample Output

96
93
96

HINT

  数据如下http://pan.baidu.com/s/1i4JxCH3

 

 

线段树裸题。。。

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<algorithm>
 8 using namespace std;
 9 int n,mod,t,len,maxx[200010*20];
10 inline int read()
11 {
12     int x=0,f=1;  char ch=getchar();
13     while(!isdigit(ch))  {if(ch=='-')  f=-1;  ch=getchar();}
14     while(isdigit(ch))  {x=x*10+ch-'0';  ch=getchar();}
15     return x*f;
16 }
17 void add(int root,int l,int r,int p,int v)
18 {
19     if(p<l||p>r)  return;
20     if(l==r)  {maxx[root]=v;  return;}
21     int mid=(l+r)/2;
22     add(root*2,l,mid,p,v);
23     add(root*2+1,mid+1,r,p,v);
24     maxx[root]=max(maxx[root*2],maxx[root*2+1]);
25 }
26 int find(int root,int l,int r,int x,int y)
27 {
28     if(y<l||x>r)  return -200;
29     if(x<=l&&y>=r)  return maxx[root];
30     int mid=(l+r)/2;
31     int left=find(root*2,l,mid,x,y);
32     int right=find(root*2+1,mid+1,r,x,y);
33     return max(left,right);
34 }
35 int main()
36 {
37     n=read();  mod=read();
38     for(int i=1;i<=n;i++)
39     {
40         char ch=getchar();  int x=read();
41         if(ch=='A')  add(1,1,n,++len,(x+t)%mod);  
42         else {t=find(1,1,n,len-x+1,len);  printf("%d\n",t);}
43     }
44     return 0;
45 }

 

posted @ 2016-09-11 17:43  chty  阅读(490)  评论(0编辑  收藏  举报