[The sixth ACM competition in Shandong province] Title B Lowest Unique Price (SDUT3252) and lowestsdut3252

Source: Internet
Author: User

[The sixth ACM competition in Shandong province] Title B Lowest Unique Price (SDUT3252) and lowestsdut3252

Link: Here

This question is my biggest regret in this year's provincial competition. Ah... I feel sad when I think about it. This question is not difficult, but I already thought about how to do it during the competition. However, due to the mistakes made by the monks, our team spent the next two hours. The more you think about it, the more pity. Let's take a look at the ideas for this question.

This question seems to be done by most people with STL set. In fact, this question can be done using the line segment tree (do not know the line segment tree, please move to: here), and it is still a simple single point update problem. If you do not know how to update a single point of time, please go to: Here. Although I did not have many questions about line tree, I found a rule. By using this rule, we can better judge whether this question is a question of a line segment tree.

Rule: there are usually insert, delete, and other operations in the question. The data range is generally between the power 5 of 10 and the power 6 of 10, this type of question can be solved by using a line segment tree. (This is purely personal experience. If you have any exceptions, please note that I have not met the question of a line segment tree that does not obey this rule, of course, this is for reference only ).

Now let's get down to the truth. How should we deal with this question using the line segment tree? We use struct to define two variables: value and cnt. Value is used to store the input value (if there is no input, a large value is given for a long time). cnt is used to record the number of times the value of this node is input. If the number of inputs is 1, the value is equal to the value of the leaf node. In this way, the question is transformed into a single-point update, with the highest range value. For more information, see the code.

There is still a pitfall for this question. I don't know if this is the case for the provincial competition. After each group of operations, it gives a space, for example, "B 2. Be sure to pay attention to it. Otherwise, it will be RE for ten years.

[The Code is as follows]

// Because log2 (1000000) is about 20 // The depth of the tree is 20 # include <stdio. h> # define MAXN 1 <20 # define Max 1e + 6 # define INF 0 xfffffff # define min (a, B) a <B? A: bstruct ST {int value, cnt;} node [2 * MAXN]; int father [MAXN]; void build (int v, int left, int right) {node [v]. value = INF; node [v]. cnt = 0; if (left = right) {father [left] = v; return;} int mid = (left + right)/2; build (2 * v, left, mid); build (2 * v + 1, mid + 1, right); return;} void Update (int ri) {if (ri = 1) return; int fa = ri/2; int left = node [2 * fa]. value; int right = node [2 * fa + 1]. value; node [fa]. value = min (left, right); Update (fa) ;}int main () {char ch [5]; int T, n, num; scanf ("% d ", & T); while (T -- & scanf ("% d", & n) {build (1, 1, Max); while (n --) {scanf ("% s", ch); if (ch [0] = 'q') {if (node [1]. value = INF) printf ("none \ n"); elseprintf ("% d \ n", node [1]. value);} else {scanf ("% d", & num); if (ch [0] = 'B') node [father [num]. cnt ++; else if (ch [0] = 'C') node [father [num]. cnt --; if (node [father [num]. cnt = 1) node [father [num]. value = num; elsenode [father [num]. value = INF; Update (father [num]) ;}} return 0 ;}
(If any error occurs, please point it out. For more information, see 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.