求助半平面交
查看原帖
求助半平面交
92254
Social_Zhao楼主2020/8/13 19:33

md,模板题不让下数据就tm离谱

错的是 #4 和 #6,都是第一列 read 1,except 0。

看起来像是有什么特判的地方没判到?

代码里面的 & 是叉积,其他的应该都是字面意思(

求 dalao 帮忙看下,谢谢 orz

#include<bits/stdc++.h>
using namespace std;

struct Point {
	double x, y;
	
	Point(){ x = 0, y = 0; }
	Point(double a, double b){ x = a, y = b; }
	friend Point operator +(Point a, Point b) { return (Point){a.x + b.x, a.y + b.y}; }
	friend Point operator -(Point a) { return (Point){-a.x, -a.y}; }
	friend Point operator -(Point a, Point b) { return a + (-b); }
	friend Point operator *(Point a, double b) { return (Point){a.x * b, a.y * b}; }
	friend Point operator /(Point a, double b) { return a * (1 / b); }
	friend double operator *(Point a, Point b) { return a.x * b.x + a.y * b.y; }
	friend double operator &(Point a, Point b) { return a.x * b.y - a.y * b.x; }
};

const double eps = 1e-4;

struct Line {
	Point p, v;
	Line(){}
	Line(Point a, Point b){ p = a, v = b; }
};

Point Intersection(Line a, Line b) {
	double s1 = a.v & (b.p - a.p), s2 = b.v & a.v;
	return b.p + b.v / s2 * s1;
}

int sign(double x) {
	if(fabs(x) < eps) return 0;
	else if(x > 0) return 1;
	return -1;
}

bool OnRight(Point a, Line b) {
	return sign(b.v & (a - b.p)) <= 0;
}

const int N = 1000005;
int n;
Point p[N];
Line edge[N];
int tot, l, r;
Line q[N];

bool cmp(Line a, Line b) {
	return atan2(a.v.y, a.v.x) < atan2(b.v.y, b.v.x);
}

int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		int mi; scanf("%d", &mi);
		for(int j = 1; j <= mi; j++) scanf("%lf%lf", &p[j].x, &p[j].y);
		for(int j = 1; j < mi; j++) edge[++tot] = (Line){p[j], p[j + 1] - p[j]};
		edge[++tot] = (Line){p[mi], p[1] - p[mi]};
	}
	sort(edge + 1, edge + 1 + tot, cmp);
	if(tot < 3 && printf("0.000")) return 0; 
	l = r = 1, q[1] = edge[1];
	for(int i = 2; i <= tot; i++) {
		while(l < r && OnRight(Intersection(q[r], q[r - 1]), edge[i])) r--;
		while(l < r && OnRight(Intersection(q[l], q[l + 1]), edge[i])) l++;
		q[++r] = edge[i];
	}
	while(l < r && OnRight(Intersection(q[l], q[l + 1]), q[r])) l++;
	while(l < r && OnRight(Intersection(q[r], q[r - 1]), q[l])) r--;
	if(r - l <= 1 && printf("0.000")) return 0; 
	tot = 0;
	for(int i = l; i < r; i++) p[++tot] = Intersection(q[i], q[i + 1]);
	p[++tot] = Intersection(q[l], q[r]);
	double ans = 0;
	for(int i = 1; i < tot; i++) ans += p[i] & p[i + 1];
	ans += p[tot] & p[1];
	printf("%.3lf", fabs(ans / 2));
	return 0;
}

2020/8/13 19:33
加载中...