BZOJ 2178: 圆的面积并 (辛普森积分)
code
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1005;
const double Pi = acos(-1.0);
const double eps = 1e-12;
const double INF = 1e12;
inline int dcmp(double x) {
if(x <= eps && x >= -eps) return 0;
return x > 0 ? 1 : -1;
}
inline double sqr(double x) { return x*x; }
struct Vector {
double x, y;
inline Vector(double _x=0, double _y=0): x(_x), y(_y){}
inline Vector operator *(const double &k)const { return Vector(x*k, y*k) ;}
inline Vector operator +(const Vector &t)const { return Vector(x+t.x, y+t.y); }
inline Vector operator -(const Vector &t)const { return Vector(x-t.x, y-t.y); }
};
struct Point {
double x, y;
inline Point(double _x=0, double _y=0): x(_x), y(_y){}
inline Vector operator -(const Point &t)const { return Vector(x - t.x, y - t.y); }
inline Point operator +(const Vector &t)const { return Point(x + t.x, y + t.y); }
inline double dist(const Point &t) { return sqrt(sqr(x-t.x) + sqr(y-t.y)); }
inline double dist(const double &a, const double &b) { return sqrt(sqr(x-a) + sqr(y-b)); }
};
struct Line {
Point p;
Vector v;
double ang;
inline Line(Point _p=Point(0, 0), Vector _v=Vector(0, 0)): p(_p), v(_v), ang(atan2(v.y, v.x)){}
inline bool operator <(const Line &t)const { return ang < t.ang; }
};
struct Circle {
Point o; double r;
inline Circle(Point _o=Point(0, 0), double _r=0): o(_o), r(_r){}
};
inline double Cross(const Vector &a, const Vector &b) { return a.x*b.y - a.y*b.x; }
inline bool Turn_Left(const Point &a, const Point &b, const Point &c) { return dcmp(Cross(b-a, c-a)) > 0; }
inline bool On_Left(const Line &a, const Point &b) { return dcmp(Cross(a.v, b-a.p)) >= 0; }
inline Point GLI(const Point &P, const Vector &v, const Point &Q, const Vector &w) { //Intersection
Vector u = Q - P;
double k = Cross(u, w) / Cross(v, w);
return P + v*k;
}
inline double Angle_C(const double &a, const double &b, const double &c) { //Triangle's 3 edges
return acos((sqr(a)+sqr(b)-sqr(c))/(2*a*b));
}
inline double Tri_S(const Point &a, const Point &b, const Point &c) { return fabs(Cross(b-a, c-a))/2; }
inline Point rotate(const double &x, const double &y, const double °ree) {
return Point(x*cos(degree)-y*sin(degree), x*sin(degree)+y*cos(degree));
}
int n; Circle cir[MAXN], tmp[MAXN];
inline bool cmp(const Circle &a, const Circle &b) {
return a.o.x-a.r < b.o.x-b.r;
//stO Orz -.- >.< xp XD qwq QAQ ToT QwQ QuQ :-) )-: o-: qx TnT
}
inline bool cmp2(const Circle &a, const Circle &b) {
return a.r > b.r;
}
inline bool chk(const Circle &a, const Circle &b) {
return sqr(a.o.x-b.o.x) + sqr(a.o.y-b.o.y) <= sqr(a.r-b.r);
}
inline void Pre_work() {
sort(cir + 1, cir + n + 1, cmp2);
int cnt = 0;
for(int i = 1; i <= n; ++i) if(cir[i].r) {
bool flg = 1;
for(int j = 1; j <= cnt; ++j)
if(chk(cir[i], tmp[j])) { flg = 0; break; }
if(flg) tmp[++cnt] = cir[i];
}
n = cnt;
for(int i = 1; i <= n; ++i) cir[i] = tmp[i];
sort(cir + 1, cir + n + 1, cmp);
}
int st, ed, tot;
pair<double, double>intervals[MAXN<<1];
inline void getCircleIntersection(const Circle &O, const double &x) {
double len = sqrt(sqr(O.r) - sqr(x-O.o.x));
intervals[++tot] = make_pair(O.o.y-len, O.o.y+len);
}
inline double f(double x) {
tot = 0;
for(int i = st; i <= ed; ++i)
if(x < cir[i].o.x+cir[i].r && x > cir[i].o.x-cir[i].r)
getCircleIntersection(cir[i], x);
sort(intervals + 1, intervals + tot + 1);
double L = -INF, R = -INF, res = 0;
for(int i = 1; i <= tot; ++i)
if(intervals[i].first > R)
res += R - L, L = intervals[i].first, R = intervals[i].second;
else R = max(R, intervals[i].second);
return res + (R - L);
}
inline double Simpson(double L, double M, double R, double fL, double fM, double fR, int dep) {
double M1 = (L + M) / 2, M2 = (M + R) / 2;
double fM1 = f(M1), fM2 = f(M2);
double g1 = (M-L) * (fL + 4*fM1 + fM) / 6,
g2 = (R-M) * (fM + 4*fM2 + fR) / 6,
g = (R-L) * (fL + 4*fM + fR) / 6;
if(dep > 11 && fabs(g-g1-g2) < 1e-10) return g;
else return Simpson(L, M1, M, fL, fM1, fM, dep+1) + Simpson(M, M2, R, fM, fM2, fR, dep+1);
}
inline double solve() {
double ans = 0;
for(int i = 1, j; i <= n; ++i) {
double L = cir[i].o.x - cir[i].r, R = cir[i].o.x + cir[i].r;
for(j = i+1; j <= n; ++j)
if(cir[j].o.x - cir[j].r > R) break;
else R = max(R, cir[j].o.x + cir[j].r);
double M = (L + R) / 2;
st = i, ed = i = j-1;
double fL = f(L), fM = f(M), fR = f(R);
ans += Simpson(L, M, R, fL, fM, fR, 0);
}
return ans;
}
int main () {
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%lf%lf%lf", &cir[i].o.x, &cir[i].o.y, &cir[i].r);
Pre_work();
printf("%.3f\n", solve());
}
BZOJ 2178: 圆的面积并 (辛普森积分)的更多相关文章
- BZOJ 2178: 圆的面积并 [辛普森积分 区间并]
2178: 圆的面积并 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1740 Solved: 450[Submit][Status][Discus ...
- bzoj 2178 圆的面积并 —— 辛普森积分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2178 先看到这篇博客:https://www.cnblogs.com/heisenberg- ...
- bzoj 2178 圆的面积并——辛普森积分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2178 把包含的圆去掉.横坐标不相交的一段一段圆分开算.算辛普森的时候预处理 f( ) ,比如 ...
- BZOJ 2178 圆的面积并 ——Simpson积分
[题目分析] 史上最良心样例,史上最难调样例. Simpson积分硬上. 听说用long double 精度1e-10才能过. 但是double+1e-6居然过了. [代码] #include < ...
- [BZOJ 2178] 圆的面积并 【Simpson积分】
题目链接:BZOJ - 2178 题目分析 用Simpson积分,将圆按照 x 坐标分成连续的一些段,分别用 Simpson 求. 注意:1)Eps要设成 1e-13 2)要去掉被其他圆包含的圆. ...
- BZOJ 1845: [Cqoi2005] 三角形面积并 (辛普森积分)
大力辛普森积分 精度什么的搞了我好久- 学到了Simpson的一个trick 深度开11,eps开1e-4.跑的比有些扫描线还快- CODE #include <bits/stdc++.h> ...
- bzoj 2178 圆的面积并【simpson积分】
直接套simpson,f可以直接把圆排序后扫一遍所有圆,这样维护一个区间就可以避免空段. 然而一定要去掉被其他圆完全覆盖的圆,否则会TLE #include<iostream> #incl ...
- 【BZOJ】2178: 圆的面积并
http://www.lydsy.com/JudgeOnline/problem.php?id=2178 题意:给出n<=1000个圆,求这些圆的面积并 #include <cstdio& ...
- BZOJ 1502: [NOI2005]月下柠檬树 [辛普森积分 解析几何 圆]
1502: [NOI2005]月下柠檬树 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1070 Solved: 596[Submit][Status] ...
随机推荐
- centos6.5安装python3及virtualenv环境
1. 下载源码: wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz wget http://mirrors.sohu.com/ ...
- while循环,格式化输出,运算符及编码初识
一.while循环 1.基本循环(死循环) while 条件: 循环体 2.使用while计数 count = 0 # 数字里面非零的都为True while True: count = count ...
- SqlServer2008 跨服务器同步数据
最近工作中需要跨服务器同步数据,在数据库DB1中的表T1插入数据,同时触发T1的触发器(这里暂不讨论触发器的效率问题),向另一台服务器DB2中的相同的一张表T2插入数据,查看了一些资料说, 需要打开D ...
- 小知识:修改IDEA的模板
小知识:修改IDEA的模板 有时候我们会发现,IDEA默认创建的模板并不是我们常用的.与其每次都在创建后进行修改,不如直接对模板进行修改. 给不知道怎么修改的同学指一下路: File->sett ...
- docker启动 elasticsearch 修改 xmx xms 堆内存大小修改
用docker 安装的elasticsearch 5.6版本默认堆内存最大设置的2G 可以通过如下方法修改 [root@nova-92 logs]# find /var/lib/docker/ -na ...
- css选择器找亲戚
1.first-child first-child表示选择列表中的第一个标签.代码如下: li:first-child{background:#090} 上面的意思是,li 列表中的 第一个li模块的 ...
- TVM图优化(以Op Fusion为例)
首先给出一个TVM 相关的介绍,这个是Tianqi Chen演讲在OSDI18上用的PPThttps://files.cnblogs.com/files/jourluohua/Tianqi-Chen- ...
- VirtualBox使用
热键:Right Ctrl 串口 端口编号: COM1 -> /dev/ttyS0 COM2 -> /dev/ttyS1 COM3 -> /dev/ttyS2 COM4 -> ...
- Linux下创建NFS来实现共享文件
简介说明: 在项目生产环境我们经常需要实现文件共享,传统的常见方案是通过NFS,实现服务器之间共享某一块磁盘,通过网络传输将分散的文件集中存储在一块指定的共享磁盘,实现基本的文件共享.实现这种方案,分 ...
- 怎么解决Win7电脑更新出现80072EE2代码的错误?
我们在使用Win7系统时经常会遇到更新,这些更新可以修复一些系统漏洞,提高系统的安全性.但有时我们在进行相关更新时会出现错误,而导致最后的更新失败.下面好系统重装助手就和大家分享一下Win7系统更新出 ...