1012: [JSOI2008] Maximum number of maxnumber time limit:3 Sec Memory limit:162 MB
submit:10440 solved:4571
[Submit] [Status] [Discuss] Description
Now ask you to maintain a sequence, the following two operations are required: 1, query operation. Syntax: Q l Function: Query the end of the current sequence of L
The largest number in the number, and outputs the value of the number. Limit: l does not exceed the length of the current series. 2, insert operation. Syntax: a n function: N Plus
On 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 taken to a fixed constant d
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 there is no
Number.
Input
The first line is two integers, M and D, where m represents the number of operations (M <= 200,000), D, as described above, satisfies D within Longint. Next
M row, query operation, or insert operation.
Output
For each query operation, output a line. The line has only one number, which is the maximum number of the last L in the sequence.
Sample Input5 100
A 96
Q 1
A 97
Q 1
Q 2Sample Output96
93
theHINT
The data is as follows Http://pan.baidu.com/s/1i4JxCH3
It is easy to find that the topic requires maintenance of a monotonous stack ...
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 intn,d,t,x;7 Charch,hh;8 inttop,len,pos[200010],num[200010];9 intMain () {Tenscanf"%d%d",&n,&d); One while(n--){ Ascanf"%c%c%c%d",&hh,&ch,&hh,&x); - if(ch=='A'){ -x= (x+t)%D; thenum[++len]=x; - while(top&&num[pos[top]]<=x) top--; -pos[++top]=Len; - } + Else{ - intTmp=lower_bound (pos+1, pos+top+1, len-x+1)-Pos; +t=Num[pos[tmp]]; Aprintf"%d\n", T); at } - } - return 0; -}
monotonic stack BZOJ1012 [JSOI2008] Maximum number MaxNumber