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
Description
[JSOI2008]
Code
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdio>5#include <vector>6#include <Set>7#include <map>8 #defineMAXN 30000059 #defineINF 0x3f3f3f3fTen #defineM 262144 One using namespacestd; A - intTr[maxn],n,d,t,p;//P Point to the tail of the team - the voidAddintXintv) { - for(tr[x+=m]=v,x>>=1; x;x>>=1){ -Tr[x]=max (tr[x<<1],tr[x<<1|1]); - } + } - + intAskintLintR) {//closed Interval A intsum=0; at for(l=l+m-1, r=r+m+1; l^r^1; l>>=1, r>>=1){ - if(~l&1) Sum=max (sum,tr[l^1]); - if(r&1) Sum=max (sum,tr[r^1]); - } - returnsum; - } in - intMain () { to //freopen ("01.in", "R", stdin); +scanf"%d%d",&n,&D); - for(intI=0; i<=3000000; i++){ thetr[i]=-INF; * } $ while(n--){Panax Notoginseng CharOpintx; -Cin>>op>>x; the if(op=='A'){ +Add (++p, (x+t)%D); A } the Else{ +T=ask (p-x+1, p); -printf"%d\n", T); $ } $ } - return 0; -}80 min, Qaq
Originally wanted to do a simple problem practice practiced hand, the result blew up
Rokua P1198 [JSOI2008] Maximum number Label: line segment Tree