poj2318 水题(二分+叉积)
题目链接:http://poj.org/problem?id=2318
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<queue> using namespace std; const int maxn = 5105; const int maxe = 20000; const int INF = 0x3f3f3f; const double eps = 1e-8; const double PI = acos(-1.0); struct Point{ double x,y; Point(double x=0, double y=0) : x(x),y(y){ } //构造函数 }; typedef Point Vector; Vector operator + (Vector A , Vector B){return Vector(A.x+B.x,A.y+B.y);} Vector operator - (Vector A , Vector B){return Vector(A.x-B.x,A.y-B.y);} Vector operator * (Vector A , double p){return Vector(A.x*p,A.y*p);} Vector operator / (Vector A , double p){return Vector(A.x/p,A.y/p);} bool operator < (const Point& a,const Point& b){ return a.x < b.x ||( a.x == b.x && a.y < b.y); } int dcmp(double x){ if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } bool operator == (const Point& a, const Point& b){ return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; } double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; } double Cross(Vector A, Vector B) { return A.x*B.y - A.y * B.x; } double Length(Vector A) { return sqrt(Dot(A,A)); } /******************************分割线*******************************/ Point P[maxn][2]; int N,M; int main() { //freopen("E:\\acm\\input.txt","r",stdin); while(scanf("%d",&N)==1 && N){ scanf("%d",&M); double x1,y1,x2,y2; scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2); P[0][0] = Point(x1,y2); P[0][1] = Point(x1,y1); P[N+1][0] = Point(x2,y2); P[N+1][1] = Point(x2,y1); for(int i=1;i<=N;i++){ double Ui,Li; scanf("%lf %lf",&Ui,&Li); P[i][0] = Point(Li,y2); P[i][1] = Point(Ui,y1); } int num[maxn]; memset(num,0,sizeof(num)); for(int i=1;i<=M;i++){ Point A; scanf("%lf %lf",&A.x,&A.y); int L=0, R=N+1; while(L < R-1){ int mid = L + (R-L)/2; if(dcmp(Cross(P[mid][1]-P[mid][0],A-P[mid][0])) > 0) R = mid; else L = mid; } num[L]++; } for(int i=0;i<=N;i++){ printf("%d: %d\n",i,num[i]); } printf("\n"); } }