Description
Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for Reza to find his favorite toys anymore.
Reza's parents came up with the following idea. They put cardboard partitions into the box. Even if Reza keeps throwing his toys into the box, at least toys that get thrown into different partitions stay separate. The box looks like this from the top:
We want for each positive integer t, such that there exists a partition with t toys, determine how many partitions have t, toys.
Input
The input consists of a number of cases. The first line consists of six integers n, m, x1, y1, x2, y2. The number of cardboards to form the partitions is n (0 < n <= 1000) and the number of toys is given in m (0 < m <= 1000). The coordinates of the upper-left corner and the lower-right corner of the box are (x1, y1) and (x2, y2), respectively. The following n lines each consists of two integers Ui Li, indicating that the ends of the ith cardboard is at the coordinates (Ui, y1) and (Li, y2). You may assume that the cardboards do not intersect with each other. The next m lines each consists of two integers Xi Yi specifying where the ith toy has landed in the box. You may assume that no toy will land on a cardboard.
A line consisting of a single 0 terminates the input.
Output
For each box, first provide a header stating "Box" on a line of its own. After that, there will be one line of output per count (t > 0) of toys in a partition. The value t will be followed by a colon and a space, followed the number of partitions containing t toys. Output will be sorted in ascending order of t for each box.
Sample Input
4 10 0 10 100 0
20 20
80 80
60 60
40 40
5 10
15 10
95 10
25 10
65 10
75 10
35 10
45 10
55 10
85 10
5 6 0 10 60 0
4 3
15 30
3 1
6 8
10 10
2 1
2 8
1 5
5 5
40 10
7 9
0
Sample Output
Box
2: 5
Box
1: 4
2: 1
Source
Tehran 2003 Preliminary
//题意:一个箱子由一些纸板隔开分成n+1个区域。给出一些玩具的落点(x,y),问分别含有i个玩具的区域个数。
//利用叉积性质,点与线段的两端点叉积<0表示点在线段的左边,可以对每个点查找在它右边的最靠近它的线段即点在改该区间内。由于n,m最大只有1000,可以o(n^2)查询也可以利用二分。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;
#define maxn 1005
#define eps 1e-8
struct point
{
int x,y;
}P[maxn];
struct line
{
point sp,ep;
}L[maxn];
int vist[maxn],ans[maxn];
int Fabs(double d)
{
if(fabs(d)<eps) return 0;
else return d>0?1:-1;
}
bool cmp(line a,line b)
{
if(a.sp.x!=b.sp.x) return a.sp.x<b.sp.x;
else return a.ep.x<b.ep.x;
}
int x_multi(point p1,point p2,point p3)
{
return (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y);
}
/*void Solve(point p,int n)
{
int i;
for(i=1;i<=n;i++)
if(Fabs(x_multi(p,L[i].sp,L[i].ep))<0)
{
vist[i]++;
return;
}
vist[i]++;
}*/
void Solve(point p,int n)
{
int l=1,r=n,mid=(l+r)>>1,flag=0,i;
while(l<=r)
{
if(Fabs(x_multi(p,L[mid].sp,L[mid].ep))<0)
flag=1,i=mid,r=mid-1;
else l=mid+1;
mid=(l+r)>>1;
}
if(!flag) i=n+1;
vist[i]++;
}
int main()
{
int n,m,i,j;
int x1,y1,x2,y2,x3,x4;
while(scanf("%d",&n),n)
{
scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2);
for(i=1;i<=n;i++)
{
scanf("%d%d",&x3,&x4);
L[i].sp.x=x3,L[i].sp.y=y1;
L[i].ep.x=x4,L[i].ep.y=y2;
}
sort(L+1,L+n+1,cmp);
memset(vist,0,sizeof(vist));
memset(ans,0,sizeof(ans));
for(i=1;i<=m;i++)
{
scanf("%d%d",&P[i].x,&P[i].y);
Solve(P[i],n);
}
for(i=1;i<=n+1;i++)
if(vist[i])
ans[vist[i]]++;
puts("Box");
for(i=1;i<=m;i++)
if(ans[i])
printf("%d: %d\n",i,ans[i]);
}
return 0;
}