現在請求你維護一個數列,要求提供以下兩種操作:
1、 查詢操作。
語法:Q L
功能:查詢當前數列中末尾L個數中的最大的數,並輸出這個數的值。
限制:L不超過當前數列的長度。
2、 插入操作。
語法:A n
功能:將n加上t,其中t是最近一次查詢操作的答案(如果還未執行過查詢操作,則t=0),並將所得結果對一個固定的常數D取模,將所得答案插入到數列的末尾。
限制:n是整數(可能為負數)並且在長整范圍內。
注意:初始時數列是空的,沒有一個數。
輸入格式:
第一行兩個整數,M和D,其中M表示操作的個數(M <= 200,000),D如上文中所述,滿足(0<D<2,000,000,000)
接下來的M行,每行一個字符串,描述一個具體的操作。語法如上文所述。
輸出格式:
對於每一個查詢操作,你應該按照順序依次輸出結果,每個結果占一行。
5 100 A 96 Q 1 A 97 Q 1 Q 2輸出樣例#1:
96 93 96
1 #include <cstdio>
2 #include <iostream>
3 #include <algorithm>
4 #include <cstring>
5 using namespace std;
6
7 struct tree{
8 int l,r,maxx;
9 } t[1000050];
10
11 int n,mod,tt,now;
12
13 void build(int x,int l,int r) {
14 t[x].l=l; t[x].r=r;
15 if (t[x].l==t[x].r) return;
16 int mid=(t[x].l+t[x].r)>>1;
17 build(x*2,l,mid);
18 build(x*2+1,mid+1,r);
19 }
20
21 void change(int x,int l,int y) {
22 if (t[x].l==t[x].r) {
23 t[x].maxx=y;
24 return;
25 }
26 int mid=(t[x].l+t[x].r)>>1;
27 if (l>mid) change(x*2+1,l,y); else change(x*2,l,y);
28 t[x].maxx=max(t[x*2].maxx,t[x*2+1].maxx);
29 }
30
31 int find(int x,int l,int r) {
32 if (t[x].l==l && t[x].r==r) return t[x].maxx;
33 int mid=(t[x].l+t[x].r)>>1;
34 if (l>mid) return find(x*2+1,l,r); else
35 if (r<=mid) return find(x*2,l,r); else
36 return max(find(x*2,l,mid),find(x*2+1,mid+1,r));
37 }
38
39 int main() {
40 scanf("%d %d\n",&n,&mod);
41 build(1,1,n);
42 for (int i=1; i<=n; i++) {
43 char opt;
44 int x;
45 scanf("%c %d\n",&opt,&x);
46 if (opt=='A') {
47 now++;
48 change(1,now,(x+tt)%mod);
49 } else
50 if (opt=='Q') {
51 tt=find(1,now-x+1,now);
52 printf("%d\n",tt);
53 }
54 }
55 return 0;
56 }