题意:图片是一个俯视图,按照从左到右的顺序输入边 然后输入点坐标,问从左到右的各个区域分别有多少玩具。
已知顺序只需要二分来确定区间,然后得出结果。
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct point { double x,y; }; struct edge { point up,down; }; double Direction(point a,point b,point c) { return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y); } bool On_Segment(point pi,point pj,point pk) { if(pk.x>=min(pi.x,pj.x)&&pk.x<=max(pi.x,pj.x)&&pk.y>=min(pi.y,pj.y)&&pk.y<=max(pi.y,pj.y)) return 1; return 0; } bool Segment_Intersect(point p1,point p2,point p3,point p4) { double d1=Direction(p3,p4,p1),d2=Direction(p3,p4,p2),d3=Direction(p1,p2,p3),d4=Direction(p1,p2,p4); if(((d1>0&&d2<0)||(d1<0&&d2>0))&&((d3>0&&d4<0)||(d3<0&&d4>0))) return 1; if(d1==0&&On_Segment(p3,p4,p1)) return 1; if(d2==0&&On_Segment(p3,p4,p2)) return 1; if(d3==0&&On_Segment(p1,p2,p3)) return 1; if(d4==0&&On_Segment(p1,p2,p4)) return 1; return 0; } int Pandingdian(point a,edge l,edge r)//1在多边形上 2在多边形外 0在多边形内 { point b,polygon[6]; b.x=-9999999,b.y=a.y; int n=4,sum=0; polygon[0]=l.up,polygon[1]=l.down,polygon[2]=r.down,polygon[3]=r.up; polygon[n]=polygon[0]; for(int i=1; i<=n; i++) if(polygon[i].y-polygon[i-1].y!=0&&Segment_Intersect(a,b,polygon[i],polygon[i-1])) { if(Direction(a,polygon[i],polygon[i-1])==0) return 1; sum++; } if(sum&1) return 0; return 2; } point lt,rb,temp; edge data[5005]; int main() { int n,m,ans[5005],s=0; while(~scanf("%d",&n),n) { scanf("%d%lf%lf%lf%lf",&m,<.x,<.y,&rb.x,&rb.y); if(s) printf("\n"); s++; memset(ans,0,sizeof(ans)); data[0].up=lt,data[0].down.x=lt.x,data[0].down.y=rb.y; point u,d; u.y=lt.y,d.y=rb.y; for(int i=1; i<=n; i++) scanf("%lf%lf",&u.x,&d.x),data[i].up=u,data[i].down=d; data[n+1].down=rb,data[n+1].up.y=lt.y,data[n+1].up.x=rb.x; while(m--) { scanf("%lf%lf",&temp.x,&temp.y); int l=0,r=n+1,mid; while(l<r) { mid=(l+r)/2; int w=Pandingdian(temp,data[mid],data[r]); if(w==0||w==1) { if(r-mid==1) { ans[mid]++; break; } l=mid; } else if(mid-l==1) { ans[l]++; break; } else r=mid; } } for(int i=0; i<=n; i++) printf("%d: %d\n",i,ans[i]); } return 0; }