P1198 [JSOI2008] Maximum numberTitle 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/Output sampleInput Sample # #:
5 100A 96Q 1 a 97Q 1Q 2
Sample # # of output:
969396
Description
[JSOI2008]
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <stack>5 using namespacestd;6 Const intm=200005, inf=2000000000;7 intM,d,len,x,last_query;8 Charcmd;9 structnode{Ten intl,r,mx; One}t[m*5]; A voidBuildintNowintLintR) { -t[now].l=l,t[now].r=r;t[now].mx=-INF; - if(L==R)return; the intMid= (L+R)/2; -Build (now*2, l,mid); -Build (now*2+1, mid+1, R); - } + intQueryintNowintLintR) { - if(t[now].l>r| | T[NOW].R<L)return-INF; + if(T[NOW].L>=L&&T[NOW].R<=R)returnt[now].mx; A returnMax (Query (now*2, l,r), query (now*2+1, L,r)); at } - voidUpdateintUintXintk) { - if(X>=T[U].L&&X<=T[U].R) t[u].mx=Max (t[u].mx,k); - Else return; -Update (u*2, x,k); -Update (u*2+1, x,k); in } - intMain () to { +scanf"%d%d\n",&m,&d); -Build1,1, m); the for(intI=1; i<=m;i++){ *CIN>>CMD;SCANF ("%d",&x); $ if(cmd=='Q') printf ("%d\n", Last_query=query (1, len-x+1, Len));Panax Notoginseng //find from the countdown x - if(cmd=='A') Update (1, ++len, (x+last_query)%d); the } + return 0; A}
Violence time-out steady, pay attention to the segment tree open four to five times times more space 、、、 the first time to do the provincial topic, although 、、、
Rokua P1198 [JSOI2008] Maximum number