http://www.lydsy.com/JudgeOnline/problem.php?id=1012| |https://www.luogu.org/problem/show?pid=1198Time limit:3 Sec Memory limit:162 MB
submit:10824 solved:4735
[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
Source
① interval Query Most values consider the segment tree,
1#include <algorithm>2#include <iostream>3#include <cstdio>4 5 using namespacestd;6 7 Const intN200005);8 intT,mod,n,x[n],ans;9 CharCh,s[n];Ten One structTree A { - intl,r,sum; -}tr[n<<2]; the #defineLC (NOW<<1) - #defineRC (now<<1|1) - #defineMid (Tr[now].l+tr[now].r>>1) -InlinevoidTREE_UP (intNow ) + { -tr[now].sum=Max (tr[lc].sum,tr[rc].sum); + } A voidTree_build (intNowintLintR) at { -Tr[now].l=l; Tr[now].r=R; - if(L==R)return ; - Tree_build (lc,l,mid); -Tree_build (rc,mid+1, R); - } in voidTree_change (intNowintTo,intx) - { to if(tr[now].l==TR[NOW].R) + { -tr[now].sum=x; the return ; * } $ if(to<=mid) Tree_change (lc,to,x);Panax Notoginseng ElseTree_change (rc,to,x); - tree_up (now); the } + intTree_query (intNowintLintR) A { the if(TR[NOW].L==L&&R==TR[NOW].R)returntr[now].sum; + if(R<=mid)returntree_query (lc,l,r); - Else if(L>mid)returntree_query (rc,l,r); $ Else returnMax (Tree_query (Lc,l,mid), Tree_query (rc,mid+1, R)); $ } - - intMain () the { -scanf"%d%d",&t,&MoD);Wuyi for(intI=1; i<=t;i++) the { -cin>>ch>>x[i]; s[i]=ch; Wu if(ch=='A') n++; - } AboutTree_build (1,1, n); $ intCnt=0; - for(intI=1; i<=t;i++) - { - if(s[i]=='A') ATree_change (1, ++cnt, (Ans+x[i])%MoD); + Else the { -Ans=tree_query (1, cnt-x[i]+1, CNT); $printf"%d\n", ans); the } the } the return 0; the}
Line segment Tree AC
② monotonically Descending Stack, two-point lookup value at l
1#include <algorithm>2#include <cstdio>3 4 using namespacestd;5 6 Const intN200005);7 intn,mod,ans,x;8 intStack[n],top,num[n],len;9 Ten intMain () One { Ascanf"%d%d",&n,&MoD); - for(Charch[2];n--;) - { thescanf"%s%d",ch,&x); - if(ch[0]=='A') - { -x= (X+ans)%MoD; +num[++len]=x; - for(;num[stack[top]]<=x&&stack[top]>0&⊤) top--; +stack[++top]=Len; A } at Else - { - intL=1, r=top; - for(intmid;l<=R;) - { -Mid=l+r>>1; in if(stack[mid]<len-x+1) l=mid+1; - Elser=mid-1, ans=Num[stack[mid]]; to } +printf"%d\n", ans); - } the } * return 0; $}
monotonic Stack ac
BZOJ--1012: [JSOI2008] Maximum number of MaxNumber | | Rokua--p1198 [JSOI2008] Maximum number