Specify the number of intervals containing a different interval?

For two lists, each of which contains N intervals (a subset of the number string), each interval with the shape of the start point and end point. How many pairs of these intervals from one list contains intervals from another list?

For example:

If list A is equal {(1,7), (2,9)}and list B is{(3,6), (5,8)}

Then the number of pairs where A has an interval containing an interval in B will be 3 pairs:

(1,7),(3,6)
(2,9)(3,6)
(2,9)(5,8)

The goal is to shoot for O (n log n).

My current algorithm is to first sort by x coordinate and accept this as one list. Then sort the list by y-coordinate and count the inversions between the two lists. But my question is, why does this work? Any insight would be appreciated.

, , ( num):

enter image description here

. , . , O (n log n). , .

+4
2

tree/gird, , . 2D, . , .

, 1 100. 100. ( ). . :

enter image description here

. 1 , 1,7 2 2,9 (1,2 - ID, 1 , , ).

, B? , . , O (log n). n O (log n) , , B A.

, ( ). , , O (logN) O (1), , .

+1

, . -, . ( ), , A B. , , A = {(1,7), (2,5)} B = {(3,6), (5,8)}. , 2 , , , .. (1,7), (3,6).

, : I1=(x1,y1) I2=(x2,y2). I2 I1. , x1 <= x2 y1 >= y2. , x, I1 I2. , y, I1 I2. , beans, I1, I2 I1, I2 , .

, x1 <= x2 y1 < y2. I1 I2 . I1, I2 I1, I2 , . , x1 > x2 y1 >= y2

:

+1

Source: https://habr.com/ru/post/1627743/


All Articles