Title:
Given N points on the plane, for M lines, determine whether the N points are on the same side of each line in turn. Output GOOD, not BAD.
Explanation:
Convex hull + dichotomy.
Firstly, the convex hull is constructed. Then, for a straight line, two parallel lines are found. The two parallel lines are tangent on the convex hull. Then, it is necessary to judge whether the line segments formed by two tangent points intersect with the straight line.
#include<iostream>
#include<set>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const double eps=1e-8;
typedef pair<double,int> pii;
inline bool operator<(const pii &a,const pii &b){
return a.first-eps<b.first;
}
vector<pii>S;
const int Maxn=1e5+50;
int n,id,tot=1;
struct point{
double x,y;
point(double x=0,double y=0):x(x),y(y){}
friend inline point operator -(const point &a,const point &b){return point(a.x-b.x,a.y-b.y);}
friend inline double operator *(const point &a,const point &b){return a.x*b.y-a.y*b.x;}
inline double len()const{return sqrt(x*x+y*y);}
}p[Maxn],q1,q2;
inline bool cmp_ang(const point &a,const point &b){
double t=(a-p[1])*(b-p[1]);
if(t!=0)return t>0;
else return ((a-p[1]).len()<(b-p[1]).len());
}
inline bool inter(point a,point b,point c,point d){
return ((d-b)*(c-b))*((c-a)*(d-a))>-eps;
}
int main(){
scanf("%d",&n);
if(!n){
while(scanf("%lf%lf%lf%lf",&q1.x,&q1.y,&q2.x,&q2.y)!=EOF)printf("GOOD\n");
return 0;
}
for(int i=1;i<=n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
if(i==1||(p[i].x<p[id].x)||(p[i].x==p[id].x&&p[i].y<p[id].y))id=i;
}
if(id!=1)swap(p[1],p[id]);
sort(p+2,p+n+1,cmp_ang);
for(int i=2;i<=n;i++){
while(tot>=2&&(p[i]-p[tot-1])*(p[tot]-p[tot-1])>=0)tot--;
p[++tot]=p[i];
}
p[tot+1]=p[1];
for(int i=1;i<=tot;i++)S.push_back(make_pair(atan2(p[i+1].y-p[i].y,p[i+1].x-p[i].x),i));
if(S.size())sort(S.begin(),S.end());
S.push_back(make_pair((*S.begin()).first+2.0*acos(-1.0),(*S.begin()).second));
while(scanf("%lf",&q1.x)!=EOF){
scanf("%lf%lf%lf",&q1.y,&q2.x,&q2.y);
if(n==0){printf("GOOD");continue;}
pii t1=*(lower_bound(S.begin(),S.end(),make_pair(atan2(q2.y-q1.y,q2.x-q1.x),0)));
pii t2=*(lower_bound(S.begin(),S.end(),make_pair(atan2(q1.y-q2.y,q1.x-q2.x),0)));
if(!inter(p[t1.second],p[t2.second],q1,q2)){printf("GOOD\n");}
else {printf("BAD\n");}
}
}