If I have a square of some area a, and n other squares of the same area a, how can I check if the union of the n squares contains the initial square completely? I think I read that there is an solution for this, could someone help? Thanks!
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
If I have a square of some area a, and n other squares of the same area a, how can I check if the union of the n squares contains the initial square completely? I think I read that there is an solution for this, could someone help? Thanks!
Name |
---|
You need to do a line sweep approach. Just google for it, you may find variations of it, but it isnt hard to change it a little bit. Interistingly the algorithm, as you mentioned, is needed for problem I from acm-icpc 2011 world finals.
There is material for this in wikipedia, cormen, geeksforgeeks (the last one is the main source from where I learnt)
Could you explain a bit? I read on the line sweep algorithm but how would I adapt it for this purpose? (As we're dealing with planes and not lines here)
First of all, the squares should have sides parallel to xy axes. Then you consider squares as start and end segments, ordered by their x value. So you do the line sweep in the x direction keeping track of the active squares, the ones that appear in the current x. You keep the active squares in a bst (like c++ set) ordered by their y value, so you can add and remove squares in log(n) time. In this way you can calculate the area of the union of the n squares, something like as shown here.
The key observation is that you keep track of the lenght of the union of the current active squares and, while you advance in the x coordinate, the area adds by d*l (where d is how much you advanced in x coordinate, and l the current total length). You keep updating the active set of squares and also the new total length by removing/adding squares to the active set and checking the neibours of the added/removed square (the neighbours in the bst (set) of active squares, ordered by y value). You check the neighbours to see if their length (y size) overlaps or doesn't (remembering that the set is ordered by y value, it is easy to see why you only check for neighbours to update the y current total length correctly). In this way you can update the current total lenght correctly in O(logn) per sqaure.
To adapt for your problem you should consider the length only inside the square you are looking for (the one to check if it is overlaped by the union of all the others). If the area calculated by the line sweep inside the square is the same as the total area of the square, then, it overlaps completely.
If you have any doubts, I would suggest to first think by yourself. If you are having troube perhaps you should practice on easier line sweep problems
Rectangles union using sweep line is what you're looking for.