In the review of the algorithm to the division of the law, the book mainly introduces the combination of sorting and quick ordering, relatively simple. Special expansion simple learning an algorithm structure--segment tree, which is applied to divide-and-conquer method.
ACM BRUSH problem encountered a number of continuous interval dynamic query problems, such as finding the sum of elements in a certain interval, to obtain the maximum value of the elements in a certain interval, if the use of the general method of solving the time will exceed the requirements. In this case, we need to use the segment tree, which is mainly used to solve the dynamic query problem of continuous interval efficiently.
A segment tree, similar to an interval tree, is a fully binary tree that holds a segment (a sub-array in an array) at each node, and because of the characteristics of the binary structure, it can basically maintain the complexity of each operation O (LgN), which greatly reduces time-consuming.
The basic properties of the segment tree are as follows: The father's interval is [A, b], (c= (A+B)/2) The left son's interval is [a,c], the right son's interval is [c+1,b], the line segment tree needs four times times the size of the array.
For an example of a range of 1 to 10, the tree segment is constructed as follows:
The algorithm constructs the basis of the segment tree mainly includes the build(Contribution function), update (updating function) and query (query interval and function).
Build function:
void Build (int l, int r, int step) //Achievements, step represents tree node number (below) { tree[step].left = l; Assignment tree[step].right = r; if (l ==r) //l = = r when the description extends to the leaf node, returns return ; int mid = (L + r) >> 1; Two-point Build (L,mid, step<<1); Recursive left subtree Build (mid+1,r, (step<<1) +1); Recursive right sub-tree}
Update function:
void Update (int l, int r, int value, int step) {if (tree[step].left== tree[step].right)//has been updated to leaf node returns return; int mid = (Tree[step].left +tree[step].right) >> 1; If the right endpoint of the point to be updated is less than mid or the left endpoint is greater than mid, then directly update the value of L to R if (r <= mid) update (l,r, value, step<<1); else if (l> mid) Update (L,r, Value, (step<<1) +1); If the point you want to update is on both sides of mid, update the else {update (L, Mid, Value, step<<1) separately on both sides; Update (mid+1, R, Value, (step<<1) +1); }}
Query function:
int Query (int l, int r, int step) {//Find leaves return value if (l = = Tree[step].left && r ==tree[step].right) Retu RN Tree[step]. value; int mid = (Tree[step].left +tree[step].right) >> 1; Query and update are similar, all are segmented operations if (r <=mid) return query (L, R, Step<<1); if (l >mid) return Query (L, R, (step<<1) +1); else return Query (L, Mid, step<<1) +query (mid+1, R, (step<<1) +1);}
The problem of single point updating of a very basic segment tree with Hangzhou electric OJ is an example to help understand:Hduoj 1166
C State of the death of a country during the period of military exercises, so the C state spy chief Derek and his men tidy began to busy again. Country A has a line of N engineering camps along the coastline, and Derek and tidy are tasked with monitoring the activities of these engineering camps. As a result of the adoption of some advanced monitoring tools, the number of people in each engineering camp, C, is well-informed and the number of personnel in each engineering camp is likely to change, possibly by increasing or reducing the number of staff, but none of them can escape the surveillance of State C.
The CIA wants to study what tactics the enemy is practicing, so Tidy to report to Derek at any time about how many people there are in a succession of engineering camps, such as Derek asking: "Tidy, Report to the 3rd camp to the 10th camp." Tidy is about to start calculating the total number of people in this section and report it immediately. But the number of enemy barracks is constantly changing, and Derek Each asked the paragraph is not the same, so tidy had to every time a camp to count, quickly exhausted, Derek to tidy's calculation speed more and more dissatisfied: "You a dead Fat boy, calculate so slow, I fire you squid!" Tidy thought, "You can count on yourself, it's a tiring job!" I wish you would fire me! " Helpless under, tidy had to call computer experts windbreaker for help, Windbreaker said: "Die fat, you usually do more than a point of ACM and see more Algorithms book, now taste the bitter pill!" Tidy said: "I know the wrong ... "But Windbreaker already hung up the phone." Tidy is very distressed, so he really will collapse, smart Reader, can you write a program to help him finish the work? But if your program is not efficient enough, tidy will still be scolded by Derek.
Input
The first line is an integer t, which indicates that there is a T group of data.
The first line of each group of data is a positive integer n (n<=50000), which indicates that the enemy has N engineering camps, followed by n positive integers, and the second positive integer AI represents the first AI individual (1<=ai<=50) in the field of the I barracks.
Next, there is one command in each line, with 4 forms of command:
(1) Add I j,i and J are positive integers, indicating that I camp increase J person (J not more than 30)
(2) Sub i J, I and J are positive integers, indicating that the I camp reduced J person (j not more than 30);
(3) Query i J, I and J are positive integers, i<=j, which indicate the total number of persons who have inquired about the first and the J camps;
(4) End indicates that the command ends in each group of data;
Up to 40,000 commands per set of data
Output
For group I data, first output "case I:" and carriage return,
For each query, output an integer and enter it to indicate the total number of people in the segment you are asking for, which remains within Int.
Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
ADD 3 6
Query 2 7
Sub 10 2
ADD 6 3
Query 3 10
End
Sample Output
Case 1:
6
33
59
It is easy to associate a line tree to solve this kind of problem.
The following code goes from TC Daniel, in the code I added a comment on the execution of the function to make the execution process easier to understand.
#include <cstdio> #include <cstring>int const MAX = 400000 + 10;struct tree//trees struct {int left, right; The tree's left and right subtree int sum; Tree value} tree[max];void Build (int l, int r, int step)//Achievements, step represents tree node number (below) {tree[step].left = L;tree[step].right = R;tree [step].sum = 0;//cout<< "achievements" <<step<<endl;printf ("node:%d left%d right%d\n", step,l,r); if (L = = r)//l = = R description Extend to leaf node, return return;int mid = (L + r) >> 1; Two-point build (L, Mid, step<<1); Build (Mid+1, R, (step<<1) +1);} void Update (int l, int r, int value, int step)//Update function {tree[step].sum + = value; Because this is the sum, the value of the traversed point is directly added to printf ("Node%d plus%d\n", step, value); if (tree[step].left = = tree[step].right)// Always updated to leaf node return return;int mid = (tree[step].left + tree[step].right) >> 1;if (R <= mid)//If the right endpoint of the point to be updated is less than mid, update L to R directly The value of update (L, r, Value, step<<1), else if (L > Mid)///If the left endpoint of the point to be updated is greater than mid, then directly update the value of L to r/note above cannot be written r<mid and l>=mid by the tree The nature of the decision, the reader can draw to see the update (L, R, Value, (step<<1) +1), else//If the point you want to update is on both sides of mid, update {update (L, Mid, Value, step<<1) separately on both sides; Update (mid+1, R, Value, (step<<1) +1);}} int query (int l, int r, int step)//Query function {if (L = = Tree[step].left && r = = tree[step].right) {printf ("Value%d\n" of "Return node%d", s Tep,tree[step].sum); return tree[step].sum;} Find the leaf return value int mid = (tree[step].left + tree[step].right) >> 1;//The following similar update steps, no longer elaborated if (R <= mid) {printf ("Node:%d query%d to% d \ n ", step,l,r); return Query (L, R, step<<1);} if (L > Mid) {printf ("Node:%d query%d to%d \ n", Step,l,r), return Query (L, R, (step<<1) +1);} Else{printf ("Node:%d queries%d to%d and%d%d \ n", step,l,mid,mid+1,r), return query (L, Mid, step<<1) + query (mid+1, R, (step< ; <1) +1);}} int main () {int t;int A, B, N;char cmd[6];scanf ("%d", &t), for (int i = 1; I <= T; i++) {scanf ("%d", &n); Build (1,n,1); 1-n Achievements for (int j = 1; J <= N; j + +) {int temp;scanf ("%d", &temp); Update (j,j,temp,1); Updated from Root to leaf}printf ("Case%d:\n", I); while (scanf ("%s", cmd)! = EOF && strcmp (cmd, "End")! = 0) {sCANF ("%d%d", &a, &b), if (strcmp (cmd, "query") = = 0) printf ("%d\n", Query (a,b,1)), Else if (strcmp (cmd, "Add") = = 0)/ /plus the value of B is positive update (a,a,b,1), Else if (strcmp (cmd, "Sub") = = 0)//minus when the value of B is negative update (a,a,-b,1);}}
to help understand this, the construction process for a segment tree with an interval number of 5 is:
The following is the process of adding a five-digit segment tree update:
Here is the procedure for query interval 2-5 function execution:
Code comments are detailed and easy to understand, and if you need to submit a OJ simply remove the comment code.
I struggled for some time after the beginning of the line to understand the tree segment is generally clear, the above a question according to Daniel called the most basic line of the tree, only update the leaf node. It seems that the basis of the algorithm is too weak, but also need refueling refueling and refueling. There are also follow-up exercises and updates about the line tree.
This is to prevent forgetting, special note today understanding and harvest for tomorrow review.
Line Tree Entry Understanding