POJ 2318 TOYS(计算几何)

居然脑残认为一定从(0,0)点作为左下角的点,然后怒WA,然后请爽爷出手查,果断一针见血啊!Orz

嗯,第一题计算几何

//POJ2318 TOYS
//叉积+二分

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;

#define N 50005
#define ll long long

struct Point{
       ll x,y;
};

struct line{
    Point front,to;
}l[N];

//叉积
ll cross(Point &p1,Point &p2){
    return p1.x*p2.y-p1.y*p2.x;
}

ll lr(line &li,Point &p){
    Point p1,p2;
    p1.x=p.x-li.front.x;
    p1.y=p.y-li.front.y;
    p2.x=li.to.x-li.front.x;
    p2.y=li.to.y-li.front.y;
    return cross(p1,p2);    //若p在ll右边则为1,左边为0
}

ll binarysearch(ll left,ll right,Point &p){
    if(left+1==right) return left;
    ll t=(left+right)/2;
    if(lr(l[t],p)>0){
        return binarysearch(t,right,p);
    }
    else return binarysearch(left,t,p);
}

ll cnt[N];

int main(){
    ll i,j,n,m,x1,x2,y1,y2;
    while(cin>>n&&n){
        cin>>m>>x1>>y1>>x2>>y2;

        l[0].front.x=x1;
        l[0].front.y=y2;
        l[0].to.x=x1;
        l[0].to.y=y1;
        l[n+1].front.x=x2;
        l[n+1].front.y=y2;
        l[n+1].to.x=x2;
        l[n+1].to.y=y1;


        for(i=1;i<=n;i++){
            cin>>x1>>x2;
            l[i].front.x=x2;
            l[i].front.y=y2;
            l[i].to.x=x1;
            l[i].to.y=y1;
        }
        memset(cnt,0,sizeof(cnt));
        for(i=0;i<m;i++){
            cin>>x1>>y1;
            Point p;
            p.x=x1;
            p.y=y1;
            ll t=binarysearch(0,n+1,p);
            cnt[t]++;
        }
        for(i=0;i<=n;i++){
            cout<<i<<": "<<cnt[i]<<endl;
        }
        cout<<endl;

    }
    return 0;
}


 

你可能感兴趣的:(计算几何)