Tree array and line segment tree

Source: Internet
Author: User

Tree array and line segment tree

I. Tree Array

During the problem solving process, we sometimes need to maintain the prefix of an array and S [I] = A [1] + A [2] +... + A [I]. But it is not hard to find that if we modify any A [I], S [I], S [I + 1]... S [n], it will change. It can be said that after each modification of A [I], it will take O (n) Time to adjust the prefix and S. When n is very large, the program runs very slowly. Therefore, here we introduce the "tree array". Its Modification and summation are O (logn), which is very efficient.

 

Implementation:

For a positive integer x, define lowbit (x) as the value of the rightmost 1 in the Binary Expression of x.

Lowbit (x) = x and-x for node I, if it is the left subnode, its parent node is I + lowbit (I );

Construct an auxiliary array C, where Ci = Ai-lowbit (I) + 1 + Ai-lowbit (I) + 2 +... + Ai means that each element of C is A continuous sum in array. Specifically, each gray node I belongs to a horizontal bar ending with itself. The sum of the numbers in this bar is Ci. For example, C12 = A9 + A10 + A11 + A12 = C10 + A11 + A12

 

How to update elements in array C: if an Ai is modified, which elements need to be updated in array C? Start from Ci and go to the right, and "Climb up" while walking (not necessarily along the side of the tree). Modify the corresponding Ci of all nodes along the way. During preprocessing, the C array is cleared and n add operations are executed. Total time is O (nlogn)

 

How to calculate the prefix and Si: Follow node I to the left, and "Climb up" while walking (not necessarily along the side of the tree), and accumulate the Ci that passes along the way. For query (L, R) = SR-SL-1.

 

Code:

Var

B, c, f: array [0 .. 100000] of longint;

Ff, a: Array [0 .. 100000] of boolean;

I, j, m, n, x, y, ans, l, r, tmp: longint;

S: string;

 

Function dfs (x: longint): longint;

Begin

If x <= 1 then

Exit;

C [f [x]: = c [f [x] + c [x];

Dfs (f [x]);

End;

 

Procedure dfs1 (x: longint );

Begin

Dec (c [x]);

If x <= 1 then

Exit;

Dfs1 (f [x]);

End;

 

Procedure dfs2 (x: longint );

Begin

Inc (c [x]);

If x <= 1 then

Exit;

Dfs2 (f [x]);

End;

 

Begin

Readln (n );

Fillchar (ff, sizeof (ff), true );

For I: = 1 to n-1 do

Begin

Readln (x, y );

F [y]: = x;

Inc (B [x]);

End;

For I: = 1 to n do

C [I]: = 1;

For I: = 1 to n do

Begin

If B [I] = 0 then

Dfs (I );

End;

Readln (m );

For I: = 1 to m do

Begin

X: = 0;

Readln (s );

If s [1] = 'q' then

Begin

For j: = 3 to length (s) do

X: = x * 10 + ord (s [j])-ord ('0 ');

Writeln (c [x]);

End;

If s [1] = 'C' then

Begin

For j: = 3 to length (s) do

X: = x * 10 + ord (s [j])-ord ('0 ');

If ff [x] then

Dfs1 (x) else

Dfs2 (x );

Ff [x]: = not ff [x];

End;

End;

End.

 

 

Ii. Line Segment tree

1. Structure of the Line Segment tree:

Range: Use a pair of numbers a and B to represent a range [a, B) that is open before and after a closure ). (You can modify it yourself) node T (a, B): indicates that the node maintains the information of the range [a, B) in the original series, where a and B are integers and 1, then T (a, (a + B)/2) is the left child node of T (a, B), T (a + B)/2, B) right child of T (a, B)

Leaf node: If the leaf node T (a, B) has B-a = 1, the tree structure of the leaf node line segment is recursively defined.

 

2. The nature of the Line Segment tree:

