题目链接:http://poj.org/problem?id=2318
题解:叉积+二分 求出每一个toy所在的区域
#include<cstdio> #include<cmath> #include<cstring> struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} }; typedef Point Vector; //Vector 为 Point的别名 //向量+向量=向量 点+向量=点 Vector operator + (Vector A, Vector B) {return Vector(A.x+B.x, A.y+B.y);} //点-点=向量 Vector operator - (Point A, Point 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); } const double eps = 1e-10; int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } //叉积:两向量v和w的叉积等于v和w组成的三角形的有向面积的两倍 XaYb - XbYa double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } #define MAXN 5050 Point up[MAXN], low[MAXN]; int main () { int n, m, x1, y1, x2, y2; while(scanf("%d", &n), n) { scanf("%d %d %d %d %d", &m, &x1, &y1 , &x2, &y2); up[0].x = x1, up[0].y = y1; low[0].x = x1, low[0].y = y2; up[n+1].x = x2, up[n+1].y = y1; low[n+1].x = x2, low[n+1].y = y2; for(int i = 1; i <= n; i++) { scanf("%lf %lf", &up[i].x, &low[i].x); up[i].y = y1; low[i].y = y2; } int ans[MAXN] = {0}; for(int i = 1; i <= m; i++) { Point tmp; scanf("%lf %lf", &tmp.x, &tmp.y); //二分 int l = 0, r = n+1; while(l < r) { int M = l + (r-l)/2; if(dcmp(Cross(up[M]-low[M], tmp-low[M]))>0) r = M; else l = M+1; } ans[l-1]++; } for(int i = 0; i <= n; i++) printf("%d: %d\n", i, ans[i]); printf("\n"); } return 0; }