A tree array is an excellent data structure that can be maintained in the case of O (log n) ~ ~
And the code is easy to understand, so the tree array is a very common data structure for solving the interval problem in the Oi contest.
Following are some examples:
A. Description of the tree outside the gate
A school gate outside the length of the road is a row of trees, every two adjacent trees between the interval is 1 meters. We can think of the road as a axis, one end of the road in the position of the axis 0, the other end in the position of L; each integral point on the axis, namely 0,1,2,......,l, is planted with a tree.
There are some areas on the road to be used to build subways. These regions are represented by their starting and ending points on the axis. The coordinates of the starting and ending points of any given area are integers, and there may be overlapping parts between regions. Now the trees in these areas (including the two trees at the end of the area) are removed. Your task is to calculate how many trees there are on the road after the trees have been removed.
Input/output format
Input format:
The first line of the input file tree.in has two integer l (1 <= L <= 10000) and M (1 <= M <=), L represents the length of the road, M represents the number of areas, and L and M are separated by a space. The next M-line contains two different integers, separated by a space, representing the coordinates of the starting and ending points of an area.
Output format:
The output file Tree.out includes a row that contains only an integer that represents the number of trees remaining on the road.
Input and Output Sample input example # #:
500 3150 300100) 200470 471
Sample # # of output:
298
Description
The second problem of NOIP2005 popularization group
For 20% of the data, there is no overlap between the regions;
For other data, there is a overlap between the regions.
"Explanation"
A classic Simulation + violence problem, the brute force algorithm will no longer say, talk about the solution of the line-segment tree:
This problem requires a differential solution:
So first look at the difference is what thing!
To understand the difference, first understand the prefix and:
For example {1,1,2,3,2} prefix and {1,2,4,7,9}
The difference is the inverse of the prefix and
We can make a difference to the initial sequence (without writing a program because each is 1 and the difference sequence is 000000000), can you understand? )
For example {1,1,2,3,2} differential sequence is {1,0,1,1,-1}
Then the time complexity of the interval addition of the differential sequence is O (1)
Note that a[5]={1,1,2,3,2}, differential sequence B is {1,0,1,1,-1}
Let's say we're going to put +2 in between.
Differential score column: {2,0,1,1,-3} Note that only the a[1]a[5] is changed in differential sequence B, and the algorithm complexity is O (1)
Focus here : for the closed interval [x, y] plus opx, we only need O (1) will [X, y] in a sequence of sequence a difference sequence B of the b[x-1]+opx b[y+1]-opx can be
Now it's time to start with the title: Suppose our tree array maintains a differential sequence c[x]
For the given [x, y] we O (1) Maintain C[x-1]+opx;c[y+1]-opx
Finally traverse the entire range of 1~n+1 to see if the difference sequence C prefix and bi is 0, then there is a tree here or there is no tree here, you can simply accumulate;
here 's the point: the difference prefix and the original sequence, and the difference between the prefix and the original sequence.
varN,m,i,l,r,ans:longint; c:array[1..100000]of longint;procedure Update (x,opx:longint); Begin whileX<=n Dobegin C[X]:=c[x]+opx; X:=x+ (x and (-x)); End;end;function query (x:longint): Longint;varsum:longint;begin sum:=0; whileX>0 DoBegin sum:=sum+C[x]; X:=x-(x and (-x)); End Exit (sum); End;begin readln (n,m); fori:=1to M Dobegin READLN (L,R); Update (L+1,1); Update (R+2,-1); End Ans:=0; fori:=1To n+1 Do ifQuery (i) =0Then Inc (ANS); Writeln (ans); end.
Simple application of tree-like array