BZOJ-2618 [CQOI2006]凸多边形

半平面交模版题。。

#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <algorithm>
#define rep(i, l, r) for(int i=l; i<=r; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define maxn 1000
//#define double long double
#define linf 1e15
using namespace std;
typedef long long ll;
inline int read()
{
	int x=0, f=1; char ch=getchar();
	while (!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while (isdigit(ch)) x=x*10+ch-'0', ch=getchar();
	return x*f;
}
struct P{double x, y;} p[maxn];
P operator - (P a, P b){return (P){a.x-b.x, a.y-b.y};}
double operator * (P a, P b){return a.x*b.y-b.x*a.y;}
struct line{P a, b; double ang;} l[maxn], a[maxn], q[maxn];
bool operator < (line a, line b){if (a.ang==b.ang) return (a.b-a.a)*(b.a-a.a)>0; return a.ang<b.ang;}


inline P inter(line a, line b)
{
	double k1=(b.b-a.a)*(a.b-a.a), k2=(a.b-a.a)*(b.a-a.a), t=k2/(k1+k2);
	return (P){b.a.x+t*(b.b.x-b.a.x), b.a.y+t*(b.b.y-b.a.y)};
}
inline bool jud(line x, line y, line v){return (inter(x, y)-v.a)*(v.b-v.a)>0;}
int n, m, cnt;
int main()
{
	n=read();
	rep(i, 1, n)
	{
		int k=read();
		rep(j, 1, k) p[j].x=read(), p[j].y=read(); p[k+1]=p[1];
		rep(j, 1, k) l[++m].a=p[j], l[m].b=p[j+1];
	}
	rep(i, 1, m) l[i].ang=atan2(l[i].b.y-l[i].a.y, l[i].b.x-l[i].a.x);
	sort(l+1, l+1+m);
	rep(i, 1, m)
		a[l[i].ang!=a[cnt].ang ? ++cnt : cnt]=l[i];
	int L=1, R=0; q[++R]=a[1]; q[++R]=a[2];
	rep(i, 3, cnt)
	{
		while (L<R && jud(q[R-1], q[R], a[i])) R--;
		while (L<R && jud(q[L+1], q[L], a[i])) L++;
		q[++R]=a[i];
	}
	while (L<R && jud(q[R-1], q[R], q[L])) R--;
	while (L<R && jud(q[L+1], q[L], q[R])) L++;
	if (R-L<2) {puts("0.000"); return 0;}
	cnt=0; q[R+1]=q[L]; rep(i, L, R) p[++cnt]=inter(q[i], q[i+1]);
	double ans=0; 
	rep(i, 1, cnt-1) ans+=p[i].x*p[i+1].y; ans+=p[cnt].x*p[1].y;
	rep(i, 2, cnt) ans-=p[i].x*p[i-1].y; ans-=p[1].x*p[cnt].y;
	if (ans<0) ans*=-1;
	printf("%.3lf", ans/2);
	return 0;
}
posted @ 2015-05-05 18:46  NanoApe  阅读(198)  评论(0编辑  收藏  举报
AmazingCounters.com