Monotonic Stacks
1012: [JSOI2008] Maximum number maxnumber time limit: 3 Sec Memory Limit: 162 MB
Submit: 4988 Solved: 2252
[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 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
[Submit] [Status] [Discuss]
/* ***********************************************author:ckbosscreated time:2015 May 12 Tuesday 23:35 49 seconds file Name : bzoj1012.cpp************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map>using namespace Std;const int Maxn=200200;int a[maxn],b[maxn],top;int N,d,x,t;char cmd[5];int Find (int x) {int low=0,mid,high=top-1;int ans=-1; while (Low<=high) {mid= (Low+high)/2;if (b[mid]<x) low=mid+1;else {ans=mid; high=mid-1;}} return A[ans];} int main () {//freopen ("In.txt", "R", stdin),//freopen ("OUT.txt", "w", stdout), scanf ("%d%d", &n,&d); int k=1;for (int i=0;i<n;i++) {scanf ("%s%d", cmd,&x), if (cmd[0]== ' A ') {x= (x+t)%d;k++;while (top>0&&a[top-1]<x) top--;a[top]=x; b [Top]=k; top++;} else if (cmd[0]== ' Q ') {t=find (k-x+1);p rintf ("%d\n", t);}} return 0;}
Bzoj 1012: [JSOI2008] Maximum number maxnumber monotone stack