Блог пользователя Usu

Автор Usu, история, 5 лет назад, По-английски

Hey! Does anybody have a solution in n log n for this? Given n rectangles, determinate a point which is included by a maximum number of rectangles (including the boundaries).

  • Проголосовать: нравится
  • +34
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится +24 Проголосовать: не нравится

It can be solved with sweep line algo and segment tree in $$$O(N \lg N)$$$.

  1. For each rectangle $$$[x_1, x_2] \times [y_1, y_2]$$$, produce two events $$$(x_1, (y_1, y_2), +)$$$ and $$$(x_2, (y_1, y_2), -)$$$.
  2. Sort all the events (there are $$$2N$$$ such many) by $$$x$$$.
  3. From minimum $$$x$$$ to maximum $$$x$$$, if it's from left side of some rectangle ($$$(x_1, (y_1, y_2), +)$$$), add 1 to all the points in the closed interval $$$[y_1, y_2]$$$, otherwise, subtract 1 to those in the closed interval $$$[y_1, y_2]$$$.
  4. After each addition or subtraction, find the maximum value among all possible $$$y$$$ which is a candidate to the answer(update the answer).
  5. Addition/Subtraction on an interval and querying maximum value can be done with segment tree in $$$O(\lg C)$$$ per operation, where $$$C$$$ is the range of values. If we first normalize all the value $$$y$$$, it can be reduced to $$$O(\lg N)$$$.
  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится +24 Проголосовать: не нравится

    I think that when sweeplining, for a certain x, you should perform all addition events for that x, then check for a new max, then all subtraction events for that x. Otherwise you might miss the correct answer if you perform a subtraction, then a query, then an addition.

    • »
      »
      »
      5 лет назад, # ^ |
        Проголосовать: нравится +20 Проголосовать: не нравится

      Yes, I just give a rough idea. Those parts should be taken care when implementing.

      • »
        »
        »
        »
        5 лет назад, # ^ |
          Проголосовать: нравится +10 Проголосовать: не нравится

        It’s a very nice sketch. I just wanted to add the detail since I had also been thinking of the same solution for a little bit. I hope it helps the asker.

        • »
          »
          »
          »
          »
          5 лет назад, # ^ |
            Проголосовать: нравится +8 Проголосовать: не нравится

          Yes, it helped me. Is this algorithm similar to that one: "find the number of intersections of segments, all segments parallel either Ox either Oy"? I think they are 90% the same, am I wrong?

          • »
            »
            »
            »
            »
            »
            5 лет назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            You can think of each segment of length L as an 1xL or an Lx1 rectangle, so the rectangle problem is a generalization of the line segment problem.