Getting the tree line the night sky bright star

-------------------------------------------- not can not do the question, since I was determined to become a Piece, if this has died, then it does not matter.

Following a previous section, the line continues to explore Shuya!

How to use the tree line interval statistics?

It is assumed that the number 13 is 1,2,3,4,1,2,3,4,1,2,3,4,1. (A [1] = 1, A [2] = 2, ... ... a [13] = 1) after the section of the section marked with numbers and:

 

 

To calculate [2, 12] and, as previously algorithm:

 

[2,12]=[2] + [3,4] + [5,7] + [8,10] + [11,12]

 

  29  = 2 + 7 + 6 + 7 + 7

 

5 and the calculation can be calculated from the number of [2,12] value.

 Hee hee, here's another question:

How to modify the point?

Pictured above a basis for discussion:

Assume that the A [6] + = 7, take a look at what intervals need to be modified? [6], [5,6], [5,7], [1,7], [1,13] all these intervals need +7. All remaining sections do not move.

Thus, Fengyun segment tree, the modified point modified segment tree up to five elements (one for each layer).

Below, the modified elements are shown in blue.

 

One fought three thousand miles, and to pursue the storage structure of the tree line:

Segment tree is a binary tree, of course, can be written as a general structure such as a tree, what pointers.
But its advantage is that it can also be implemented with a tree structure array, the code can be greatly simplified.
An array suitable for use in programming competitions, under tree line already know the maximum size, the array directly open enough space, and then build on top of the tree line.
Simple notation: space = four times a sufficient array of size n.
In fact, enough space = (n expanded up to a nearest power of 2) twice.
举例子:假设数组长度为5,就需要5先扩充成8,8*2=16.线段树需要16个元素。如果数组元素为8,那么也需要16个元素。
所以线段树需要的空间是n的两倍到四倍之间的某个数,一般就开4*n的空间就好,如果空间不够,可以自己算好最大值来省点空间。
 
怎么用数组来表示一颗二叉树呢?假设某个节点的编号为v,那么它的左子节点编号为2*v,右子节点编号为2*v+1。
然后规定根节点为1.这样一颗二叉树就构造完成了。通常2*v在代码中写成 v<<1 。 2*v+1写成 v<<1|1
 
 

Guess you like

Origin www.cnblogs.com/dragondragon/p/11241773.html