Segment Tree Learning (i)

Source: Internet
Author: User

See UESTC data structure topic is about to end, feel that they really wasted a lot of time, no like Xin Aviation learning elder sister as told, tightly with live training.

So determined to seriously start learning the knowledge of the line segment tree, before the line-tree learning is smattering, that is, I only know that the line segment tree is used for single-point update and interval check value, in fact, the function of line tree is far more than these.

First of all, the line segment tree is used to solve the problem of the absolute magic weapon, why say absolutely forced it. Because it is able to complete each query in Logn time.

Categories of Queries:

1. Interval query

-Access certain properties of an interval (for example, maximum, minimum, continuous and, etc.)

2. Interval update

-Some operations affect a certain interval (for example, unify plus a value)

Attributed to the above two queries, we have summed up the following three issues that can be solved by the segment tree.

-Update point, query interval

-Update interval, query point

-Update interval, query interval

To get a deeper understanding of the segment tree, let's start with a more classic example.

I now have a one-dimensional array of length n (a[1]~a[n])

We only allow the following actions for this array at a time:

To. Modify the value of an element in an array

-[1,5,4,1,6]---(a[2] = 3)---[1,3,4,1,6]

The maximum value of an interval in an array is queried.

-[1,5,4,1,6]---(max (1,4) =?)---> 5

To inquire about an interval in an array.

-[1,5,4,1,6]---(sum (3,5) =?)---> 11

Before we knew the line tree, we knew that it was very simple to do such an operation on an array, and each time the O (n) time was scanned, the ANS of the various queries could be obtained.

So, what if I'm going to ask Q? This becomes the complexity of O (NQ), rub, this is too slow, give you a set of data, you are T

So, in order to solve these query problems in a faster time, we introduced a data structure called segment tree, which is one that can complete each operation in Logn time.

The nature of line segment tree is a binary tree, different from the general two-fork tree, each node of the segment tree maintains a section of information.

For example, the following binary tree, for a length of 5 array a[1]~a[5]

[1,.5]

[1,3] [4,5]

[3,3] [+] [5,5] for the construction of this tree has a few notes, for any non-leaf node, if it maintains the interval is [L,r], its left son area

[2,2] is [L, (L+R)/2], the right son interval is [(l+r)/2+1,r];

---Implementation of segment tree

Information logged by each node

1 struct Segtree 2 {3     int left,right; // End of Interval 4     int mx,mn; // maximum and minimum values within a range 5     int sum; // sum of elements within a range 6 }tree[max*4];

We first use an array a[] to record the node, and the root node subscript is 1, then, for any one node a[k], his left son is a[k<<1], the right son is a[k<<1|1]

Create a line segment tree code implementation

voidBuild (intIdintLintR) {Tree[id].left= L; Tree[id].right =R; if(l==r) {tree[id].sum= tree[id<<1].sum+tree[id<<1|1].sum; Tree[id].mx= Max (tree[id<<1].mx,tree[id<<1|1].mx); Tree[id].mn= Min (tree[id<<1].mn,tree[id<<1|1].mn); }    Else    {        intMid = (l+r) >>1; Build (ID<<1, L,mid); Build (ID<<1|1, mid+1, R); Tree[id].sum= tree[id<<1].sum+tree[id<<1|1].sum; Tree[id].mx= Max (tree[id<<1].mx,tree[id<<1|1].mx); Tree[id].mn= Min (tree[id<<1].,mn,tree[id<<1|1].mn); }}

Segment Tree Learning (i)

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.