struct Rect { double left, right, top, bottom; }; std::vector<Rect> vec;
we now have N (N> 1000) rectangles, which efficient algorithm determines if any of the two of them overlap?
updates: all of these rectangles are parallel to the coordinate system.
You can imagine a rectangle using two segments : open the segment (x1, y1) to (x1, y2) and close the segment (x2, y1) to (x2, y2) with x1 <x2 and y1 <y2.
First, we can sort all these segments O (nlogn) times by the x coordinate .
-, , , (y1, y2) , , . , , , , . O (logn).
, O (nlogn).
, . , , H, ditto V . H V , , 2D .
2D , O (n log n), , - .
, D * D, , :
std::vector<Rect*> grid[D][D];
, 20x20, 1000+ , . , D , , 5-50 vector ( , ), .
vector
:
nullptr
empty()
( , , , / , .., , , , . , , ( ). , , true/false --- .)
, RH GΓΌting W Schilling, " ", Information Sciences 42 (1987). ( ), , , and-conquer , "-", : " , , , ?"