Title Description
Now you are asked to maintain a sequence that requires the following two actions:
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 an integer (possibly negative) and within a long range.
Note: The initial sequence is empty and does not have a number.
Input/output format
Input Format:
The first line is two integers, m and d, where m represents the number of operations (m <= 200,000), and D is satisfied (0<d<2,000,000,000) as described above
The next m-line, one string per line, describes a specific operation. The syntax is as described Above.
Output format:
For each query operation, you should output the results sequentially, with each result in one ROW.
Input and Output sample input example # #:
5 100A 96Q 1 a 97Q 1Q 2
Sample # # of Output:
969396
- Jiangsu 2008 Provincial topic, in fact, is not difficult .
- Maintain the maximum number of sequence intervals, first think of the segment tree, and the line segment tree is suitable for the data range (O (nlogn)).
- But the interval is not long, how to build it?
- By Test instructions, the longest segment tree will be no more than 200000, then build a 200000 tree directly.
- Make a note of the current number of insertions, and insert the data dynamically behind the Sequence.
- The segment tree is used to maintain the maximum interval Value.
- Expect to score 100 Points.
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5 using namespacestd;6 7 structtree{8 intl,r,maxx;9} t[1000050];Ten one intn,mod,tt,now; a - voidBuildintXintLintR) { -t[x].l=l; T[x].r=r; the if(t[x].l==t[x].r)return; - intMid= (t[x].l+t[x].r) >>1; -Build (x*2, l,mid); -Build (x*2+1, mid+1, r); + } - + voidChangeintXintLintY) { a if(t[x].l==T[x].r) { att[x].maxx=y; - return; - } - intMid= (t[x].l+t[x].r) >>1; - if(l>mid) Change (x*2+1, l,y);ElseChange (x*2, l,y); -T[x].maxx=max (t[x*2].maxx,t[x*2+1].maxx); in } - to intFindintXintLintR) { + if(t[x].l==l && T[x].r==r)returnt[x].maxx; - intMid= (t[x].l+t[x].r) >>1; the if(l>mid)returnFind (x*2+1, l,r);Else * if(r<=mid)returnFind (x*2, l,r);Else $ returnMax (find (x*2, l,mid), find (x*2+1, mid+1, r));Panax Notoginseng } - the intmain () { +scanf"%d%d\n",&n,&mod); aBuild1,1, n); the for(intI=1; i<=n; i++) { + Charopt; - intx; $scanf"%c%d\n",&opt,&x); $ if(opt=='A') { -now++; -Change1, now, (x+tt)%mod); the}Else - if(opt=='Q') {WuyiTt=find (1, now-x+1, now); theprintf"%d\n", tt); - } wu } - return 0; about}
[JSOI2008] [BZOJ1012] Maximum number (dynamic open Point segment Tree)