1012: [JSOI2008] Maximum number of maxnumber time limit:3 Sec Memory limit:162 MB
submit:4435 solved:2000
[Submit] [Status] 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
theHINT Source
The sum: This one... Online said a lot of high-end algorithm (hansbug: Say is a line tree, but how to add in; monotonous queue, trouble; The tree array interval is the most value, does not write Phile: I am also drunk) and then I think of the "Introduction to the algorithm" above that for static fast interval maximum value of RMQ (Can do O ( NLOGN) initialization, O (1) query), and this problem requires that will not stop at the end of the sequence to append the number, this will find the original RMQ algorithm can be added to an additional number operation (this is my own AH hehe)--When the new numbers, For each level of a one-dimensional array will only need to append a second, each level of the append can be analogous to the original query operation ... Then can be modified O (LOGN), when evaluated O (1), the total complexity of O (nlogn+n), n<=200000, looks a bit dangerous, but a test or accept ...
1 Constlx=200000; Ly=trunc (ln (LX)/LN (2))+1;2 var3 I,j,k,l,m,n,p,t:longint;4A:Array[0.. Ly0.. lx ofLongint;5 C1:char;6 functionMax (x,y:longint): Longint;7 begin8 ifX>y ThenMax:=xElsemax:=y;9 End;Ten procedureAdd (x:longint); One varI,j,k:longint; A begin -X:=xMoDp; -a[0, m+1]:=x; the fori:=1 toLy Do - begin -J:=M-TRUNC (exp (I*LN (2)))+2; - ifj<1 ThenBreak ; +K:=j+trunc (exp ((i-1) *ln (2))); -A[i,j]:=max (a[i-1, j],a[i-1, K]); + End; A Inc (M); at End; - functionLast (x:longint): Longint; - varI:longint; - begin - ifX>m Thenexit (Last (m)); -I:=trunc (ln (x)/LN (2)); inLast:=max (a[i,m-x+1],A[I,M-TRUNC (exp (I*LN (2)))+1]); - End; to begin + readln (n,p); -m:=0; the fori:=1 toN Do * begin $ readln (c1,l);Panax Notoginseng CaseUpCase (C1) of - 'A': Add (L +t); the 'Q':begin +t:=Last (l); A Writeln (t); the End; + End; - End; $ End. $
1012: [JSOI2008] Maximum number of maxnumber