Tree-like array basics

Source: Internet
Author: User
Tags abstract definition

Source: http://www.cnblogs.com/xxx0624/archive/2012/07/27/2612364.html#undefinedNo. 01, what is a tree-like array?

The tree-like array is used to find the interval element and the time efficiency of the interval element is O (logn).

Some of the classmates feel very strange. Using an array s[i] to save the first I element of the sequence a[], then the element of the interval i,j and not the s[j]-s[i-1], then the time efficiency is O (1), Wouldn't it be faster?

But what if the a[of the title] will change? For example:

Let's define the following questions: We have n boxes. Possible exercises as

1. Add Stones to Box K

2. Query the total number of stones from box I to Box J

The natural solution has a time complexity of 1 O (1) for operation and an O (n) for Operation 2. But in a tree-like array, the time complexity of operations 1 and 2 is O (Logn).

No. 02 Diagram tree-like array c[]

Now, what do you mean by the tree-like array? Assumed sequence is a[1]~a[8]

There's a graph on the network, but I've made a 2-point improvement to this diagram.

(1) There is a tree full of two forks, each node of a two-fork tree corresponds to an element in a[].

(2) C[i] The highest node of the column corresponding to A[i].

Now tell you: Sequence c[] is a tree-like array.

So c[] How to find?

C[1]=A[1];

C[2]=A[1]+A[2];

C[3]=A[3];

C[4]=A[1]+A[2]+A[3]+A[4];

C[5]=A[5];

C[6]=A[5]+A[6];

C[7]=A[7];

C[8]= A[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8];

The above just enumerates all the cases, then generalize to the general situation and get an abstract definition of c[i]:

Because each element in a[] corresponds to each leaf of a two-forked tree, we simply take each element of a[] as a leaf, and then: All the leaves of c[i]=c[i].

Now we have to elicit a rule about binary:

First Look carefully:

Decimal into binary, then observe the position of the rightmost 1 of these binary numbers:

1--00000001

2--00000010

3--00000011

4--00000100

5--00000101

6--00000110

7--00000111

8--00001000

1 of the position in fact from my picture of the full two tree can be seen. But what does this have to do with c[]?

The next part of the content is important:

In a tree full of two forks,

Those nodes ending in 1 (c[1],c[3],c[5],c[7]) have 1 leaves, so these nodes c[i] represent elements with a range of 1;

Those nodes ending in 10 (c[2],c[6]) have a number of 2 leaves, so these nodes c[i] represent elements with a range of 2;

Those nodes ending in 100 (c[4]) have a number of 4 leaves, so these nodes c[i] represent elements with a range of 4;

Those nodes ending in 1000 (c[8]) have a number of 8 leaves, so these nodes c[i] represent elements with an interval range of 8.

Extended to the general situation:

