Description
Calculate the number of toys that land in each bin of a partitioned toy box.
Mom and Dad had a problem-their child John never puts his toys away when he was finished playing with them. They gave John a rectangular box to put he toys in, but John is rebellious and obeys he parents by simply throwing he t Oys into the box. All the toys get mixed up, and it's impossible for John to find his favorite toys.
John ' s parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys to the box, at least toys, then get thrown into different bins stay separated. The following diagram shows a top view of an example toy box.
For this problem, you is asked to determine what many toys fall into each partition as John throws them into the toy box.
Input
The input file contains one or more problems. The first line of a problem consists of six integers, N m x1 y1 x2 y2. The number of cardboard partitions is n (0 < n <=) and the number of toys is m (0 < M <= 5000). The coordinates of the upper-left corner and the lower-right corner of the box is (x1,y1) and (X2,y2), respectively. The following n lines contain, integers per line, Ui Li, indicating, the ends of the i-th cardboard partition are at The coordinates (UI,Y1) and (Li,y2). Assume that the cardboard partitions does not intersect each other and that they is specified in sorted order from Left-to-right. The next m lines contain and integers per line, Xj Yj specifying where the j-th toy have landed in the box. The order of the toy locations is random. Assume that no toy would land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0.
Output
The output for each problem would be a line for each separate bin in the Toy box. For each bin, print it bin number, followed by a colon and one space, followed by the number of the toys thrown into the bin . Bins is numbered from 0 (the leftmost bin) to N (the rightmost bin). Separate the output of different problems by a single blank line.
Sample Input
5 6 0 10 60 03 14 36 810 1015 301 52 12 85 540 107 94 10 0 10 100 020 2040 4060 6080 80 5 1015 1025 1035 1045 1055 1065 10 75 1085 1095 100
Sample Output
0:21:12:13:14:05:10:21:22:23:24:2
1#include <iostream>2#include <cstdio>3 #defineDB Double4#include <cstring>5#include <algorithm>6 using namespacestd;7 structline18 {9 intb;Ten }; One intcmpConstLine1 &x,ConstLine1 &y) A { - returnX.a>y.a; - } the intXmulti (intX1,intY1,intX2,intY2,intX0,inty0) - { - return(x1-x0) * (y2-y0)-(x2-x0) * (y1-y0); - } + Const intmaxn=5050; - line1 LIN1[MAXN]; + intANS[MAXN]; A intMain () at { - intN,m,x1,y1,x2,y2,c,d,first; -first=1; - while(SCANF ("%d", &n) = =1&&N) - { - if(first) infirst=0; - Else toprintf"\ n"); +memset (ans,0,sizeof(ans)); -scanf"%d%d%d%d%d",&m,&x1,&y1,&x2,&y2); the for(intI=0; i<n;i++) *scanf"%d%d",&lin1[i].a,&lin1[i].b); $Sort (lin1,lin1+n,cmp);Panax Notoginseng while(m--) - { thescanf"%d%d",&c,&d); + intTmp,l=0, r=N; A while(l<=R) the { + intMid= (L+R)/2; - if(Xmulti (LIN1[MID].A,Y1,LIN1[MID].B,Y2,C,D) >0) $ { $tmp=mid; -r=mid-1; - } the Else -L=mid+1;Wuyi } theans[tmp]++; - } Wu for(intI=0; i<=n;i++) -printf"%d:%d\n", i,ans[n-i]); About } $ return 0; -}
View Code
2318--toys