SPOJ - CIRU - The area of the union of circles / BZOJ 2178
CIRU - The area of the union of circles
You are given N circles and expected to calculate the area of the union of the circles !
Input
The first line is one integer n indicates the number of the circles. (1 <= n <= 1000)
Then follows n lines every line has three integers
Xi Yi Ri
indicates the coordinate of the center of the circle, and the radius. (|Xi|. |Yi| <= 1000, Ri <= 1000)
Note that in this problem Ri may be 0 and it just means one point !
Output
The total area that these N circles with 3 digits after decimal point
Example
Input:
3
0 0 1
0 0 1
100 100 1
Output:
6.283
题意:求圆的面积并
解法:simpson积分,递归至左右区间积分和与大区间积分的差值小于EPS
1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <cmath> 7 #include <string> 8 #include <cstring> 9 #include <algorithm> 10 #include <queue> 11 #include <stack> 12 #include <vector> 13 #include <set> 14 #include <map> 15 #include <list> 16 #include <iomanip> 17 #include <cctype> 18 #include <cassert> 19 #include <bitset> 20 #include <ctime> 21 22 using namespace std; 23 24 #define pau system("pause") 25 #define ll long long 26 #define pii pair<int, int> 27 #define pb push_back 28 #define pli pair<ll, int> 29 #define pil pair<int, ll> 30 #define pdd pair<double, double> 31 #define clr(a, x) memset(a, x, sizeof(a)) 32 33 const double pi = acos(-1.0); 34 const int INF = 0x3f3f3f3f; 35 const int MOD = 1e9 + 7; 36 const double EPS = 1e-9; 37 38 /* 39 #include <ext/pb_ds/assoc_container.hpp> 40 #include <ext/pb_ds/tree_policy.hpp> 41 using namespace __gnu_pbds; 42 #define TREE tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update> 43 TREE T; 44 */ 45 46 struct Circle { 47 double x, y, r; 48 Circle () {} 49 Circle (double x, double y, double r) : x(x), y(y), r(r) {} 50 } c[1005]; 51 int n; 52 double Cut(double x) { 53 pdd arr[1005]; 54 int index = 0; 55 for (int i = 1; i <= n; ++i) { 56 if (fabs(x - c[i].x) < c[i].r) { 57 double dd = sqrt(c[i].r * c[i].r - (x - c[i].x) * (x - c[i].x)); 58 arr[++index] = pdd(c[i].y - dd, c[i].y + dd); 59 } 60 } 61 sort(arr + 1, arr + index + 1); 62 double res = 0, ma = -INF; 63 for (int i = 1; i <= index; ++i) { 64 res += max(0.0, arr[i].second - max(arr[i].first, ma)); 65 ma = max(ma, arr[i].second); 66 } 67 return res; 68 } 69 double simpson(double l, double r, double fl, double fr) { 70 double fmi = Cut((l + r) * 0.5); 71 return (r - l) / 6.0 * (fl + 4 * fmi + fr); 72 } 73 double solve(double l, double r) { 74 double mi = (l + r) * 0.5; 75 double fmi = Cut(mi), fl = Cut(l), fr = Cut(r); 76 double ans = simpson(l, r, fl, fr); 77 double ans1 = simpson(l, mi, fl, fmi); 78 double ans2 = simpson(mi, r, fmi, fr); 79 if (r - l < 1 && fabs(ans1 + ans2 - ans) < EPS) return ans1 + ans2; 80 return solve(l, mi) + solve(mi, r); 81 } 82 int main() { 83 scanf("%d", &n); 84 for (int i = 1; i <= n; ++i) { 85 scanf("%lf%lf%lf", &c[i].x, &c[i].y, &c[i].r); 86 } 87 printf("%.3f\n", solve(-2000, 2000)); 88 return 0; 89 } 90