TOYS
Time Limit:2000 MS |
|
Memory Limit:65536 K |
Total Submissions:9023 |
|
Accepted:4290 |
Description
Calculate the number of toys that land in each bin of a partitioned toy box.
Mom and dad have a problem-their child John never puts his toys away when he is finished playing with them. they gave John a rectangular box to put his toys in, but John is rebellious and obeys his parents by simply throwing his toys into the box. all
Toys get mixed up, and it is 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 into the box, at least toys that get thrown into different bins stay separated. the following dimo-shows a top view of an example
Toy box.
For this problem, you are asked to determine how 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 <= 5000) 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 are (x1, y1) and (x2, y2), respectively. the following n lines contain two integers per line, Ui Li, indicating that the ends of the I-th cardboard partition is at the coordinates (Ui, y1) and (Li, y2 ).
You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. the next m lines contain two integers per line, Xj Yj specifying where the j-th toy has landed in the box. the order
The toy locations is random. You may assume that no toy will 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 will be one line for each separate bin in the toy box. for each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. bins are 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 1075 1085 1095 100
Sample Output
0: 21: 12: 13: 14: 05: 10: 21: 22: 23: 24: 2
Hint
As the example between strates, toys that fall on the boundary of the box are "in" the box.
Code:
Only one edge on the right of each grid exists in the array,
If <0 indicates that the point should be in the clockwise direction of the line segment, that is, the right side;
If the value is greater than 0, it indicates that the point should be in the clockwise direction of the line segment, that is, the left side.
In particular, if it is = 0, it means that toy falls on the grid. As suggested, I put it in the case of <0
It is obviously very convenient to search directly from the first search to the end. You only need to determine the size of the cross product.
In order to save time, I used binary search, which of course caused a crash and added judgment conditions (note below)
The result is that I have a deeper understanding of the nature of binary search. Remember!
# Include <stdio. h> # include "string. h "typedef struct par {int x1, x2;} par; par box [5005]; int res [5005], n, zsx, zsy, yxx, yxy; double calc (int x1, int y1, int x2, int y2, int x3, int y3) {return (x1-x2) * (y1-y3)-(y1-y2) * (x1-x3 );} int land (int a, int B, int s, int e) {double c1, c2, c3; int mid = (s + e)/2, temp; // printf ("s: % d e: % d mid: % d \ n", s, e, mid); if (s = e) return mid; c1 = calc (box [mid]. x1, zsy, box [mid]. x2, yxy, a, B); c2 = cal C (box [mid-1]. x1, zsy, box [mid-1]. x2, yxy, a, B ); // these two sentences and the following judgment conditions are used to prevent mid from removing c3 = calc (box [mid + 1 ]. x1, zsy, box [mid + 1]. x2, yxy, a, B ); // For example, if only 4 and 5 are left, mid = won't be judged. // printf ("% lf \ n", c1, c2, c3 ); if (c1 <= 0 & c2> = 0) return mid; if (c1> 0 & c3 <= 0) return mid + 1; if (c1 <= 0) return land (a, B, s, mid); if (c1> 0) return land (a, B, mid, e );} int main () {int m, I, temp, a, B; while (scanf ("% d", & n), n) {memset (res, 0, sizeof (res ); Scanf ("% d", & m, & zsx, & zsy, & yxx, & yxy); box [n]. x1 = box [n]. x2 = yxx; for (I = 0; I <n; I ++) {scanf ("% d", & a, & B ); // Save the edge box [I] on the right side of the I lattice. x1 = a; box [I]. x2 = B ;}for (I = 0; I <m; I ++) {scanf ("% d", & a, & B ); // obtain the x value. Temp = land (a, B, 0, n); // printf ("temp: % d \ n ----- \ n", temp ); res [temp] ++;} for (I = 0; I <= n; I ++) printf ("% d: % d \ n", I, res [I]); printf ("\ n") ;}/ * 4 1 0 100 600 040 6045 6550 70100 8055 0 */