Description
Now ask you to maintain a sequence, the following two operations are required: 1, query operation. Syntax: Q L function: Query the maximum number of L in the number of the end of the current sequence and output the value of this number. Limit: l does not exceed the length of the current series. 2, insert operation. Syntax: a n function: N plus t, where T is the answer to the most recent query operation (t=0 if the query operation has not been performed), and the resulting result is modeled on a fixed constant d and the resulting answer is inserted at the end of the sequence. Limit: N is a non-negative integer and is within a long range. Note: The initial sequence is empty and does not have a number.
Input
The first line is two integers, M and D, where m represents the number of operations (M <= 200,000), and D, as described above, satisfies (0
Output
For each query operation, you should output the results sequentially, with each result in one row.
Sample Input5 100
A 96
Q 1
A 97
Q 1
Q 2Sample Output96
93
theHINTmaintain a tree segment ... The insertion point and query are processed once. Source:
#include <iostream>#include<cstdio>#defineN 200000using namespacestd;intsegtree[4*N];intN,me,ans;voidAddDataintNow ) {Segtree[now]=max (segtree[(now<<1)],segtree[(now<<1)+1]);}voidPointchange (intNowintLintRintXintv) { if(L==R) {segtree[now]+=v; Segtree[now]%=me;return;} intMid= (l+r) >>1; if(X<=mid) Pointchange (now<<1), l,mid,x,v); ElsePointchange ((now<<1)+1, mid+1, r,x,v); AddData (now);}voidQueryintNowintLintRintBeginintend) { if(Begin<=l && end>=r) {Ans=max (Ans,segtree[now]);return;} intMid= (l+r) >>1; if(begin<=mid) query ((now<<1), l,mid,begin,end); if(end>mid) query ((now<<1)+1, mid+1, r,begin,end);}intMain () {inti,num=0, X; Chars[Ten]; scanf ("%d%d",&n,&me); for(i=1; i<=n;i++) {scanf ("%s%d",s,&x); if(s[0]=='A') {num++; Pointchange (1,1, n,num,ans+x); } Else{ans=-99999999; Query (1,1, n,num-x+1, num); printf ("%d\n", ans); } } return 0;}
"BZOJ1012" "JSOI2008" Maximum number MaxNumber