POJ 2318 TOYS cross product application

Source: Internet
Author: User
Tags x2 y2

POJ 2318 TOYS cross product application
Click Open Link

TOYS
Time Limit:2000 MS Memory Limit:65536 K
Total Submissions:11078 Accepted:5312

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 the 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 of 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.

Source

Rocky Mountain 2003

N boards divide a box into n + 1 parts, give you some coordinates of the toys, and find the number of toys contained in each part of n + 1. The coordinates of the Boards are arranged in order. Therefore, you can use the cross product to determine the position of the toys in two hours. Double, long wa.
// 468 KB219 ms # include
 
  
# Include
  
   
# Include # define M 5007 # define ll long longusing namespace std; double U [M], L [M]; int sum [M], n; double y_1, y2; bool chaji (double x, double y, int mid) // determine whether the cross product is on the left of the mid {double x_1 = U [mid]-L [mid]; double x_2 = x-L [mid]; double y_2 = y-y2; return (x_1 * y_2-x_2 * y_1)> = 0;} double binary_search (double x, double y) // binary {int s = 1, e = n, mid; while (s <= e) {mid = (s + e)> 1; if (chaji (x, y, mid) e = mid-1; else s = mid + 1;} return s;} int main () {double x1, y1, x2; int m; int flag = 0; while (scanf ("% d", & n), n) {scanf ("% d % lf", & m, & x1, & y1, & x2, & y2); y_1 = y1-y2; memset (U, 0, sizeof (U); memset (L, 0, sizeof (L); memset (sum, 0, sizeof (sum); for (int I = 1; I <= n; I ++) scanf ("% lf", & U [I], & L [I]); U [0] = x1; L [0] = x1; double x, y; while (m --) {scanf ("% lf", & x, & y); int pos = binary_search (x, y ); sum [pos-1] ++;} if (flag) {printf ("\ n");} flag = 1; for (int I = 0; I <= n; I ++) printf ("% d: % d \ n", I, sum [I]);} return 0 ;}
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.