题目意思很简单,一个盒子,被几条直线分成几个部分,然后有一些随机分布的点,问各部分中点的个数分别为多少。
基本思路是判断点在直线的左边还是右边,这里要用到向量积,在不同边向量积是正负性是不同的。只要找到一个区间,左边的直线和右边的直线同这个点,得到的向量积正负性不同,即可。查找的方法是二分查找,至于查找的方向,也是由向量积决定 的。
#include<iostream>
using namespace std;
typedef struct{
int x, y;
} POINT;
POINT board[15000];
int result[7000];
int vectProduct(POINT o, POINT a, POINT b){
return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);
}
void init(int x1, int y1, int x2, int y2, int i){
board[i].x = x1;
board[i].y = y1;
board[i + 1].x = x2;
board[i + 1].y = y2;
}
int binarySearch(int s, int e, POINT a){
int mid = ((s + e) >> 1);
int p1 = vectProduct(board[mid * 2], board[mid * 2 + 1], a);
int p2 = vectProduct(board[mid * 2 + 2], board[mid * 2 + 3], a);
if(p1 < 0 && p2 < 0)
return binarySearch(mid + 1, e, a);
else if(p1 > 0 && p2 > 0)
return binarySearch(s, mid - 1, a);
else
return mid;
}
void print(POINT a[], int b){
for(int i = 0; i < b; i++)
cout << a[i].x << " " << a[i].y << endl;
}
int main(){
int n, m, x1, y1, x2, y2;
while(cin >> n){
if(n == 0)
break;
cin >> m >> x1 >> y1 >> x2 >> y2;
init(x1, y2, x1, y1, 0);
init(x2, y2, x2, y1, 2 * (n + 1));
for(int i = 1; i <= n; i++){
int a, b;
cin >> a >> b;
init(b, y2, a, y1, 2 * i);
}
// print(board, 2 * n + 4);
for(int i = 0; i <= n; i++)
result[i] = 0;
for(int i = 0; i < m; i++){
POINT toy;
cin >> toy.x >> toy.y;
result[binarySearch(0, n, toy)]++;
//cout << binarySearch(0, n, toy) << endl;
}
for(int i = 0; i <= n; i++)
cout << i << ": " << result[i] <<endl;
cout << endl;
}
}