BZOJ 1012: [JSOI2008]最大数maxnumber (线段树,,单点更新)

1012: [JSOI2008]最大数maxnumber

Time Limit: 3 Sec  Memory Limit: 162 MB Submit: 9717  Solved: 4244 [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

思路:线段树即可….

只是为了回忆一下..发现线段树还是没有忘记的23333

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月01日 星期六 16时37分55秒
 4File Name :code/bzoj/1012.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define PB push_back
20#define fst first
21#define sec second
22#define lson l,m,rt<<1
23#define rson m+1,r,rt<<1|1
24#define ms(a,x) memset(a,x,sizeof(a))
25typedef long long LL;
26#define pi pair < int ,int >
27#define MP make_pair
28using namespace std;
29const double eps = 1E-8;
30const int N=2E5+7;
31const int dx4[4]={1,0,0,-1};
32const int dy4[4]={0,-1,1,0};
33const int inf = 0x3f3f3f3f;
34int m,D;
35int tree[N<<2]; //树最大为M个节点..
36int lst; //最后一个查询的结果
37int cur;//当前队列中元素的个数.
38void PushUp( int rt)
39{
40    tree[rt] = max( tree[rt<<1] , tree[rt<<1|1]);
41}
42void update(int p,int sc, int l,int r,int rt)
43{
44//    cout<<"p:"<<p<<" sc:"<<sc<<" l:"<<l<<" r:"<<r<<" rt:"<<rt<<endl;
45    if (l==r)
46    {
47	tree[rt] = sc;
48	return;
49    }
50    int m = (l+r)>>1;
51    if (p<=m) update(p,sc,lson);
52    else update(p,sc,rson);
53    PushUp(rt);
54}
55int query( int L,int R,int l,int r,int rt)
56{
57//    cout<<"L:"<<L<<" R:"<<R<<endl;
58    if (L<=l && r<=R) return tree[rt];
59    int m = (l+r)>>1;
60    int ret = 0 ;
61    if (L<=m) ret = max(ret,query(L,R,lson));
62    if (R>=m+1) ret = max(ret,query(L,R,rson));
63    return ret;
64}
65int main()
66{
67	#ifndef  ONLINE_JUDGE 
68	freopen("code/in.txt","r",stdin);
69  #endif
70	ms(tree,0);
71	lst = 0 ;
72	cur = 0 ;
73	scanf("%d %d",&m,&D);
74	for ( int i = 1 ; i <= m ; i++)
75	{
76	    char opt[2];
77	    int x;
78	    scanf("%s %d",opt,&x);
79	    if (opt[0]=='A')
80	    {
81		update(++cur,(x+lst)%D,1,m,1);
82	    }
83	    else
84	    {
85		lst = query(cur-x+1,cur,1,m,1);
86		printf("%d\n",lst);
87	    }
88	}
89//	for ( int i = 1 ; i <= 30 ;  i++) printf("%d ",tree[i]);
90
91
92  #ifndef ONLINE_JUDGE  
93  fclose(stdin);
94  #endif
95    return 0;
96}