Can anyone explain the algorithm to solve area of union of circles problem( given n circles + centre and radius of each)
Thankyou
# | 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 | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 151 |
Name |
---|
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.
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?