POJ 2318 TOYS (computational geometry)

Source: Internet
Author: User

B-toysTime limit:2000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64 U SubmitStatus

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 mX1Y1X2Y2. 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 are (X1,y1) and (X2,y2), respectively. The following n lines contain and the integers per line,Ui Li, indicating that the ends of theI-thcardboard partition is 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

Hint

As the example illustrates, toys that fall on the boundary of the box is "in" the box.

Test instructions: There is a large box, divided into n+1 block of n plates, labeled 0~n the upper left and bottom right corner of the box and the coordinates of each board up and down

The horizontal axis of the two ends (the plates are not staggered and given in order) and the coordinates of the toys are given, counting the number of toys in each space.

(Make sure the toy falls in space).

The solving method: using dichotomy and cross product to judge the position of the point. Cross product: If X1y2-x2y1 equals 0 segment 12 collinear, if the difference is greater than 0, then the Segment 1 on the right side of 2,

Otherwise on the left. Save the output with an array.

#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;structpoint{intx, y;};structline{Point A, B;} line[5005];intcnt[5005];intMulti (Point p1,point p2,point p0) {return(p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (p1.y-p0.y);}voidS (Point A,intN) {    intL=0, r=n-1, Mid;  while(l<r) {Mid= (l+r) >>1; if(Multi (A,LINE[MID].A,LINE[MID].B) >0) l=mid+1; ElseR=mid; }    if(Multi (A,LINE[L].A,LINE[L].B) <0) Cnt[l]++; Elsecnt[l+1]++;}intMain () {intN,m,x1,y1,x2,y2; inti,t1,t2;    Point A;  while(cin>>n&&N) {cin>>m>>x1>>y1>>x2>>Y2;  for(intI=0; i<n;i++) {cin>>t1>>T2; Line[i].a.x=T1; LINE[I].A.Y=Y1; line[i].b.x=T2; Line[i].b.y=Y2; } memset (CNT,0,sizeof(CNT));  for(intI=0; i<m;i++) {cin>>a.x>>a.y;        S (a,n); }         for(intI=0; i<=n;i++) cout<<i<<": "<<cnt[i]<<Endl; cout<<Endl; }    return 0;}

POJ 2318 TOYS (computational geometry)

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.