Tree-like array learning

Source: Internet
Author: User

reprinted from: http://blog.csdn.net/int64ago/article/details/7429868

Write down this title, in fact, there is no end of the heart, not so much as to write a blog post, rather than to do a summary. The first contact tree array or two years ago, in what language to describe the feeling at that time? ...... It's amazing! Really, can't express that feeling, she is so elegant, 10 lines of code, but do things so well! No understanding of her principles under the premise of even the code recite can not understand! Among them, I have never understood the use of her. After two years, and have no intention to meet her, may be two years of code experience accumulation, have some new understanding, can confidently say understand it! I'm trying to get more people to understand her in my own way, instead of reciting her. In order to be more convenient to explain, the article will impose some of its own concepts, just for better understanding, not what professional terms and so on.

What is a tree-like array?

Usually we will encounter a number of arrays to maintain the operation of the query, more common such as, modify the value of a point, to find an interval of the and these two are exactly the strength of the tree array! Of course, when the data size is very small, it is very easy to modify the value of a point, the complexity is O (1), but for the sum of an interval to sweep again, the complexity is O (N), if the real-time array of M modified or summed, the worst case complexity is O (m*n), This is not going to come when the scale increases! and the tree array to do the same thing the complexity is O (M*LGN), do not underestimate this LG, a large number of LG is very small, this study of mathematics know it, do not need me to say. To affirm, look at the following article must not be urgent, only need to understand each step at the end of the natural understanding.

How did the tree-like array do?

First look at two pictures (online looking, if the same, do not make a fuss ~), the following instructions are based on these two pictures, the left is called a graph bar, the right side is called B figure:

Does it look like a tree? Yes, this is why it is called a tree-like array ~ First Look at a, an array is the array we want to maintain and query, but in fact we do not use the whole process of a array, you can think of it as a decoration! The C array is the center of attention and manipulation of our whole process. Let's take a look at the rules of the C array first, where C8 = C4+c6+c7+a8,c6 = C5+a6 ... Do not have to tangle how to do, we just know the approximate rules of the C array can be, it is easy to know that C8 represents the A1~a8 and, but C6 is to show A5~a6 and, why will it produce such a difference? Or why the people who invented her were treated differently? The answer is, this will make the operation easier! See this believe some people have some feeling, why is the complexity of LG? As can be seen, C8 can be regarded as the left half of a1~a8 and + right half Bien Hoa, while the left half and is determined by the C4, the right half is actually the same rule to divide a5~a8 into two ... Continue to be divided into a split until not, you can look at the B chart. What do you think? Is it a bit of a two-point taste? Yes, the tree-like array is a clever use of two points, she is not mysterious, the key is her ingenious!

How did she ever do it in the same way? Say this before I say a thing called Lowbit, Lowbit (k) is the K of the binary high 1 all empty, leaving only the lowest bit of 1, such as 10 of the binary is 1010, then Lowbit (k) =lowbit (1010) =0010 (2 binary), In this lowbit is often used in the following, here is a very convenient way to implement, the more common method Lowbit (k) =k&-k, this is a bitwise operation, we know that a number plus a minus sign is the binary of this number is reversed +1, such as 10 of the binary is -1010= 0101+1=0110, and then with 1010&0110, the answer is 0010! Understand the method of solving lowbit can be, continue below. In the following discussion of the decimal has no meaning (the world is a binary, the person should be subjective to build a decimal), the following all the number is not specifically described as a binary.

Above so many words say lowbit, haven't said its use, it is to contact a array and C array! CK said from the AK start to the left to continuously seek lowbit (k) Number of the and, for example, c[0110]=a[0110]+a[0101], that is, starting from 110 calculated 0010 number and, because Lowbit (0110) = 0010, you can see actually only low 1 plays a role, Because it's obviously possible to write c[0010]=a[0010]+a[0001], that's why any number of us cares only about its lowbit, because the high does not work (it must be so, based on our dichotomy)! ), unless the rest of the high-level is 0, then itself is lowbit.

Since the relationship is established, and see how to achieve a a certain location data and change, she will not directly change (beginning to say, a does not exist), she each change in fact to maintain the C array should be the nature, because the sum to be used later. and maintenance is very simple, such as changing the a[0011], we have to modify the c[0011],c[0100],c[1000], it is very easy to see from the picture, but you may ask, they have a declaration of inevitable contact? Every time the solution is not always take the picture to see it? In fact, from the 0011-->0100-->1000 changes are carried out "to tail" operation, but also their own creation of the word-", I would like to explain that the tail should be removed from the 1 are removed to the higher 1, remember that each transformation must have a high 1 generation, So 0100 is not able to change to 0101, because there is no new high 1 generation, the transformation process happens to be able to use our lowbit, K +=lowbit (k).

Well, now that the updates are in order, there may be a new question: Why does it have to be this kind of relationship? This is to be investigated before we say C8 can be regarded as the left half of the a1~a8 and the + right half and ... Of the content, why c[0011] will affect the c[0100] without affecting c[0101], which is said before c[0100] the solution is actually such a segmented interval c[0001]~c[0001] and interval c[0011]~c[0011] and, the number is too small, Perhaps this is not very understanding, in such as c[0100] will affect c[1000], why? Because c[1000] can be regarded as 0001~0100 and plus 0101~1000, but the number of 0101-bit changes and will directly affect the c[1000], because its tail 1 can not be in the jump two in the production two times high 1, is through the c[0110] indirect influence, but, c[ 0100] But can jump one level to produce a high 1.

Perhaps the above said you are more around, then you only need to pay attention to: C of the constituent nature (in fact, the group nature) determines c[0011] only directly affect the c[0100], and c[0100] only directly affect [1000], and the relationship between the following table is also must be K +=lowbit (k). At this point we are writing the code with the new maintenance tree:

void Add (int k,int  num) {    while (k<=N)    {        tree[k] + =num;        K+=k&-k;    }}

With the foundation above, it is easier to say the sum. For example, the 0001~0110 and directly c[0100]+c[0110], the analysis method and the above is reversed, and the wording is reversed, specifically not tired of:

int Read (int k)//1~k interval and {    int sum=0;      while (k)    {        sum+ =Tree[k];        K-=k&-k;    }     return sum;}
Three, a summary of it

First, understand that the tree array is white and is grouped by two pairs of arrays; maintenance and querying are LGN complexity, and complexity depends on the worst case, and also O (LGN); Lowbit here is just a trick, the key is to understand the C array of the composition of the law; Analysis of the process of the binary system must be rooted in, As in the mind of the decimal.

Tree-like array learning

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.