[CQOI2005]三角形面积并
[CQOI2005]三角形面积并
题目大意:
求\(n(n\le100)\)个三角形的面积并。
思路:
自适应辛普森法,玄学卡精度可过。
源代码:
#include<cmath>
#include<cstdio>
#include<cctype>
#include<vector>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=101;
const double eps=1e-10;
struct Point {
double x,y;
};
struct Triangle {
Point p1,p2,p3;
};
Triangle t[N];
struct Node {
double p;
int v,id;
bool operator < (const Node &rhs) const {
return p<rhs.p;
}
};
std::vector<Node> v;
std::vector<int> c;
inline bool cross(const Point &a,const Point &b,const double &x) {
if(a.x==b.x) return false;
if(a.x<b.x) {
return a.x<=x&&x<b.x;
} else {
return b.x<x&&x<=a.x;
}
}
inline double calc(const Point &a,const Point &b,const double &x) {
return (b.y-a.y)/(b.x-a.x)*(x-a.x)+a.y;
}
inline double F(const double &x) {
std::vector<std::pair<double,int> > v;
for(register unsigned j=0;j<c.size();j++) {
const int &i=c[j];
double q[3];
int h=0;
if(cross(t[i].p1,t[i].p2,x)) {
q[h++]=calc(t[i].p1,t[i].p2,x);
}
if(cross(t[i].p2,t[i].p3,x)) {
q[h++]=calc(t[i].p2,t[i].p3,x);
}
if(cross(t[i].p3,t[i].p1,x)) {
q[h++]=calc(t[i].p3,t[i].p1,x);
}
if(h==2) {
if(q[0]>q[1]) std::swap(q[0],q[1]);
v.push_back(std::make_pair(q[0],1));
v.push_back(std::make_pair(q[1],-1));
}
}
std::sort(v.begin(),v.end());
double st,ans=0;
for(register unsigned i=0,tmp=0;i<v.size();i++) {
if(tmp==0) st=v[i].first;
tmp+=v[i].second;
if(tmp==0) {
ans+=v[i].first-st;
}
}
return ans;
}
inline double simpson(const double &fl,const double &fm,const double &fr,const double &len) {
return (fl+4*fm+fr)*len/6;
}
inline double asr(const double &a,const double &b,const double &fl,const double &fm,const double &fr,const int &d) {
const double c=(a+b)/2;
const double flm=F((a+c)/2),frm=F((c+b)/2);
const double ls=simpson(fl,flm,fm,c-a),rs=simpson(fm,frm,fr,b-c),s=simpson(fl,fm,fr,b-a);
if(fabs(ls+rs-s)<eps&&d>13) return ls+rs;
return asr(a,c,fl,flm,fm,d+1)+asr(c,b,fm,frm,fr,d+1);
}
int main() {
const int n=getint();
for(register int i=1;i<=n;i++) {
scanf("%lf%lf%lf%lf%lf%lf",&t[i].p1.x,&t[i].p1.y,&t[i].p2.x,&t[i].p2.y,&t[i].p3.x,&t[i].p3.y);
v.push_back((Node){std::min(std::min(t[i].p1.x,t[i].p2.x),t[i].p3.x),1,i});
v.push_back((Node){std::max(std::max(t[i].p1.x,t[i].p2.x),t[i].p3.x),-1,i});
}
std::sort(v.begin(),v.end());
int tmp=0,beg;
double ans=0;
for(register unsigned i=0;i<v.size();i++) {
if(tmp==0) beg=i;
tmp+=v[i].v;
if(tmp==0) {
for(register unsigned j=beg;j<=i;j++) {
c.push_back(v[j].id);
}
std::sort(c.begin(),c.end());
c.resize(std::unique(c.begin(),c.end())-c.begin());
const double &st=v[beg].p,&en=v[i].p;
ans+=asr(st,en,F(st),F((st+en)/2),F(en),1);
c.clear();
}
}
printf("%.2f\n",ans);
return 0;
}
[CQOI2005]三角形面积并的更多相关文章
- bzoj 1845: [Cqoi2005] 三角形面积并 扫描线
1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 848 Solved: 206[Submit][Statu ...
- BZOJ 1845: [Cqoi2005] 三角形面积并 [计算几何 扫描线]
1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 1151 Solved: 313[Submit][Stat ...
- 【BZOJ1845】[Cqoi2005] 三角形面积并 几何+扫描线
[BZOJ1845][Cqoi2005] 三角形面积并 Description 给出n个三角形,求它们并的面积. Input 第一行为n(N < = 100), 即三角形的个数 以下n行,每行6 ...
- CQOI2005 三角形面积并 和 POJ1177 Picture
1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 1664 Solved: 443[Submit][Stat ...
- BZOJ1845 [Cqoi2005] 三角形面积并 扫描线 计算几何
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1845 题意概括 给出n个三角形,求其面积并. 题解 有一个很经典的扫描线题目:矩形面积并.那个比较 ...
- BZOJ1845 : [Cqoi2005] 三角形面积并
求出所有交点后从左往右扫描线,用每段的中位线去截所有三角形,算出长度并后乘以该段长度即可,时间复杂度$O(n^3\log n)$. #include<cstdio> #include< ...
- BZOJ 1845: [Cqoi2005] 三角形面积并 (辛普森积分)
大力辛普森积分 精度什么的搞了我好久- 学到了Simpson的一个trick 深度开11,eps开1e-4.跑的比有些扫描线还快- CODE #include <bits/stdc++.h> ...
- ytu 1058: 三角形面积(带参的宏 练习)
1058: 三角形面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 190 Solved: 128[Submit][Status][Web Boar ...
- UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)
Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...
随机推荐
- 修改ElementUI源码样式
参考:https://segmentfault.com/a/1190000010932321
- vue指令问题
挂载点:最外层标签就是vue实例的挂载点,即id或者类对应的 dom节点 模板:指挂载点内部的内容,在实例里使用template标签来构 建 h1标签放在body里面不使用 “template”是一样 ...
- Jmeter 获取CSV行数
import java.io.BufferedReader; import java.io.FileInputStream; String str = "E:\\Desktop\\WOS接口 ...
- 矩阵乘法的运算量计算(华为OJ)
题目地址: https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b?tpId=37&&tqId=21293 ...
- MyBatis配置:在控制台打印SQL语句
1.在spring-mybatis.xml中配置语句 注意:value=”classpath:mybatis-config.xml”这个文件如果之前没有,是需要新建的 2.新建mybatis-con ...
- Oracle 11g 安装过程中“检查网络配置要求 未执行”解决方法
正在检查网络配置要求... 检查完成.此次检查的总体结果为: 未执行 网上查了一下,很多朋友都遇到这个问题而无从下手,其实解决起来很容易的. 只需要在 Windows XP 中安装 Microsoft ...
- IIS异常
http 错误 500.19 - internal server error 今天发布wcf到本地的IIS上,访问时出现了500.19错误.有效解决办法:是因为IIS没有安装完全,把能勾选的全部勾选上 ...
- NPOI导出Excel帮助类
工具类 using System; using System.Collections.Generic; using System.Data; using System.IO; using System ...
- nginx 301重定向一种实现方法
假设要使用的域名是b.com,以前的老域名是a.com,则以下设置让nginx把a.com的请求访问转发到b.com,并返回301给浏览器. server { listen 80; server_na ...
- 【技巧汇总】eclipse中如何跳转到指定行
技巧汇总 持续更新ing eclipse中如何跳转到指定行 ctrl+L