Line Segment tree Summary 1 [go]

Source: Internet
Author: User
Data Structure: Line Segment tree]

Http://blog.csdn.net/wypblog/article/details/8219727

I. Basic concepts of line tree

A line segment tree is a binary search tree similar to an interval tree. It divides an interval into several unit intervals. Each unit interval corresponds to a leaf node in a line segment tree.
For each non-leaf node [a, B] In a line segment tree, its left son represents the interval [A, (A + B)/2], the range indicated by the right son is [(A + B)/2 + 1, B]. Therefore, a line segment tree is a balanced binary tree. The number of subnodes is N, that is, the length of the entire line segment.

You can use the line segment tree to quickly find the number of times a node appears in several line segments. The time complexity is O (logn ). The unoptimized space complexity is 2n. Therefore, it is sometimes necessary to compress the space by discretization.

Nature: The father's range is [a, B], (C = (a + B)/2) the left son's range is [a, c], the range of the right son is [C + 1, B]. The space required by the line segment tree is four times the size of the array.

2. Storage Data Structure of the Line Segment tree

From the figure above, we can see that the storage of a line segment tree is a bit similar to that of a binary tree. The left and right child nodes are required. In addition, to store the number of times each line segment appears, therefore, the counting element is usually added as follows:

1 struct node // line segment tree 2 {3 int left; 4 int right; 5 Int counter; 6} segtree [4 * border];

Left indicates the left endpoint, right indicates the right endpoint, counter indicates the number of occurrences of each line segment, and Borde indicates that the coordinate of the Line Segment endpoint cannot exceed 100. We can see from the above nature that we need 4 times of space for storage.

3. operations supported by the line segment tree

A line segment tree supports at least four operations:

    • Void construct (INT index, int lef, int rig) to build the line segment tree. The root node of the Line Segment tree starts to build the line segment tree with the interval [lef, rig ].
    • Void insert (INT index, int start, int end), insert a line segment [start, end] to the line segment tree, count the number of times at the same time
    • Int query (INT index, int X): The number of times X appears in the query point. From the root node to the number of times X appears in the path of the [x, x] leaf.
    • Void Delete _ (int c, int D, int index), delete a line segment from the line segment tree [c, d]

 

The procedure is as follows:

1. Create a line segment tree

/* Construct the root node of the Line Segment tree and start to build the line segment tree in the interval [lef, rig */
Void construct (INT index, int lef, int rig)
{
Segtree [Index]. Left = lef;
Segtree [Index]. Right = rig;
If (LEF = rig) // leaf node
{
Segtree [Index]. Counter = 0;
Return;
}
Int mid = (LEF + rig)> 1;
Construct (index <1) + 1, Lef, mid );
Construct (index <1) + 2, Mid + 1, rig );
Segtree [Index]. Counter = 0;
}

1/* construct the root node of the Line Segment tree and start to build the line segment tree of the [lef, rig] range */2 void construct (INT index, int lef, int rig) 3 {4 segtree [Index]. left = lef; 5 segtree [Index]. right = rig; 6 if (LEF = rig) // leaf node 7 {8 segtree [Index]. counter = 0; 9 return; 10} 11 int mid = (LEF + rig)> 1; 12 construct (index <1) + 1, Lef, mid ); 13 construct (index <1) + 2, Mid + 1, rig); 14 segtree [Index]. counter = 0; 15}
2. Insert elements of a line segment tree

/* Insert a line segment [start, end] to the line segment tree and count the number of times */
Void insert (INT index, int start, int end)
{
If (segtree [Index]. Left = Start & segtree [Index]. Right = end)
{
++ Segtree [Index]. counter;
Return;
}
Int mid = (segtree [Index]. Left + segtree [Index]. Right)> 1;
If (end <= mid) // left subtree
{
Insert (index <1) + 1, start, end );
} Else if (Start> mid) // right subtree
{
Insert (index <1) + 2, start, end );
} Else // split
{
Insert (index <1) + 1, start, mid );
Insert (index <1) + 2, Mid + 1, end );
}
}

1/* Insert a line segment [start, end] to the line segment tree and count the number of times */2 void insert (INT index, int start, int end) 3 {4 If (segtree [Index]. left = Start & segtree [Index]. right = END) 5 {6 ++ segtree [Index]. counter; 7 return; 8} 9 int mid = (segtree [Index]. left + segtree [Index]. right)> 1; 10 if (end <= mid) // left subtree 11 {12 insert (index <1) + 1, start, end ); 13} else if (Start> mid) // right subtree 14 {15 insert (index <1) + 2, start, end ); 16} else // split into 17 {18 insert (index <1) + 1, start, mid); 19 insert (index <1) + 2, mid + 1, end); 20} 21}
3. Search for line segment Tree Elements

/* Number of times point X appears
* From the root node to the path of the [x, x] leaf, the number of times X appears when the sum of some counts is X.
*/
Int query (INT index, int X)
{
If (segtree [Index]. Left = segtree [Index]. Right) // go to the leaf and return
{
Return segtree [Index]. counter;
}
Int mid = (segtree [Index]. Left + segtree [Index]. Right)> 1;
If (x <= mid)
{
Return segtree [Index]. Counter + query (index <1) + 1, x );
}
Return segtree [Index]. Counter + query (index <1) + 2, X );
}

1/* Number of times the query point X appears 2 * from the root node to [X, x] The sum of some counts in the path of the leaf is X number of occurrences 3 */4 int query (INT index, int X) 5 {6 if (segtree [Index]. left = segtree [Index]. right) // go to the leaf and return 7 {8 return segtree [Index]. counter; 9} 10 int mid = (segtree [Index]. left + segtree [Index]. right)> 1; 11 if (x <= mid) 12 {13 return segtree [Index]. counter + query (index <1) + 1, x); 14} 15 return segtree [Index]. counter + query (index <1) + 2, x); 16}
4. Delete the elements of a line segment tree

Void Delete _ (int c, int D, int index)
{
If (C <= segtree [Index]. Left & D> = segtree [Index]. Right)
Segtree [Index]. counter --;
Else
{
If (C <(segtree [Index]. Left + segtree [Index]. Right)/2) Delete _ (c, d, segtree [Index]. Left );
If (D> (segtree [Index]. Left + segtree [Index]. Right)/2) Delete _ (c, d, segtree [Index]. Right );
}
}


 1 void  delete_ (int c , int  d, int index) 2 { 3        if(c <= segTree[index].left && d >= segTree[index].right)  4            segTree[index].counter--; 5        else  6        { 7           if(c < (segTree[index].left + segTree[index].right)/2 ) delete_( c,d, segTree[index].left); 8           if(d > (segTree[index].left + segTree[index].right)/2 ) delete_( c,d, segTree[index].right); 9        }10 } 
Iv. Application of Line Segment tree
  • Maximum Range Query
  • Dynamic query for continuous interval modification or single-node update
  • Dynamic query of Multi-Dimensional Space
(For more information, see http://www.iteblog.com/archives/144. Do not use it for commercial purposes .)
Related Article

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.