In the binary of I, there is a continuous x "0" in the right-to-left number, and a 2^x leaves, which is the a[of the i-2^x+1 to the first element of the sequence.

Finally, we get a specific definition of a c[i]:

C[i]=a[i-2^x+1]+...+a[i], where x is the number of consecutive "0" in the binary of I from right to left.

No. 03, using a tree-like array to find the first and the first elements of the s[i]

After understanding C[i], the first element and S[i] are easy to implement.

Starting from the definition of c[i]:

C[i]=a[i-2^x+1]+...+a[i], where x is the number of consecutive "0" in the binary of I from right to left.

We can know: C[i] is certainly including A[i], then:

s[i]=c[i]+c[i-2^x]+ ...

Perhaps the above formula is too abstract, because there are ellipses, we take a concrete example to see:

S[7]=C[7]+C[6]+C[4]

Because C[7]=a[7],c[6]=a[6]+a[5],c[4]=a[4]+a[3]+a[2]+a[1], so s[7]=c[7]+c[6]+c[4]

(1) i=7, to obtain x=0, then we have obtained a[7];

(2) I=i-2^x=6, to obtain x=1, then obtained a[6]+a[5];

(3) i=i-2^x=4, to obtain x=2, then obtained a[4]+a[3]+a[2]+a[1].

This is actually a bit difficult, because s[i], if you want to speak clearly, then have to write too many things. So do not understand the classmate, and then repeatedly read a few times.

From (1) (2) (3) These 3 steps can be known, s[i] is an accumulation of the process, if the 2^x to find out, then this process in C + + implementation is no difficulty.

I'll tell you straight now. Conclusion:2^x=i& (-i)

Proof: Set a ' for the binary counter code of a, I binary representation into A1B, where a regardless, B is the whole 0 sequence. So-i=a ' 0B ' +1. Since B is a full 0 sequence, then B ' is the full 1 sequence, so-i=a ' 1B, so:

i& (-i) = a1b& A ' 1b=1b, which is the value of 2^x.

So according to the process of (1) (2) (3) We can write the following function:

int Sum (int i)//return first element and

{

int s=0;

while (i>0)

{

S+=c[i];

i-=i& (-i);

}

return s;

}

No. 04 Talk Update c[]

As mentioned in the No. 01 question of the small stone, if the array a[i] is updated what to do? So how do I change c[]?

If change c[] also requires O (n) time complexity, then the tree array has no advantage. So the time efficiency of the tree array in changing c[] is O (logn), why?

Because change A[i] only need to change part of the c[]. This can be seen from the No. 02 figure:

Such as:

If a[3]=3, then A[3]+=1, then which c[] need to change?

The answer can be drawn: c[3],c[4],c[8]. Because these values are associated with a[3], their relationship with the tree is described as: c[3],c[4],c[8] is the ancestor of A[3].

So how do you know those c[] need to change?

Let's look at the junction of "A". This "a" node is very important because he embodies a relationship: A has twice times the number of leaves C[3]. Because the number of leaves in the Saozi right subtree of "A" is the same. Because 2^x represents the number of leaves, so c[3] father is A,a's father is c[i+2^0], that is, c[3] change, then c[3+2^0] also changed.

Let's take a look at the node "B". The number of leaves of the B-node is twice times c[6]. So B and c[6+2^1] in the same column, so c[6] changes, c[6+2^1] also change.

The general situation is:

If a[i] changes, then c[i] changes, C[i's father C[i+2^x] also changed.

The iterative process of this line, we can write that when a[i] changes, the update function of c[] is:

void Update (int i,int value)//a[i] is changed to value

{

while (i<=n)

{

C[i]+=value;

i+=i& (-i);

}

}

The No. 05 One-dimensional tree-like array application example

4 of the words, we finally took a one-dimensional tree array of 2 less than 5 lines of code to be done. Now it's time to get into the application formally.

Title Link: http://poj.org/problem?id=2352

Test instructions: The coordinates of n stars are given in Y ascending order, if the x, y coordinates of the M stars are less than or equal to the coordinates of star A, then star A is rated M.

Analysis: is a tree-like array problem. For example, the following is the input to the topic:

5

1 1

5 1

7 1

3 3

5 5

Because the y-coordinate is ascending and the coordinates are not duplicated, the x, y coordinates of the stars entered after star A cannot be less than or equal to star a. If the current input star is (3,3), it is easy for us to find a tree-like array of less than or equal to 3 of the value can be, that is, Getsum (3). Note: A[i] represents the number of x coordinates I, c[] is a tree array of a[], then getsum (i) is the sum of the first elements in the sequence, that is, X is less than or equal to the number of stars.

The point is to note that the star coordinate input can be (0,0), so we put the X coordinate 1, and then implemented with a tree-like array.

No. 06 Two-dimensional tree-like array

Bit can be used as a two-dimensional data result. Suppose you have a plane with a point (with non-negative coordinates). You have three questions:

1. Set point at (x, y)

2. Remove points from (x, y)

3. Calculate the number of points in the rectangle (0, 0), (x, Y)-where (0, 0) is the lower-left corner, (x, y) is the upper-right corner, and the edge is parallel to the x-axis and y-axis.

For the 1 operation, set the point at (x, y), i.e. update (x,y,1), what is the update to write? Very simple, because the x, Y coordinates are discrete, so we update the x, Y, respectively, the function is as follows:

void Update (int x,int y,int val)

{

while (x<=n)

{

int y1=y;

while (y1<=n)

{

C[x][y1]+=val;

y1+=y1& (-Y1);

}

x+=x& (-X);

}

}

Then according to the update can be pushed: the Getsum function is:

int getsum (int x,int y)

{

int sum=0;

while (x>0)

{

int y1=y;

while (y1>0)

{

SUM+=C[X][Y1];

y1-=y1& (-Y1);

}

x-=x& (-X);

}

return sum;

}

The No. 07 Application example of two-dimensional tree-like array

Title Link: http://poj.org/problem?id=2155

Let us first discuss the one-dimensional situation of POJ2155, as follows:

There is an array of n cards. Each card is placed upside down on the desktop. You have two questions:

1. T I j (reverse the card from index I to index J, including section I Zhang and J equiped-face down card will face up; face up card will face down)

2. Q I (If I card face down answer 0 otherwise answer 1)

Solve:

The method of solving the problem (1 and 2) has time complexity O (log n). In the array f (length n + 1) We store each question T (i, j)--we set f[i]++ and F[j + 1]--。 To between I and J (including I and j) each card K sum f[1] + f[2] + ... + f[k] will increment by 1, all the rest is the same as the previous (see Figure 2.0 clearly), our results will be described as and (and cumulative frequency) modulo 2.

Figure 2.0

Use bit to store (increase/decrease) frequency and read the cumulative frequency.

Understanding the one-dimensional situation, POJ2155 is the second dimension of the version, easy to only need more (X1,y1), (x1,y2+1), (X2+1,y1), (x2+1,y2+1) four points c[] value is OK, the final result is still getsum (x, y)%2

Tree-like array basics

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.