BZOJ 1845: [Cqoi2005] 三角形面积并 [计算几何 扫描线]
1845: [Cqoi2005] 三角形面积并
Time Limit: 3 Sec Memory Limit: 64 MB
Submit: 1151 Solved: 313
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
0.0 0.0 2.0 0.0 1.0 1.0
1.0 0.0 3.0 0.0 2.0 1.0
Sample Output
//
// main.cpp
// bzoj1845
//
// Created by Candy on 2017/2/1.
// Copyright © 2017年 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const double eps=1e-;
const double INF=1e9;
const int N=; inline int sgn(double x){
if(abs(x)<eps) return ;
else return x<?-:;
} struct Vector{
double x,y;
Vector(double a=,double b=):x(a),y(b){}
bool operator <(const Vector &a)const{
//return x<a.x||(x==a.x&&y<a.y);
return sgn(x-a.x)<||(sgn(x-a.x)==&&sgn(y-a.y)<);
}
void print(){printf("%lf %lf\n",x,y);}
};
typedef Vector Point;
Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==&&sgn(a.y-b.y)==;} double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;} struct Line{
Point s,t;
int id;
Line(){}
Line(Point s,Point t,int i):s(s),t(t),id(i){}
Line(Point s,Point t):s(s),t(t){}
}L[N];
int nl;
Point LI(Line a,Line b){
Vector v=a.s-b.s,v1=a.t-a.s,v2=b.t-b.s;
double t=Cross(v2,v)/Cross(v1,v2);
return a.s+v1*t;
}
bool isLSI(Line l1,Line l2){
Vector v=l1.t-l1.s,u=l2.s-l1.s,w=l2.t-l1.s;
return sgn(Cross(v,u))!=sgn(Cross(v,w));
}
struct Triangle{
Point a,b,c;
Triangle(){}
Triangle(Point a,Point b,Point c):a(a),b(b),c(c){}
}t[N];
int n;
double mp[];int m;
void iniMP(){
sort(mp+,mp++m);
int p=;
mp[++p]=mp[];
for(int i=;i<=m;i++) if(mp[i]!=mp[i-]) mp[++p]=mp[i];
m=p;
}
Point a,b,c;
struct Interval{
double l,r;
bool operator <(const Interval &a) const{
return l==a.l?r<a.r:l<a.l;
}
Interval(double a=,double b=):l(a),r(b){}
}in[N];
double solve(double x){
int m=;
for(int i=;i<=n;i++){
a=t[i].a,b=t[i].b,c=t[i].c;
if(sgn(x-min(a.x,min(b.x,c.x)))<||sgn(x-max(a.x,max(b.x,c.x)))> ) continue;
Line l1(a,b),l2(a,c),l3(b,c),l(Point(x,),Point(x,));
Point p[];int cnt=;
if(isLSI(l,l1)) p[++cnt]=LI(l,l1);
if(isLSI(l,l2)) p[++cnt]=LI(l,l2);
if(isLSI(l,l3)&&cnt!=) p[++cnt]=LI(l,l3);
in[++m]=Interval(min(p[].y,p[].y),max(p[].y,p[].y));
}
sort(in+,in++m);
double last=-INF,re=;
for(int i=;i<=m;i++){
if(in[i].l>last) re+=in[i].r-in[i].l,last=in[i].r;
else if(in[i].r>last) re+=in[i].r-last,last=in[i].r;
}
return re;
}
int main(int argc, const char * argv[]) {
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y);
t[i]=Triangle(a,b,c);
mp[++m]=a.x;mp[++m]=b.x;mp[++m]=c.x;
L[++nl]=Line(a,b,i);L[++nl]=Line(a,c,i);L[++nl]=Line(b,c,i);
}
for(int i=;i<=n*;i++)
for(int j=i+;j<=n*;j++)
if(L[i].id!=L[j].id&&sgn(Cross(L[i].t-L[i].s,L[j].t-L[j].s))!=)
mp[++m]=LI(L[i],L[j]).x;
iniMP();
double ans=;
for(int i=;i<m;i++){
if(sgn(mp[i+]-mp[i])>){
double x=(mp[i]+mp[i+])/;
ans+=solve(x)*(mp[i+]-mp[i]);
}
}
printf("%.2lf",ans-eps);
return ;
}
BZOJ 1845: [Cqoi2005] 三角形面积并 [计算几何 扫描线]的更多相关文章
- bzoj 1845: [Cqoi2005] 三角形面积并 扫描线
1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 848 Solved: 206[Submit][Statu ...
- BZOJ 1845: [Cqoi2005] 三角形面积并 (辛普森积分)
大力辛普森积分 精度什么的搞了我好久- 学到了Simpson的一个trick 深度开11,eps开1e-4.跑的比有些扫描线还快- CODE #include <bits/stdc++.h> ...
- 【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 ...
- [CQOI2005]三角形面积并
[CQOI2005]三角形面积并 题目大意: 求\(n(n\le100)\)个三角形的面积并. 思路: 自适应辛普森法,玄学卡精度可过. 源代码: #include<cmath> #inc ...
- BZOJ1845 [Cqoi2005] 三角形面积并 扫描线 计算几何
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1845 题意概括 给出n个三角形,求其面积并. 题解 有一个很经典的扫描线题目:矩形面积并.那个比较 ...
- BZOJ1845 : [Cqoi2005] 三角形面积并
求出所有交点后从左往右扫描线,用每段的中位线去截所有三角形,算出长度并后乘以该段长度即可,时间复杂度$O(n^3\log n)$. #include<cstdio> #include< ...
- [hdu4629 Burning]三角形面积并,扫描线
题意:给n个三角形,分别求覆盖1次~n次的总面积 思路: 对每个y坐标作一条平行于x轴的直线,按直线从下往上处理,每两条直线之间为若干梯形(也可以是三角形)首尾相连的情况,从左扫到右时,用一个变量cn ...
- BZOJ 1845三角形面积并
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1845 给定100个三角形,求三角形面积并. 戴神模板太可怕.直接调用函数秒掉.思路 ...
随机推荐
- 什么是 core dump ? 以及如何使用gdb对 core dumped 进行调试
什么是core dump?(down = 当) core的意思是:内存,dump的意思是:扔出来.堆出来. 开发和使用linux程序时,有时程序莫名其妙的down掉了,却没有任何的提示(有时候会提示c ...
- Coins、Tokens、山寨币:区别在哪里
[译] Coins, Tokens & Altcoins: What's the Difference? coin:指"正宗的"数字货币,比如比特币 tokens:比如用 ...
- typedef和define具体的详细区别
1) #define是预处理指令,在编译预处理时进行简单的替换,不作正确性检查,不关含义是否正确照样带入,只有在编译已被展开的源程序时才会发现可能的错误并报错.例如: #define PI 3.141 ...
- Hive HQL学习
HQL学习 1.hive的数据类型 2.hive_DDL 2.1创建.删除.修改.使用数据库 Default数据库,默认的,优先级相对于其他数据库是最高的 2.2重点:创建表_内部表_ ...
- SVN报Previous operation has not finished; run 'cleanup'&
做着项目突然SVN报Previous operation has not finished; run 'cleanup' if it was interrupted,进度又要继续,烦.百度一下发现很多 ...
- 提交到svn服务器时,一直缓冲不,
1,服务器端url问题,正常:https://192.168.1.102:8443/svn/dedegls/ 错误:https:// ...
- win7 64位wamp2.5无法启动MSVCR110.DLL丢失听语音
从网上下载wampserver2.5 64位的PHP集成环境,根本无法使用,说是丢失了MSVCR110.DLL,然后再网上找了一大堆资料工具都无用,比如下微软的了vcredist_x64,重新卸载安装 ...
- 在MAC电脑上抓取iphone数据包的方法
一.说明: 1.整个抓包操作的过程中,手机必须一直通过USB链接MAC电脑 2.手机系统要求在IOS5以上,因为使用的RVI技术在IOS5以后的系统中才有 3.抓包过程中,手机可以使用任何网络2G.3 ...
- .netCore数据库迁移
程序包管理器控制台下Nuget 命令: 初始迁移命令: add-migration init -Context DAL.ProductContext 全称:migrations add Initial ...
- IDEA关掉重复代码波浪线
如图: File----Settings