BZOJ 1012 maxnumber, bzojmaxnumber

Source: Internet
Author: User

BZOJ 1012 maxnumber, bzojmaxnumber
Description

Now, you are requested to maintain a sequence, which requires the following two operations: 1. query operation. Syntax: q l function: query the maximum number of L numbers at the end of the current series and output the value of this number. Limit: L cannot exceed the length of the current sequence. 2. insert operation. Syntax: A n function: add n to t, where t is the answer to the last query operation (if the query operation has not been executed, t = 0 ), then, modulo a fixed constant D and insert the obtained answer to the end of the sequence. Limit: n is a non-negative integer and is within the Long Integer Range. Note: The columns in the initial hours are empty and there is no number.

Input

The first line has two integers, M and D. M indicates the number of operations (M <= 200,000). D is equal

Output

For each query operation, you should output the results in sequence, and each result occupies one row.

Sample Input

5 100
A 96
Q 1
A 97
Q 1
Q 2

Sample Output

96
93
96

When I see this question, I first think of the Line Segment tree. If the line segment tree is half written, the insertion operation cannot be completed. So what are the data structures to be searched by maintaining the array to complete insertion? Splay? SBT? Although these issues can be quickly solved, the pressure on templates with over one hundred rows is high ..... So here we will introduce a special data structure-monotonous queue. This type of data structure is not commonly used, but it can be quickly solved. Monotonous queues, as the name implies, are the key elements in a queue and arranged in a certain way. For example, monotonic increasing or monotonic decreasing. The monotonic queue here is a monotonic decreasing queue, followed by the usage of the monotonic queue.

First, monotonous queues must be monotonous. In this question, because the maximum value of the last l-bit is found, we set a queue named maxx []. Maxx [I] indicates the maximum value from I to the end of the sequence. When finding the maximum value of the end L bit, output maxx [len-L + 1. Len is the length of the series. Okay, the queue is complete, and then how to update the queue value ------

Let's assume that the maximum number of digits at the end of a Series 1 2 3 4 5 is 5, so we can store it in the maxx array as follows: 5, 5, 5, 5, and 5 are all filled in. No matter what the number of L is, the output of maxx [len-L + 1] will not be affected. For example, the storage method of this series 1, 2, 5, 4, 3 in a monotonous pair column is 5, 5, 5, 4, and 3. If the maximum value of the last digit is 3, the return value of the last two digits is 4. The last three, four, and five digits are all five. It can be found that when an element is inserted, the element is added to the end of the team as long as it is smaller than the element at the end of the team. If the element is larger than the element at the end of the team, it is replaced until it is found to be smaller than the element, this is the essence of monotonous queues !!

Paste the following code: (there are some notes in the Code)

# Include <iostream> # include <cstdio> using namespace std; const int MAXN = 2001000; long int a [MAXN], maxx [MAXN]; // The reason for long is that the Integer Range int last = 0, M, mod, len may be exceeded during the continuous accumulation process; // I have personally experienced int main () {scanf ("% d", & M, & mod); for (int t = 1; t <= M; t ++) {char c; int x; scanf ("% s % d", & c, & x ); if (c = 'A') {A [++ len] = (last + x) % mod; for (int I = len; I> = 1; I --) // because it is monotonically decreasing, compare if (maxx [I] <a [len]) maxx [I] = a [len] from the last digit; // if the value in the queue is smaller than the inserted value, replace else break; // if it is larger than the inserted value, stop because the closer it is to the first value of the queue, the larger it is, no need to compare} else {printf ("% d \ n", maxx [len-x + 1]); // directly output the last = maxx [len-x + 1] ;}}

It can be reproduced. Please indicate the source !!!

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.