Explanation--line tree

Source: Internet
Author: User

explanation--line tree

O, cited examples

A. Give the number of N, n<=100, and M queries, each query interval [l,r] and output.

One answer: This is too simple, O (n) Enumerate the search on the line.

Another answer: the O (N) enumeration is also useful, and the prefix and O (1) are done.

Well, I'll revise the subject again.

B. Give the number of N, n<=100, and M operations, each operation may have two kinds: 1, add a number in a position, 2, ask the interval [l,r] and output.

Answer: O (N) enumeration.

Dynamic modification cannot be done with static prefixes at least.

OK, I'll revise the topic again:

C. Give the number of N, n<=1000000, and M operations, each operation may have two kinds: 1, add a number in a position, 2, ask the interval [l,r] and output.

Answer: O (N) enumeration absolute timeout.

So what? This requires a powerful data structure: The segment Tree .

First, the basic concept

1, line tree is a binary search tree, it stores a range of information.

2. Each node is stored in a structured way, and the structure contains the following information:

Interval left end, right end, (both must have)

The information to be maintained in this interval (the actual situation depends on the number of different).

3, the basic idea of segment tree: two points .

4, Segment tree general structure:

5. Special properties:

By the available,

1. The left child interval range for each node is [L,mid] and the right child is [mid+1,r]

2, for node K, left child node for 2*k, right child for 2*k+1, which conforms to the nature of the complete binary tree

Second, the base operation of the segment tree

Note: The following basic operations are examples of summing in an example, as an example of a struct:

struct node
{
The int l,r,w;//l,r represents the left and right ends of the interval, w indicates intervals and
}TREE[4*N+1];

There are 5 basic operations for the segment tree:

Achievements, single-point query, single-point modification, interval query, interval modification.

1, build a tree line

① main idea:A, for the two points to each node, to its left and right endpoints to determine the range.

b, if it is a leaf node, store the information to be maintained.

C, State Merge.

② Code

voidBuildintLintRintk) {TREE[K].L=l;tree[k].r=R; if(L==R)//leaf node{scanf ("%d",&TREE[K].W); return ; }    intM= (L+R)/2; Build (L,m,k*2);//left childBuild (m+1, r,k*2+1);//Right Childtree[k].w=tree[k*2].w+tree[k*2+1].W;//State merge,The w= of this junction two children of W}

③ Note

A. The structure to open 4 times times the space , the specific reasons have not yet understood, remember it is good.

B. Do not miss the return statement, because the leaf node does not need to continue to recursion.

2, a single point of inquiry, that is, to query the state of a point, set to query point x

① main idea: with the binary query method is basically consistent, if the current enumeration of points around the same endpoint, that is, the leaf node, is the target node. If not, because this is the dichotomy, so set the query position to x, the current node interval range in order to L,r, midpoint for mid, then if X<=mid, then recursion its left child, otherwise recursion its right child

② Code

voidAskintk) {    if(TREE[K].L==TREE[K].R)//the left and right endpoints of the current node are equal, the leaf nodes, the final answer .{ans=TREE[K].W; return ; }    intM= (TREE[K].L+TREE[K].R)/2; if(x<=m) Ask (k*2);//the target position is closer to the left than the midpoint, and the left child    ElseAsk (k*2+1);//Conversely, recursive right child}

③ Correctness Analysis:

Because if it is not the target location, the target location is determined by the If-else statement, the target range is narrowed down, and finally the target leaf node must be reached.

3, a single point of modification, that is, change the state of a point. Using the example in the example, add Y to the number of X

The main idea of ①

Based on the principle of single-point query, the position of X is found, and the state of each node is modified according to the principle of merging the state of achievement.

② Code

voidAddintk) {    if(TREE[K].L==TREE[K].R)//Find the target location{TREE[K].W+=y; return; }    intM= (TREE[K].L+TREE[K].R)/2; if(x<=m) Add (k*2); ElseAdd (k*2+1); TREE[K].W=tree[k*2].w+tree[k*2+1].W;//All node status updates that contain node K}

4, interval query, that is, query the state of an interval, in the example for the query interval [x, y] and

The main idea of ①

② Code

SUM (int  k) {    if(tree[k].l>=x&&tree[k].r<=y)     {        ans+ =tree[k].w;         return ;    }     int m= (TREE[K].L+TREE[K].R)/2;     if (x<=m) sum (k*2);     if (y>m) sum (k*2+1);}

③ Correctness Analysis

Situation 1,3 Needless to say, for Case 2, the worst case is to search the leaf node, at this time must meet the situation 1

5, the interval modification, the time reason, later to fill up

Now go back to the question in the example

First, enter N, then the number of N,

And then enter M,

Re-enter M inquiry, ask format: p,x,y

P=1, indicating the number of x +y

P=2 that represents the query and output interval [x, y] and

p=3, query and output the value of point x

The code is as follows

#include <cstdio>using namespacestd;intN,m,p,x,y,ans;structnode{intL,r,w;} tree[200001];inlinevoidBuildintLintRintk) {TREE[K].L=l;tree[k].r=R; if(L==R)//leaf node{scanf ("%d",&TREE[K].W); return ; }    intM= (L+R)/2; Build (L,m,k*2);//left childBuild (m+1, r,k*2+1);//Right Childtree[k].w=tree[k*2].w+tree[k*2+1].W;//The w= of this junction two children of W}inlinevoidAddintk) {    if(TREE[K].L==TREE[K].R)//Find the target location{TREE[K].W+=y; return; }    intM= (TREE[K].L+TREE[K].R)/2; if(x<=m) Add (k*2); ElseAdd (k*2+1); TREE[K].W=tree[k*2].w+tree[k*2+1].W;//All node status updates that contain node K}inlinevoidAskintk) {    if(TREE[K].L==TREE[K].R)//the left and right endpoints of the current node are equal, the leaf nodes, the final answer .{ans=TREE[K].W; return ; }    intM= (TREE[K].L+TREE[K].R)/2; if(x<=m) Ask (k*2);//the target position is closer to the left than the midpoint, and the left child    ElseAsk (k*2+1);//Conversely, recursive right child}inlinevoidSumintk) {    if(tree[k].l>=x&&tree[k].r<=y) {ans+=TREE[K].W; return; }    intM= (TREE[K].L+TREE[K].R)/2; if(x<=m) sum (k*2); if(y>m) sum (k*2+1);}intMain () {scanf ("%d",&N); Build (1N1); scanf ("%d",&m);  for(intI=1; i<=m;i++) {scanf ("%d%d%d",&p,&x,&y); Ans=0; if(p==1) Add (1); Else if(p==2) {sum (1); printf ("%d\n", ans); }        Else{Ask (1); printf ("%d\n", ans); }    }}

Iii. reference topics

1, codevs1080 line tree Practice http://codevs.cn/problem/1080/

Not to be continued

Explanation--line tree

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.