题意:给你一个矩形 再给你一些线段 将矩形分成若干部分 再给你一些点 让你判断这些点在哪个区域内
思路:假设i点在区域a内 那么点i一定位于区域a左边线的右侧且位于右边线的左侧 由于点和区间太多因此要二分判断点在哪个区间
这题代码写的有点乱...但是思路就是我上面所说的 还算比较清晰吧...
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
#define REP( i, a, b ) for( int i = a; i < b; i++ )
#define CLR( a, x ) memset( a, x, sizeof a )
const int maxn = 5000 + 10;
const double eps = 1e-8;
struct Point{
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
void read(){ scanf("%lf%lf", &x, &y); }
} p[maxn];
typedef Point Vector;
int dcmp(double x){
if(fabs(x) < eps) return 0;
return x > eps ? 1 : -1;
}
Point operator - (const Point &A, const Point &B){ return Point(A.x - B.x, A.y - B.y); }
bool operator == (const Point &A, const Point &B){ return !dcmp(A.x - B.x) && !dcmp(A.y - B.y); }
double Cross(const Vector &A, const Vector &B){ return A.x * B.y - A.y * B.x; }
struct Area{
double left_up, left_down, right_up, right_down;
//分别表示一个区域内左右两条线段的端点的横坐标
Area() {}
Area(double a, double b, double c, double d) : left_up(a), left_down(b), right_up(c), right_down(d) {}
} area[maxn];
int n, m, ans[maxn], flag = 0;
double X1, Y1, X2, Y2;
void input(){
double l_u, l_d, r_u, r_d;
scanf("%d", &m);
scanf("%lf%lf%lf%lf", &X1, &Y1, &X2, &Y2);
l_u = X1, l_d = X1;
REP(i, 0, n){
scanf("%lf%lf", &r_u, &r_d);
area[i] = Area(l_u, l_d, r_u, r_d);
l_u = r_u, l_d = r_d;
}
area[n] = Area(l_u, l_d, X2, X2);
REP(i, 0, m) p[i].read();
}
int bi(int cur){
int l = 0, r = n, ans;
while(1){
int mid = (l + r) / 2;
if(Cross(p[cur]-Point(area[mid].left_down, Y2),Point(area[mid].left_up, Y1)-Point(area[mid].left_down,Y2))>eps&&
Cross(p[cur]-Point(area[mid].right_down,Y2),Point(area[mid].right_up, Y1)-Point(area[mid].right_down,Y2))<eps)
return mid;
else if(Cross(p[cur]-Point(area[mid].right_down, Y2),Point(area[mid].right_up,Y1)-Point(area[mid].right_down,Y2))>eps)
l = mid+1;
else r = mid-1;
}
}
void solve(){
CLR(ans, 0);
REP(i, 0, m) ans[bi(i)]++;
if(flag) printf("\n");
flag = 1;
REP(i, 0, n + 1) printf("%d: %d\n", i, ans[i]);
}
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d", &n) && n){
input();
solve();
}
return 0;
}