Tanmoy1228's blog

By Tanmoy1228, history, 8 years ago, In English

Problem: 610D - Vika and Segments

can not understand the solution in editorial

can anyone give me any idea of solution in deails???

  • Vote: I like it
  • +1
  • Vote: I do not like it

»
8 years ago, # |
Rev. 4   Vote: I like it +6 Vote: I do not like it

First of all, break up the problems into rectangles of two types. One those which are horizontal, and the other which are horizontal. Now there are 3 types of events:

  1. Horizontal-horizontal intersection
  2. Vertical vertical intersection
  3. Horizontal vertical intersection

The events of type 1 and 2 can easily be taken care of using a stack, and merging the two rectangles into a single longer one. For the events of type 3, imagine you're looking at the plane and run your gaze through it from left to right, Whenever you see such an intersection you subtract the area from your answer. Also you need to use Coordinate compression.

This is essentially what we do. We move from left to right, keeping track of the rectangles currently in the x coordinate, and check for intersections. We could do this using a set, but because finding distance between iterators takes O(n) time, that would make our solution O(2·n2). To reduce this runtime, we use a BIT to keep track of them, and doing so makes our runtime O(2·n lgn)

Update: You could read more on line sweep algorithms here and here.

My submission is this: 15129127, it's definitely slightly messy but it should help.

Please point out any errors/inaccuracies, as I solved the problem some time back.