Interval problem and discretization

Source: Internet
Author: User

During the competition, we often encounter operations on intervals, such as dynamic planning of intervals. This type of question usually has a large data range, and the operation may seem very simple, but the simulation will time out 1000%... At this time, the idea of discretization is the best way to solve such problems (You may say that many problems can be solved using the line segment tree. But who is willing to give up just a few lines of code and spend 10 minutes or even dozens of minutes writing line tree ).

Example 1 

 [Problem description] (noip2005 popularity group P2)
There is a row of trees on the road with a length of L outside the gate of a school. The interval between every two adjacent trees is 1 meter. We can regard the road as a number axis, where one end of the road is 0 and the other is L. Each integer point on the number axis is 0, 1, 2 ,......, L. There is a tree.
There are some areas on the road to build a subway. These regions are expressed by their starting and ending points on the number axis. It is known that the coordinates of the starting and ending points of any region are integers, and the areas may overlap. Now we need to remove the trees (including the two trees at the region endpoint) in these regions. Your task is to calculate the number of trees on the road after all these trees are removed.
[Input file]
Input File tree. the first line of in has two integers, L (1 <= L <= 10000) and M (1 <= m <= 100). l represents the length of the road, M indicates the number of regions. L and M are separated by a space. Each row of the next M line contains two different integers separated by a space, representing the coordinates of the start and end points of a region.
[Output file]
The output file tree. Out contains one row. This row contains only one integer, indicating the number of remaining trees on the road.
[Example input]
500 3
150 300
100 200
470 471
[Sample output]
298
[Data scale]
There is no overlap between regions for 20% of data;
For other data, areas overlap.

Needless to say, there are many solutions. The simplest thing is to open a tag array, mark all covered fields, and count the number of array Units marked.

But what if we change the data range to 1 <= L <= 1000000 and 1 <= m <= 10000?

It is still the idea of the previous question. it overwrites an array in order of reading. When l and m are large, the O (N * m) algorithm obviously does not work.

Now we have another idea. This question is characterized by a large total length of L, but the number of M segments is relatively small. So why not operate on m intervals?

We use arrays X and Y to record the Left and Right vertices of the read points, and then sort them by X (either by Y, and then the order of enumeration needs to be changed ). Use the variable head to record the left endpoint of the current operation interval, and tail to record the right endpoint of the current operation interval. Initialize the head value to X [1], and the tail value to Y [1].

Enumerate each interval from 2 to M. If X [I] <tail and Y [I]> tail, there is an intersection between the two ranges. Therefore, update tail to Y [I], that is, combine the two intervals into a larger interval and use this interval for operation. (1)

If X [I]> tail, We need to update the operation interval, assign head to X [I], and assign tail to Y [I]. and accumulate the length of the previous interval. In this way, the solution can be obtained in O (m) time.

Reference code:

Program tree; var L, R: array [0 .. 10010] of longint; X, Y: array [0 .. 10010] of longint; I, J, K: longint; n, m, tail, sum: longint; F: Boolean; Procedure sort (L, R: longint); var I, j, XX, YY: longint; begin I: = L; J: = r; XX: = x [(L + r) Div 2]; repeat while X [I] <XX do Inc (I); While XX <X [J] Do Dec (j); if not (I> J) then begin YY: = x [I]; X [I]: = x [J]; X [J]: = YY; YY: = Y [I]; y [I]: = Y [J]; y [J]: = YY; Inc (I); j: = J-1; end; until I> J; if l <j then sort (L, j); If I <r then sort (I, R); end; begin readln (n, m); for I: = 1 to M do readln (X [I], Y [I]); sort (1, m); F: = false; K: = 1; tail: = Y [1]; L [1]: = x [1]; for I: = 1 to M do begin if F then begin R [k]: = tail; Inc (k); L [k]: = x [I]; F: = false; tail: = Y [I]; end; If X [I + 1]> tail then F: = true // Update Interval else if y [I + 1]> tail then tail: = Y [I + 1]; // merge Interval End; R [k]: = tail; for I: = 1 to k do sum: = sum + R [I]-l [I] + 1; sum: = N-sum + 1; writeln (SUM); end.

After reading this question, do you have a preliminary understanding of the discretization interval? Simply put, it is to move the operations on the entire range of data to operations in intervals, thus greatly improving the algorithm efficiency.

Example2

 [Title Description] (adapted from Byvoid's wow simulation competition stage.3, which is included at the end of The Link)
Given n intervals, they are colored differently. After coloring, the interval will overwrite the original color of the interval. The coloring is performed in the order of reading. Ask how many different colors are available in the entire section after coloring. [Input format]
Row 1st: an integer N, indicating the number of colored intervals.
Line 2nd to line n + 1: line I + 1 provides the head and tail coordinates of the I-th interval, AI, Bi.
[Output format]
An integer that represents the number of different colors in a segment.
[Example input]
4
0 5
3 8
5 6
4 7
[Sample output]
3
[Example]

We can see that the range 3 and 3 are covered by the range 4.
[Data Scope]
50% of the data meet n <= 1,000; 0 <= AI <Bi <= 100,000 (1 <= I <= N );
100% of the data meet n <= 20,000; 0 <= AI <Bi <= 1,000,000,000 (1 <= I <= N ).

Massive Data... If you use a powerful simulation method, you will not be able to understand it in 10 seconds. In this case, we want to operate the intervals again.

At this time, the interval and the interval have only three relationships: intersection, separation, and inclusion. We scan the interval from the back to the front. Every time we scan for a range I, we try to overwrite it with the interval I + 1 after it. If it is separated, we enumerate I + 2 until n. If interval I + K has a point in interval I (this includes intersection and two States ), then, at the interval I and its corresponding points in the interval I + K (the left node of I and the left node of I + K, the right node of I + k and the right node of I + k) try the next interval I + k + 1 in the enclosed range to check whether the interval can be overwritten. If some part of the interval I is not covered, add the number of colors of the record to 1. In this way, we can obtain the solution in the O (kn2) Time. K is a small constant. At this time, our program is much faster than the original algorithm.

Reference code:

Program punch; var X, Y: array [1 .. 20001] of longint; V: array [0 .. 20001] of Boolean; N, Ans: longint; I, J: longint; Procedure plus (A, B, P: longint); begin if V [I] Then exit; while (P <= N) and (B <= x [p]) or (A> = Y [p]) do Inc (P ); if P> N then begin Inc (ANS); V [I]: = true; // The record color has been added and cannot be added again. End; If (X [p] <B) and (x [p]> A) Then plus (A, X [p], p + 1 ); // If (Y [p] <B) and (y [p]> A) Then plus (Y [p], B, P + 1); // The End Of The relative right node; begin readln (n); ans: = 0; for I: = 1 to n do readln (X [I], Y [I]); for I: = n-1 downto 1 do plus (X [I], Y [I], I + 1); writeln (ANS + 1); end.

These two questions are very representative ~ Now we have a general understanding of the discretization method of the interval problem. This type of question generally has a large range, but the number of intervals is relatively small. The general idea is to find the relationship between intervals, and then enumerate the intervals one by one to find conditions that meet the conditions. Therefore, the analysis of the relationship between intervals is crucial. This kind of sensitivity needs to be cultivated in more exercises.

After sending it, I found that the number axes in Figure 1 are not as rough as they are... because it is troublesome to change the image, saltless will steal it ~ Thank you for your understanding.>. <|

Example 2 Source: http://www.byvoid.com

Address: http://www.cnblogs.com/saltless/archive/2010/11/14/1877171.html

(Saltless original, reprinted please indicate the source)

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.