这题没有模版真的写不出代码来。于是可耻的找了一份代码修改一下成了模版。。
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <queue> #include <stack> using namespace std; #define EPS 1e-10 #define inf 10000 #define zero(a) fabs(a)<EPS #define N 20005 using namespace std; struct cpoint{ double x,y; cpoint(double a=0,double b=0):x(a),y(b){} void get(){scanf("%lf%lf",&x,&y);} }p[N*2]; struct segline{ cpoint s,e; double angle; segline(cpoint a,cpoint b):s(a),e(b){} segline(){} void get_ang(){angle=atan2(e.y-s.y,e.x-s.x);} }re[N]; double x_mult(cpoint sp,cpoint ep,cpoint op){ return (sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y); } cpoint intersection(segline s1,segline s2){ double u=x_mult(s1.e,s2.s,s1.s),v=x_mult(s1.s,s2.e,s1.e); cpoint t; t.x=(s2.s.x*v+s2.e.x*u)/(u+v);t.y=(s2.s.y*v+s2.e.y*u)/(u+v); return t; } bool cmp(segline s1,segline s2){ if(s1.angle>s2.angle+EPS) return true;//先按极角排序 else if(zero(s1.angle-s2.angle)&& x_mult(s2.e,s1.e,s2.s)>-EPS) return true;//极角相等,内侧的在前 return false; } segline hp[N]; int hpi(segline re[],int n){ sort(re,re+n,cmp); int tmp=1; for(int i=1;i<n;i++) if(!zero(re[i].angle-re[tmp-1].angle)) re[tmp++]=re[i]; n=tmp; hp[0]=re[0];hp[1]=re[1]; int l=0,r=1; for(int i=2;i<n;i++){ while(l<r&&x_mult(re[i].e,intersection(hp[r],hp[r-1]),re[i].s)<-EPS) r--; while(l<r&&x_mult(re[i].e,intersection(hp[l],hp[l+1]),re[i].s)<-EPS) l++; hp[++r]=re[i]; } while(l<r&&x_mult(hp[l].e,intersection(hp[r],hp[r-1]),hp[l].s)<-EPS) r--; while(l<r&&x_mult(hp[r].e,intersection(hp[l],hp[l+1]),hp[r].s)<-EPS) l++; if(l==r) return 0; int m=0; for(int i=l;i<r;i++) p[m++]=intersection(hp[i],hp[i+1]); if(r>l+1) p[m++]=intersection(hp[l],hp[r]); return m;///m个点 } double solve(cpoint p[],int &n){ double area=0; for(int i=1;i<n-1;i++) area+=x_mult(p[i],p[i+1],p[0]); return fabs(area)/2.0; } int main(){ freopen("in.txt","r",stdin); int n; while(scanf("%d",&n)!=EOF){ for(int i=0;i<n;i++){ re[i].s.get(),re[i].e.get(); } re[n++]=segline(cpoint(0,0),cpoint(1e4,0)); re[n++]=segline(cpoint(1e4,0),cpoint(1e4,1e4)); re[n++]=segline(cpoint(1e4,1e4),cpoint(0,1e4)); re[n++]=segline(cpoint(0,1e4),cpoint(0,0)); for(int i=0;i<n;i++) re[i].get_ang(); int m = hpi(re,n); printf("%.1f\n",solve(p,m)); } return 0; }