[hdu4629 Burning]三角形面积并,扫描线
题意:给n个三角形,分别求覆盖1次~n次的总面积
思路: 对每个y坐标作一条平行于x轴的直线,按直线从下往上处理,每两条直线之间为若干梯形(也可以是三角形)首尾相连的情况,从左扫到右时,用一个变量cnt记录当前区域被覆盖的次数,遇到入边cnt++,遇到出边cnt--,边扫边更新答案。入边表示这条边的右边在三角形内部,出边表示这条边的右边在三角形的外部。思路并不复杂,只是代码稍长点。
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; //#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
//#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
template<typename T>
void V2A(T a[],const vector<T>&b){for(int i=;i<b.size();i++)a[i]=b[i];}
template<typename T>
void A2V(vector<T>&a,const T b[]){for(int i=;i<a.size();i++)a[i]=b[i];} const double PI = acos(-1.0);
const int INF = 1e9 + ;
const double EPS = 1e-8; /* -------------------------------------------------------------------------------- */ const int maxn = ; int dcmp(double a) {
if (abs(a) < EPS) return ;
return a > ? : -;
} struct Point {
double x, y;
void read() {
int x, y;
scanf("%d%d", &x, &y);
this->x = x;
this->y = y;
}
Point(double x, double y) {
this->x = x;
this->y = y;
}
Point() {}
Point operator - (const Point &that) const {
return Point(this->x - that.x, this->y - that.y);
}
bool operator < (const Point &that) const {
return dcmp(this->y - that.y) < || dcmp(this->y - that.y) == &&
dcmp(this->x - that.x) < ;
}
Point operator * (const double &m) const {
return Point(this->x * m, this->y * m);
}
};
struct Segment {
Point a, b;
Segment(Point a, Point b) {
this->a = a;
this->b = b;
}
Segment() {}
bool operator < (const Segment &that) const {
return dcmp(this->a.x + this->b.x - that.a.x - that.b.x) < ;
}
};
Point point[maxn * maxn * ];
Segment seg[maxn * ];
int type[maxn * ];
double ans[maxn]; double cross(const Point &a, const Point &b) {
return a.x * b.y - a.y * b.x;
} bool onMid(Point a, Point b, Point c) {
return dcmp(cross(c - a, b - a)) == ;
}
bool onLeft(Point a, Point b, Point c) {
return dcmp(cross(c - a, b - a)) < ;
}
bool Intersect(Segment A, Segment B) {
int r1 = dcmp(cross(A.b - A.a, B.a - A.a));
int r2 = dcmp(cross(A.b - A.a, B.b - A.a));
int r3 = dcmp(cross(B.b - B.a, A.a - B.a));
int r4 = dcmp(cross(B.b - B.a, A.b - B.a));
return (r1 ^ r2) || (r3 ^ r4);
}
Point getLineIntersection(Segment A, Segment B) {
Point u = A.a - B.a, v = A.b - A.a, w = B.b - B.a;
double t = cross(w, u) / cross(v, w);
return A.a - v * -t;
}
double Area(Segment A, Segment B) {
return fabs((A.a.x - B.a.x + A.b.x - B.b.x) * (A.a.y - A.b.y) / );
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int T, n, cs, cp;
cin >> T;
while (T --) {
cin >> n;
cp = cs = ;
for (int i = ; i < n; i ++) {
Point p[];
for (int j = ; j < ; j ++) p[j].read();
if (onMid(p[], p[], p[])) continue;
sort(p, p + );
seg[cs ++] = Segment(p[], p[]);
seg[cs ++] = Segment(p[], p[]);
seg[cs ++] = Segment(p[], p[]);
if (onLeft(p[], p[], p[])) {
type[cs - ] = type[cs - ] = ;
type[cs - ] = -;
}
else {
type[cs - ] = type[cs - ] = -;
type[cs - ] = ;
}
}
for (int i = ; i < cs; i ++) {
for (int j = i + ; j < cs; j ++) {
if (Intersect(seg[i], seg[j]))
point[cp ++] = getLineIntersection(seg[i], seg[j]);
}
}
sort(point, point + cp);
vector<double> Y;
for (int i = ; i < cp; i ++) {
if (!i || dcmp(point[i].y - point[i - ].y) != ) Y.pb(point[i].y);
}
fillchar(ans, );
for (int i = ; i < Y.size(); i ++) {
vector<pair<Segment, int> > S;
for (int j = ; j < cs; j ++) {
if (dcmp(seg[j].a.y - Y[i - ]) <= && dcmp(seg[j].b.y - Y[i]) >= ) {
Point a = seg[j].a, b = seg[j].b;
double d = (b.x - a.x) / (b.y - a.y);
double x1 = a.x + d * (Y[i - ] - a.y), x2 = a.x + d * (Y[i] - a.y);
S.pb(mp(Segment(Point(x1, Y[i - ]), Point(x2, Y[i])), type[j]));
}
}
sort(all(S));
int cnt = ;
for (int j = ; j < S.size(); j ++) {
if (cnt) ans[cnt] += Area(S[j - ].X, S[j].X);
cnt += S[j].Y;
}
}
for (int i = ; i <= n; i ++) {
printf("%.10f\n", ans[i]);
}
} return ;
}
[hdu4629 Burning]三角形面积并,扫描线的更多相关文章
- bzoj 1845: [Cqoi2005] 三角形面积并 扫描线
1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 848 Solved: 206[Submit][Statu ...
- BZOJ1845 [Cqoi2005] 三角形面积并 扫描线 计算几何
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1845 题意概括 给出n个三角形,求其面积并. 题解 有一个很经典的扫描线题目:矩形面积并.那个比较 ...
- 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 ...
- 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 ...
- OpenJudge计算概论-计算三角形面积【海伦公式】
/*============================================== 计算三角形面积 总时间限制: 1000ms 内存限制: 65536kB 描述 平面上有一个三角形,它的 ...
- nyoj 67 三角形面积【三角形面积公式】
三角形面积 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 给你三个点,表示一个三角形的三个顶点,现你的任务是求出该三角形的面积 输入 每行是一组测试数据,有6个 ...
随机推荐
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- 为什么选择python?
Why python? 那些最好的程序员不是为了得到更高的薪水或者得到公众的仰慕而编程,他们只是觉得这是一件有趣的事情. —— Linux 之父 Linux Torvalds 作为一个使用主义的学习者 ...
- XSS语义分析的阶段性总结(一)
本文作者:Kale 前言 由于X3Scan的研发已经有些进展了,所以对这一阶段的工作做一下总结!对于X3Scan的定位,我更加倾向于主动+被动的结合.主动的方面主要体现在可以主动抓取页面链接并发起请求 ...
- 域名和服务器绑定及https协议更换
服务器是之前已经购买了的 1.腾讯云产品中搜索域名注册(产品太多了懒得找,直接搜索来得快些) 2.进去之后可以选择各种后缀的域名,输入自己喜欢的,看看哪些后缀是没有被注册的.自己挑选一个就可以,按照指 ...
- 从源码解读Spring如何解决bean循环依赖
1 什么是bean的循环依赖 循环依赖的原文是circular reference,指多个对象相互引用,形成一个闭环. 以两个对象的循环依赖为例: Spring中的循环依赖有 3 种情况: 构造器(c ...
- jQuery的attr和prop属性
<div id="div1"></div> attr: 首先是一个参数的attr. $("#div").attr("id&qu ...
- 当git上只做文件大小写重命名的修改时,如何躲坑
一. 提交时 假设修改ABC.java为Abc.java. 1.1 如果使用git命令进行仅涉及大小写的重命名 1.1.1 设置git库为大小写敏感(不建议) $ git config core.ig ...
- 牛客网练习赛61 A+B
A.打怪 思路:先判定当小怪的攻击力为0时,你能杀无数只怪,因为小怪A不动你,然后再计算每个小怪最多能给你造成多少伤害(用小怪的血量除以你的攻击力,也就是你砍它几下它会死,你先手,所以小怪肯定比你少砍 ...
- 创建线程的方式三:实现Callable接口-----JDK5.0 新增
package com.yhqtv.java2; /* * 创建线程的方式三:实现Callable接口-----JDK5.0 新增 * * 如何理解实现Callable接口的方式创建多线程比实现Run ...
- 播放声音 (c++) (windows)
自己看自己看自己看自己看自己看自己看 在<windows.h>中 一:BOOL WINAPI MessageBeep (_in UINT uType ); 播放一个波形文件 (也就是wac ...