poj 2318 TOYS

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 diagram 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 0
3 1
4 3
6 8
10 10
15 30
1 5
2 1
2 8
5 5
40 10
7 9
4 10 0 10 100 0
20 20
40 40
60 60
80 80
 5 10
15 10
25 10
35 10
45 10
55 10
65 10
75 10
85 10
95 10
0

Sample Output

0: 2
1: 1
2: 1
3: 1
4: 0
5: 1

0: 2
1: 2
2: 2
3: 2
4: 2

一开始看到5000的数据,大胆的写O(n*m)的,可是tle了,这个题不就是考察怎样判断一个点是否在多边形里面吗?回头想一想,可以二分啊,这样二分下来172ms,这次真的见识到二分的威力了,其实计算机和的题目并没有那么可怕,只是我们把它想象的很可怕罢了,可能是我只做了少量计算机和的原因吧!代码大部分没有用,请看main部分
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define  inf 0x0f0f0f0f

using namespace std;

const double PI=acos(-1.0);
const double eps=1e-8;
typedef pair<int,int>pii;

struct Point
{
     double x,y;
     Point(double x=0,double y=0):x(x),y(y){}
};

typedef Point Vector;

Vector operator + (Vector A,Vector B)//向量加法
{
     return Vector(A.x+B.x,A.y+B.y);
}

Vector operator - (Vector A,Vector B)
{
     return Vector(A.x-B.x,A.y-B.y);
}

Vector operator * (Vector A,double p)
{
     return Vector(A.x*p,A.y*p);
}

Vector operator / (Vector A,double p)
{
     return Vector(A.x/p,A.y/p);
}

bool operator <(const Point &a,const Point &b)
{
     return (a.x<b.x || (a.x==b.x && a.y<b.y));
}

int dcmp(double x)
{
     if (fabs(x)<eps) return 0;
     else return x<0?-1:1;
}

bool operator == (const Point &a,const Point &b)
{
     return (dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0);
}

double Dot(Vector A,Vector B)//点积
{
     return A.x*B.x+A.y*B.y;
}

double Length(Vector A)//向量的模
{
     return sqrt(Dot(A,A));
}

double Angle(Vector A,Vector B)//向量夹角
{
     return acos(Dot(A,B)/Length(A)/Length(B));
}

double Cross(Vector A,Vector B)//向量叉积
{
     return A.x*B.y-A.y*B.x;
}

double Area2(Point A,Point B,Point C)//三角形面积2倍
{
     return Cross(B-A,C-A);
}

Vector Rotate(Vector A,double rad)//向量旋转
{
     return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}

Vector Normal(Vector A)//单位发向量
{
     double L=Length(A);
     return Vector(-A.y/L,A.x/L);
}

Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)//直线相交
{
     Vector u=P-Q;
     double t=Cross(w,u)/Cross(v,w);
     return P+v*t;
}

double DistanceToLine(Point P,Point A,Point B)//点到直线距离
{
     Vector v1=B-A, v2=P-A;
     return fabs(Cross(v1,v2))/Length(v1);
}

double DistanceToSegment(Point P,Point A,Point B)//点到线段的距离
{
     if (A==B) return Length(P-A);
     Vector v1=B-A, v2=P-A, v3=P-B;
     if (dcmp(Dot(v1,v2))<0) return Length(v2);
     else if (dcmp(Dot(v1,v3))>0) return Length(v3);
     else return fabs(Cross(v1,v2))/Length(v1);
}

Point GetLineProjection(Point P,Point A,Point B)//点在直线的投影
{
     Vector v=B-A;
     return A+v*(Dot(v,P-A)/Dot(v,v));
}

bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)//判断线段是否相交
{
     double c1=Cross(a2-a1,b1-a1);
     double c2=Cross(a2-a1,b2-b1);
     double c3=Cross(b2-b1,a1-b1);
     double c4=Cross(b2-b1,a2-b1);
     return (dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0);
}

double dis_point_point(Point A,Point B)//点到点的距离
{
     return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}

double angle(Vector v)//向量的角
{
     return atan2(v.y,v.x);
}

