Noi2002 galaxy legends

Source: Internet
Author: User

// The training has suffered a huge blow... So I decided that I had to write an answer to each question to ensure that I understood it... (Water questions or something will not be flushed)

Theme

To merge and query a given sequence, you must be able to obtain the number of elements between any two elements.

Problem Solving Process

Didn't Nima talk about water problems during training! I thought about it for almost a day... First, it is undoubtedly necessary to use and query the set, and additional fields must be maintained. However, it is because the system has gone astray in the selection of information maintenance, which leads to stuck all the time. The original idea was to use an array before to save the number of elements before an element, during the merge operation, another array Delta is used to record the changes in the number of elements in the sequence so as to update the before value (the Delta record is on the root of one of the sets during the merge ), however, this method does not seem feasible (at least I cannot think of it): After the before value is updated during the path compression process, the Delta value should be changed to 0 immediately, otherwise, Delta will continue to be accumulated when you access the node next time. However, if Delta is updated to 0 immediately, maybe the before value of the child node of the node cannot be updated (because the leaf node updates the before value by reading the sum of deltas from itself to the root path ). So I am stuck in such a strange circle for a long time.Last... I have a shameless Google question...Then I suddenly realized that... In fact, there is no need to set the Delta array, because every time two sets are merged, assuming that the root a of Set 1 becomes the child node of the root B of Set 2, then the before value of A is changed from 0 to the number of elements of Set 2, that is, the before value of node A is equal to the Delta recorded previously. Then, we can add before directly when accumulating. As for reading the number of elements in the Set, if no other array is set, traverse the tree to find the largest before value in the leaf node. The complexity can be imagined. The solution is to set a new sum array and add it when merging.

Lessons learned

Learn to discard the original method, even if the original algorithm has been debugged for a long time. In addition, if you have not been able to solve the problem for a long time, you can leave it for a long time or read the question. If you have been entangled in a question for a long time, it will often lead to irritability, affecting your mentality and inefficiency.

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.