Maximum number (COGS 1844)

Source: Internet
Author: User

"Title 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 Format"

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 1≤intMA x

"Output Format"

For each query operation, you should output the results sequentially, with each result in one row.

"Sample Input"
5 100A 96Q 1 a 97Q 1Q 2
"Sample Output"
969396
/*segment Tree Maintenance maximum Note that n here can be the number of queries, because n is the maximum number of queries*/#include<cstdio>#include<iostream>#defineM 200010#defineLson l,m,now*2#defineRson m+1,r,now*2+1#defineLL Long Longusing namespacestd; LL mx[m*4],mod,t=0, N; LL Read () {CharC=getchar (); LL num=0, flag=1;  while(c<'0'|| C>'9'){if(c=='-') flag=-1; c=GetChar ();}  while(c>='0'&&c<='9') {num=num*Ten+c-'0'; c=GetChar ();} returnnum*Flag;}voidPUSH_UP (intNow ) {Mx[now]=max (mx[now*2],mx[now*2+1]);}voidInsert (LL v,ll pos,ll l,ll r,ll now) {if(l==R) {Mx[now]=v; return; } LL M= (l+r)/2; if(pos<=m) insert (V,pos,lson); ElseInsert (V,pos,rson); PUSH_UP (now);} ll query (ll x,ll y,ll l,ll r,ll now) {if(l>=x&&r<=y)returnMx[now]; LL m= (l+r)/2; LL ans=0; if(m>=x) ans=Max (Ans,query (X,y,lson)); if(m<y) ans=Max (Ans,query (X,y,rson)); returnans;}intMain () {Freopen ("bzoj_1012.in","R", stdin); Freopen ("Bzoj_1012.out","W", stdout); LL T=read (), p=0; Mod=read (); n=u;  while(t--)    {        CharC;cin>>C; LL x=read (); if(c=='Q') {LL ans=query (p-x+1P1N1); cout<<ans<<Endl; T=ans; }        Else{p++; Insert ((x+T)%mod,p,1N1); }    }    return 0;}
View Code

Maximum number (COGS 1844)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.