POJ2318的升级版,就是增加了边界的排序。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 5111
#define x1 x_1
#define x2 x_2
#define y1 y_1
#define y2 y_2
#define find Find
struct node {
double x1, y1, x2, y2;
double x, y;
bool operator < (const node &a) const {
return x < a.x;
}
}a[maxn], b[maxn];
int n, m, ans[maxn], cnt[maxn];
double x1, y1, x2, y2;
bool cmp (const node &a, const node &b) {
return a.x1 < b.x1;
}
void find (int pos) {
double h = b[pos].y;
for (int i = 0; i < n; i++) {
double rate = (h-a[i].y1)/(a[i].y2-a[i].y1);
a[i].x = a[i].x1 + rate*(a[i].x2-a[i].x1);
}
int p = upper_bound (a, a+n, b[pos]) - a-1;
ans[p]++;
}
bool legal (double x, double y) {
if (x < x1 || x > x2 || y < y2 || y > y1)
return 0;
return 1;
}
int main () {
//freopen ("in", "r", stdin);
while (scanf ("%d", &n) == 1 && n) {
scanf ("%d%lf%lf%lf%lf", &m, &x1, &y1, &x2, &y2);
a[0].x1 = x1, a[0].y1 = y2, a[0].x2 = x1, a[0].y2 = y1;
double u, v;
for (int i = 1; i <= n; i++) {
scanf ("%lf%lf", &u, &v);
a[i].x1 = v, a[i].x2 = u;
a[i].y1 = y2, a[i].y2 = y1;
}
n++;
a[n].x1 = x2, a[n].y1 = y2;
a[n].x2 = x2, a[n].y2 = y1;
n++;
sort (a, a+n, cmp);
memset (ans, 0 ,sizeof ans);
for (int i = 1; i <= m; i++) {
scanf ("%lf%lf", &u, &v);
b[i].x = u, b[i].y = v;
if (!legal (u, v))
continue;
find (i);
}
printf ("Box\n");
memset (cnt, 0, sizeof cnt);
for (int i = 0; i < n-1; i++) {
cnt[ans[i]]++;
}
for (int i = 1; i <= n; i++) {
if (cnt[i])
printf ("%d: %d\n", i, cnt[i]);
}
}
return 0;
}