poj 2318 点与线位置关系的判断 二分+叉积

时间:2021-08-18 10:41:51

题意:n个挡板,m个玩具,盒子的左上角坐标和右下角坐标,给挡板的x坐标(上下),给玩具的坐标,求每个格子里有多少个玩具。

解法:因为题中给的挡板具有顺序性,所以二分查找,叉积判断可行性,然后统计个数。

/*************************************************************************
> File Name: poj2318.cpp
> Author:
> Mail:
> Created Time: 2014/8/12 10:12:39
************************************************************************/
#include<iostream>
#include<cstring>
#include <algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#define maxn 50000+5
#define inf 0x3f3f3f3f
#define INF 0x3FFFFFFFFFFFFFFFLL
#define rep(i,n) for(i=0;i<n;i++)
#define reP(i,n) for(i=1;i<=n;i++)
#define ull unsigned long long
#define ll long long
#define cle(a) memset(a,0,sizeof(a))
using namespace std;
int n;
int m;
struct point
{
double x,y;
};
int sum[maxn];
bool getbool(point a,point b,point c)
{
return ((b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x))<0;
}
point up[maxn];
point down[maxn];
point val[maxn];
void doit(point x)
{
int i,j,k;
int l=0,r=n+1;
int ans;
while(l<=r){
int mid=(l+r)>>1;
if(getbool(x,up[mid],down[mid])){
ans=mid;
r=mid-1;
}
else l=mid+1;
}
sum[ans-1]++;
}
int main()
{
int T=0;
freopen("in.txt","r",stdin);
int i,j,k;
double x1,y1;
double x2,y2;
while(cin>>n){
cle(sum);
if(n==0)break;
//cin>>m;
scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);
reP(i,n){
scanf("%lf%lf",&up[i].x,&down[i].x);
up[i].y=y1,down[i].y=y2;
}
up[0].x=x1,up[i].y=y1;
down[0].x=x1,down[i].y=y2;
up[n+1].x=x2,up[n+1].y=y1;
down[n+1].x=x2,down[n+1].y=y2;
rep(i,m){
scanf("%lf%lf",&val[i].x,&val[i].y);
}
rep(i,m)doit(val[i]);
if(T)cout<<endl;
rep(i,n+1)printf("%d: %d\n",i,sum[i]);
T++;

}
return 0;
}