[JSOI2008] Maximum number of maxnumber (segment tree)

Source: Internet
Author: User


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


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.


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
theSolving : Bare-wire section tree, interval query, single-point modification. Code:
#include <cstdio>#include<algorithm>using namespacestd;template<classT>inlinevoidRead (T &x) {x=0;intf=0;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') f|= (ch=='-'), ch=GetChar ();  while(ch<='9'&&ch>='0') x= (x<<1) + (x<<3) + (ch^ -), ch=GetChar (); X=f?-x:x; return;}inttr[1000000],n,d,tot,t;voidAddintIintLintRintXinty) {    if(L==R) {Tr[i]+=y;tr[i]%=d;return;} intMid=l+r>>1; if(x<=mid) Add (i<<1, L,mid,x,y); ElseAdd (i<<1|1, mid+1, R,x,y); Tr[i]=max (tr[i<<1],tr[i<<1|1]);}intQueryintIintLintRintXinty) {    if(x<=l&&r<=y)returnTr[i]; intMid=l+r>>1; if(Y<=mid)returnQuery (i<<1, L,mid,x,y); Else if(X>mid)returnQuery (i<<1|1, mid+1, R,x,y); Else returnMax (Query (i<<1, L,mid,x,mid), query (i<<1|1, mid+1, r,mid+1, y));} intMain () {intXChars;    Read (n); read (D);  for(intI=1; i<=n;i++) {scanf ("%c",&s); read (x); if(s=='A') {x= (x+t)%D; Add (1,1,200000,++tot,x); }        Else{x=tot-x+1; X=query (1,1,200000, X,tot); T=x; printf ("%d\n", x); }    }    return 0;}

[JSOI2008] Maximum number of maxnumber (segment tree)

Related Article

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.