Segment tree)

Source: Internet
Author: User

1. Summary The Line Segment tree, also called the Interval Tree, is a complete binary tree. It stores a line segment (that is, a "subarray") on each node and is often used to solve the series maintenance problem, the complexity of each operation is basically O (lgN ). 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. URL: http://www.cnblogs.com/archimedes/p/segment-tree.html. 2. Basic operations of a line segment tree include constructing a line segment tree, interval query, and interval modification. (1) line tree construction first introduces the method of constructing line tree: Let the root node represent the range [0, N-1], that is, a range composed of all N number, then, divide the interval into two halves, represented by left and right subtree. It is not hard to prove that such a line segment tree has only 2N-1 nodes at the O (N) level. The node definition is as follows: typedef struct node {int l; // The left endpoint int r of the line segment; // The left endpoint int value of the line segment; // The value on the Line Segment} node; build bulid of the Line Segment tree // build the tree using node v as the root, and the corresponding v range is [l, r] {initialize if (l! = R) {build on the root of the left child of v, range: [l, (l + r)/2] Build on the root of the right child of v, the complete build code for the interval [(l + r)/2 + 1, r]} is as follows: # define N 10000 node tree [N]; void bulid (int l, int r, int v) // establish node v with the interval of l ~ R {tree [v]. l = l; tree [v]. r = r; if (l = r) {// initialize the node tree [v]. value = a [r]; return;} int mid = (l + r)/2; bulid (v * 2, l, mid); bulid (v * 2 + 1, mid + 1, r); // update the current node tree based on the left and right sons [v]. value = tree [v * 2]. value + tree [v * 2 + 1]. value;} updated when a [I] ~ When a value c is added to all the elements in a [j], if a [I] ~ When a [j] is still a complete segment, add the value of this segment directly to c * (r-l + 1). When the updated range is not a complete segment, an incremental record method is used: add a field: int add for each node, and record the incremental c of the update operation. The value of add is 0 at the initial time. For example, when ~ 5. After the interval is updated, add a value c to the add of the node, and then the next secondary pair 2 ~ When the three nodes are updated or queried, add is passed to the following child node to complete the update Tree Code: typedef struct node {int l; // The left endpoint int r of the line segment; // The left endpoint int value of the line segment; // The value int add;} node; void update (int v, int r, int l, int m) // update the range l ~ R plus the number m {if (tree [v]. l = l & tree [v]. r = r) {// locate, update and record the incremental tree [v]. value + = m * (r-l + 1); tree [v]. add = m; return;} if (tree [v]. add) {tree [2 * v]. add + = tree [v]. add; tree [2 * v + 1]. add + = tree [v]. add; tree [v]. add = 0;} int mid = (tree [v]. l + tree [v]. r)/2; if (r <= mid) {update (v * 2, l, r, m ); // update only to the left son} else {if (l> mid) {update (v * 2 + 1, l, r, m ); // update only to the right son} else {// The interval spans the left and right son intervals, and both of them are Update (v * 2, l, mid, m); update (v * 2 + 1, mid + 1, r, m) ;}} query range l ~ Value void query (int v, int l, int r) on r // The current query node is v and the range to be queried is l ~ R {if (tree [v]. l = l & tree [v]. r = r) {ans + = tree [v]. value; return;} if (tree [v]. add) {tree [v * 2]. add + = tree [v]. add; tree [v * 2 + 1]. add + = tree [v]. add; tree [v]. add = 0;} int mid = (tree [v]. l + tree [v]. r)/2; if (r <= mid) {query (v * 2, l, r ); // The range to be queried is in the left son} else {if (l> mid) {query (v * 2 + 1, l, r ); // The range to be queried is in the left son} else {// The range to be queried spans the left and right children query (v * 2, l, mid ); query (v * 2 + 1, mid + 1, r) ;}} programming practices poj2182 Lost Cows description # include <stdio. h> # include <stdlib. h ># define N struct int small [N], ans [N]; struct segment {int lc, rc, len ;}; struct segment s [3 * N]; void bulid (int root, int lc, int rc)/* */{s [root]. lc = lc; s [root]. rc = rc; s [root]. len = rc-lc + 1; if (rc = lc) return; bulid (2 * root, lc, (lc + rc)/2 ); bulid (2 * root + 1, (lc + rc)/2 + 1, rc);} int query (int root, int k) {s [root]. len --; if (s [root]. lc = s [root]. rc) return s [root]. lc; else if (k <= s [2 * root]. len) {return query (2 * root, k);} else {return query (2 * root + 1, k-s [2 * root]. len) ;}} int main () {int n, I; scanf ("% d", & n); for (I = 2; I <= n; I ++) scanf ("% d", & small [I]); small [1] = 0; bulid (1, 1, n); for (I = n; i> = 1; I --) ans [I] = query (1, small [I] + 1); for (I = 1; I <= n; I ++) printf ("% d \ n", ans [I]); return 0;

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.