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