http://poj.org/problem?id=2398
poj2318的升级版,只是中间的n个隔板未排序,输出的是装有i个玩具的区域的个数,其他完全一样
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<cmath> using namespace std; #define N 5110 #define INF 0x3f3f3f3f #define MOD #define met(a, b) memset(a, b, sizeof(a)) typedef long long LL; using namespace std; struct point { int x, y; }; struct line { point up, down; }p[N]; int ans[N]; bool cmp (line a, line b) { return a.down.x < b.down.x; } int xmult (point p1, point p2, point p0) { return (p1.x-p0.x) * (p2.y-p0.y) - (p2.x-p0.x) * (p1.y-p0.y); }///叉积 void Search (point Q, int n) { int l = 1, r = n; while (l < r) { int mid = (l + r) >> 1; if (xmult (Q, p[mid].up, p[mid].down) > 0) l = mid+1; else r = mid; } if (xmult (Q, p[l].up, p[l].down) > 0) ans[l+1]++; else ans[l]++; } int main () { int n, m, x1, y1, x2, y2; while (scanf ("%d", &n), n) { met (p, 0); met (ans, 0); scanf ("%d %d %d %d %d", &m, &x1, &y1, &x2, &y2); for (int i=1; i<=n; i++) { int u, l; scanf ("%d %d", &u, &l); p[i].up = (point) {u, y1}; p[i].down = (point) {l, y2}; } sort (p+1, p+n+1, cmp); point Q; for (int i=0; i<m; i++) { scanf ("%d %d", &Q.x, &Q.y); Search (Q, n); } sort (ans+1, ans+n+2); int cnt = 0, sum = 0; puts ("Box"); for (int i=1; i<=n+2; i++) { if (ans[i] && !cnt) { cnt = ans[i]; sum = 0; } if (ans[i] == cnt && cnt) sum++; else if (cnt && ans[i] != cnt) { printf ("%d: %d\n", cnt, sum); cnt = ans[i]; sum = 1; } } } return 0; }