Can anyone explain the algorithm to solve area of union of circles problem( given n circles + centre and radius of each)
Thankyou
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Название |
---|
The main idea of counting the area of union of some figures is that we draw vertical lines throug the "interesting" points ans solve the problem in each of the stripes between two adjacent lines.
The interesting points here will be the most left and right points on each circle and all the intersection points. There will be O(n2) interesting points.
Inside each stripe there will be some disjoint arcs, so we can sort all the arcs by their y-coordinates and count the area of union in O(n) complexity using, may be, stack (that arcs will form correct parthenesis sequence) and some integrals.
So we have the O(n3 log n) algorithm. It can be reduced to O(n2 log n) by looking on the coefficients in the integral and understanding how they change from strip to strip, so we can use some data structure to update them fast.
Do you know how to find the length of the union of the segments on the line in O(nlogn)? Here is the same: when we process opening arc and the balance is 0 we add area under this arc to the sum. When we process closing arc and balance becomes 0 (or, the same, balance is currentlty 1) we substract the area under this arc from sum.
UPD: The balance is the difference between opening arcs and closing arcs. Oblivious that it is non-negative at all the time.
That was brainsmashing. Hours of debugging...
Advice: if you decide to write it, insert lots of asserts to check wheter variable under square root/arctan/denumerators of rationals are all correct. It will save your time.
....OOO...
..._____..
../.....\.
..|....O|.
..|.....|.
..\_____/.
и ты добавляешь нижнюю маленькую окружность, то соседними ей будут куча маленьких окружностей сверху, а не объемлющая её. Хотя, может я не очень понял, как ты будешь удалять (точнее, когда) окружности из сета.
This problem literally asks you to eliminate the circles that are inside some other circle and add up the areas of the dominating circles.
You can check if a circle is inside another using sweepline.
Main idea:
Sweep from left to right. Insert circles at time cx - r and erase them at cx + r (break ties by r decreasingly).
The sweepline status will be the set of active circles sorted by their cy coordinate.
Before inserting any circle, check it against its immediate predecessor and successor (they are the only candidates for inclusion). If you find that it's included, simply do not insert it into the set. Else, add its area to the answer and insert it into the set.
This can be solved using Green's Theorem, with a complexity of n^2log(n). If you're not familiar with the Green's Theorem and want to know more, here is the video and notes from Khan Academy. But for the sake of our problem, I think my description will be enough. The general equation of Green's Theorem is
If I put L and M such that
then the RHS is simply the area of the Region R and can be obtained by solving the closed integral or LHS and this is exactly what we're going to do.
So Integrating along the path in the anticlockwise gives us the Area of the region and integrating along the clockwise gives us negative of the Area. So
AreaOfUnion = (Integration along red arcs in anticlockwise direction + Integration along blue arcs in clockwise direction)
But the cool trick is if for each circle if we integrate the arcs which are not inside any other circle we get our required area i.e. we get integration in an anticlockwise direction along all red arcs and integration along all blue arcs along the clockwise direction. JOB DONE!!!
Here is the GitHub link to my C++ Code
This code is not passing Area of Union of Circles on SphereOnlineJudge
Problem Links:
Any uncovered corner cases?
I used the same approach and my solutions worked in both cases. There must some other problem in your code! Have you excluded the circles having the same center and radius? If some circles have the same centre and radius take only one of them.
could you please explain how your code works?