1012: [JSOI2008]最大數maxnumber
阿新 • • 發佈:2018-06-13
TE CI zoj scrip TP 其中 末尾 arc else Time Limit: 3 Sec Memory Limit: 162 MB
Submit: 13258 Solved: 5737
[Submit][Status][Discuss]
A 96
Q 1
A 97
Q 1
Q 2
93
96
Submit: 13258 Solved: 5737
[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 100A 96
Q 1
A 97
Q 1
Q 2
Sample Output
9693
96
HINT
數據如下http://pan.baidu.com/s/1i4JxCH3
Source
經典線段樹
事實證明bzoj永遠不要用cin/cout
事實證明bzoj永遠不要用cin/cout
事實證明bzoj永遠不要用cin/cout
調了我一個小時!!!
1 #include<iostream>
2 #include<cstdio>
3 using namespace std;
4
5 const int maxn=800010;
6
7 int tree[maxn];
8 int M,D,t,pos;
9
10 void update(int k,int l,int r,int x)
11 {
12 if(l==r) {tree[k]=x;return;}
13 int mid=(l+r)>>1;
14 if(pos<=mid) update(k<<1,l,mid,x);
15 else update(k<<1|1,mid+1,r,x);
16 tree[k]=max(tree[k<<1],tree[k<<1|1]);
17 }
18
19 int search(int k,int l,int r,int s,int t)
20 {
21 if(l==s&&r==t) return tree[k];
22 int mid=(l+r)>>1;
23 if(t<=mid) return search(k<<1,l,mid,s,t);
24 if(s>mid) return search(k<<1|1,mid+1,r,s,t);
25 return max(search(k<<1,l,mid,s,mid),search(k<<1|1,mid+1,r,mid+1,t));
26 }
27
28 int main()
29 {
30 scanf("%d%d",&M,&D);
31 for(int i=1;i<=M;i++)
32 {
33 char o[5];int n;
34 scanf("%s",o);
35 scanf("%d",&n);
36 if(o[0]==‘A‘)
37 {
38 pos++;
39 update(1,1,M,(n+t)%D);
40 }
41 else
42 {
43 t=search(1,1,M,pos-n+1,pos);
44 printf("%d\n",t);
45 }
46 }
47 return 0;
48 }
1012: [JSOI2008]最大數maxnumber