struct Circle
{
     Point c;
     double r;
     Circle (Point c,double r):c(c),r(r) {}
     Point point(double a)
     {
          return Point(c.x+cos(a)*r,c.y+sin(a)*r);
     }
};

int get_circle_circle_intersection(Circle C1,Circle C2,vector<Point>&sol)
{
     double d=Length(C1.c-C2.c);
     if (dcmp(d)==0)
     {
          if (dcmp(C1.r-C2.r)==0) return -1;
          return 0;
     }
     if (dcmp(C1.r+C2.r-d)<0) return 0;
     if (dcmp(fabs(C1.r-C2.r)-d)>0) return 0;
     double a=angle(C2.c-C1.c);
     double da=acos((C1.r*C1.r+d*d-C2.r*C2.r)/(2*C1.r*d));
     Point p1=C1.point(a-da),p2=C1.point(a+da);
     sol.push_back(p1);
     if (p1==p2) return 1;
     sol.push_back(p2);
     return 2;
}

int get_Tangents(Point p,Circle C,Vector *v)//点和圆的切线
{
     Vector u=C.c-p;
     double dist=Length(u);
     if (dist<C.r) return 0;
     else if (dcmp(dist-C.r)==0)
     {
          v[0]=Rotate(u,PI/2);
          return 1;
     }
     else
     {
          double ang=asin(C.r/dist);
          v[0]=Rotate(u,-ang);
          v[1]=Rotate(u,+ang);
          return 2;
     }
}

int ConvexHull(Point* p,int n,Point* ch)//求凸包
{
     sort(p,p+n);
     int m=0;
     for (int i=0;i<n;i++)
     {
          while(m>1 && Cross(ch[m-1]-ch[m-1],p[i]-ch[m-2])<=0)m--;
          ch[m++]=p[i];
     }
     int k=m;
     for (int i=n-2;i>=0;i--)
     {
          while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
          ch[m++]=p[i];
     }
     if(n>1) m--;
     return m;
}

double rectangular_area(Point A,Point B,Point C,Point D)
{
     double area=0;
     area+=fabs(Cross(B-A,C-A));
     area+=fabs(Cross(C-A,D-A));
     return area/2;
}

double Point_rectangular(Point P,Point A,Point B,Point C,Point D)
{
     double area=0;
     area+=fabs(Cross(A-P,B-P));
     area+=fabs(Cross(B-P,C-P));
     area+=fabs(Cross(C-P,D-P));
     area+=fabs(Cross(D-P,A-P));
     return area/2;
}
double area[5000+10];
int num[5000+10];
Point point[10000+10];
Point P;
int find(int x,int y)
{
     while(x<y)
     {
          int m=x+(y-x)/2;
          double area1=Point_rectangular(P,point[1],point[2],point[m*2+2],point[m*2+1]);
          if (dcmp(area1-area[m])==0) y=m;
          else x=m+1;
     }
     return x-1;
}

int main()
{
     //freopen("in.txt","r",stdin);
     double x1,x2,y1,y2,x,y;
     int n,m,N;
     while(scanf("%d",&n)!=EOF && n)
     {
          scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);
          memset(num,0,sizeof(num));
          N=2*n+4;
          point[1].x=x1; point[1].y=y1; point[2].x=x1; point[2].y=y2;
          point[N-1].x=x2; point[N-1].y=y1; point[N].x=x2; point[N].y=y2;
          for (int i=1;i<=n;i++)
          {
               scanf("%lf%lf",&x,&y);
               point[2*i+1].x=x; point[2*i+1].y=y1;
               point[2*i+2].x=y; point[2*i+2].y=y2;
               area[i]=rectangular_area(point[1],point[2],point[2*i+2],point[2*i+1]);
          }
          area[n+1]=rectangular_area(point[1],point[2],point[2*n+4],point[2*n+3]);

          for (int i=1;i<=m;i++)
          {
               scanf("%lf%lf",&P.x,&P.y);
               num[find(1,n+1)]++;
          }
          for (int i=0;i<=n;i++)
          printf("%d: %d\n",i,num[i]);
          printf("\n");
     }
     return 0;
}

作者 chensunrise



posted @ 2014-07-09 12:00  Hust_BaoJia  阅读(127)  评论(0编辑  收藏  举报
努力