Number of Knots: assuming that the length of the series processed by the line segment tree is n, that is, the root node range is [1, n + 1), the number of summed points cannot exceed 2 * n. Depth: The line segment tree can be considered as a full binary tree, so the depth cannot exceed the order of magnitude of Line Segment decomposition in log (2 * n: the line segment tree divides any line segment with a length of L in the interval into a line segment of no more than 2 logl, which enables most queries to be solved within the O (logn) time.

Storage of line tree:

1. Linked List Storage

2. simulate a linked list using Arrays

3. Heap Structure Storage

 

 

Application:

Loyal)

[Problem description]

Old manager is a smart and competent person. He has been working for the financial owner for 10 years, and the financial owner wants to make his account clearer. The manager is required to keep k accounts every day. Because the manager is smart and competent, the manager always makes the manager very satisfied. However, some people have doubts about the manager because of their provocation. So he decided to use a special method to judge the loyalty of the butler. He pressed every account to 1, 2, 3... Number, and then occasionally ask the manager's question, the question is: what is the smallest sum of Accounts a to B? In order to prevent the butler from committing frauds, he always asks multiple questions at a time.

The account book content may be modified during the inquiry.

[Input format]

In the input, the first row has two numbers of m, n indicates m (m <= 100000), n indicates n problems, and n <= 100000.

Next, there are three numbers for each behavior. The first p is the number 1 or number 2, the second is x, and the third is y.

If p = 1, query the x and y intervals.

If p = 2, the number of x is changed to y.

[Output format]

The output file contains the answer to each question. For details, see the example.

[Input example]

10 3

1 2 3 4 5 6 7 8 9 10

1 2 7

2 2 0

1 1 10

[Output example]

2 0

 

Code:

Type

Point = ^ node;

Node = record

Left, right: longint;

Lp, rp: point;

Sum: longint;

End;

 

Var

P: array [1 .. 100000] of node;

I, j, m, n, x, y, k: longint;

A: array [1 .. 100000] of longint;

Root: point;

 

Procedure creat (p: point; l, r: longint );

Begin

P ^. left: = l; p ^. right: = r; p ^. sum: = maxlongint;

If l + 1 = r then

Begin

P ^. lp: = nil;

P ^. rp: = nil;

End

Else

Begin

New (p ^. lp); creat (p ^. lp, l, (l + r) div 2 );

New (p ^. rp); creat (p ^. rp, (l + r) div 2, r );

End;

End;

 

Function min (x, y: longint): longint;

Begin

If x

Exit (x );

Exit (y );

End;

 

Procedure update (p: point; x, delta: longint );

Begin

If p ^. left + 1 = p ^. right then

Begin

P ^. sum: = delta;

End

Else

Begin

If x <(p ^. left + p ^. right) div 2 then

Update (p ^. lp, x, delta );

If x> = (p ^. left + p ^. right) div 2 then

Update (p ^. rp, x, delta );

P ^. sum: = min (p ^. lp ^. sum, p ^. rp ^. sum );

End;

End;

 

Function query (p: point; l, r: longint): longint;

Var

Ans: longint;

Begin

If (l <= p ^. left) and (p ^. right <= r) then

Exit (p ^. sum );

Ans: = maxlongint;

If l <(p ^. left + p ^. right) div 2 then

Ans: = min (ans, query (p ^. lp, l, r ));

If r> (p ^. left + p ^. right) div 2 then

Ans: = min (ans, query (p ^. rp, l, r ));

Exit (ans );

End;

 

Begin

Readln (n, m );

For I: = 1 to n do

Read (a [I]);

New (root );

Creat (root, 1, n + 1 );

For I: = 1 to n do

Update (root, I, a [I]);

For I: = 1 to m do

Begin

Readln (k, x, y );

If k = 2 then

Update (root, x, y );

If k = 1 then

Write (query (root, x, y + 1 ),'');

End;

Writeln;

End.

 

 

 

 

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.