正文
UVALive7461 - Separating Pebbles 判断两个凸包相交
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
//UVALive7461 - Separating Pebbles 判断两个凸包相交 #include <bits/stdc++.h>
using namespace std;
#define LL long long
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
const int N =1e5+;
#define clc(a,b) memset(a,b,sizeof(a))
const double eps = 1e-;
const int MOD = 1e9+;
void fre() {freopen("in.txt","r",stdin);}
void freout() {freopen("out.txt","w",stdout);}
inline int read() {int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-;ch=getchar();}while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}return x*f;} int sgn(double x) {
if(fabs(x) < eps)return ;
if(x < )return -;
else return ;
} struct Point {
int x,y;
Point() {}
Point(int _x,int _y) {
x = _x;
y = _y;
}
Point operator -(const Point &b)const {
return Point(x - b.x,y - b.y);
}
int operator ^(const Point &b)const {
return x*b.y - y*b.x;
}
int operator *(const Point &b)const {
return x*b.x + y*b.y;
}
friend int dis2(Point a) {
return a.x*a.x+a.y*a.y;
}
friend bool operator<(const Point &a,const Point &b){
if(fabs(a.y-b.y)<eps) return a.x<b.x;
return a.y<b.y;
}
};
typedef Point Vector;
double Dot(Point A, Point B){return A.x*B.x+A.y*B.y;}//点积
double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}//叉积
double Length(Vector A){return sqrt(Dot(A,A));}//OA长
double Angle(Point A,Point B){return acos(Dot(A,B)/Length(A)/Length(B));}//OA和OB的夹角
//判断线段相交,不在端点相交
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return sgn(c1)*sgn(c2)<&&sgn(c3)*sgn(c4)<;
} int graham(Point p[],int n,Point q[]){
int top=;
sort(p,p+n);
if(n==) return ;
q[]=p[];
if(n==) return ;
q[]=p[];
if(n==) return ;
q[]=p[];
for(int i=;i<n;i++){
while(top&&(Cross(q[top]-q[top-],p[i]-q[top-])<=)) top--;
q[++top]=p[i];
}
int len=top;
q[++top]=p[n-];
for(int i=n-;i>=;i--){
while(top!=len&&(Cross(q[top]-q[top-],p[i]-q[top-])<=)) top--;
q[++top]=p[i];
}
return top;
} bool C_S(Point *ch1,int t1,Point *ch2,int t2)//判断凸包是否相交
{
double angle[],x;
int i,j,k,m;
if(t1==)return true;
if(t1==)
{
for(i=;i<t2;i++)
{
k=sgn(Cross(ch1[]-ch1[],ch2[i]-ch1[]));
if(k==&&Dot(ch1[]-ch1[],ch2[i]-ch1[])>)
{
if(Length(ch2[i]-ch1[])<Length(ch1[]-ch1[]))break;
}
}
if(i<t2)return false;
if(t2==&&SegmentProperIntersection(ch1[],ch1[],ch2[],ch2[]))return false;
return true;
}
angle[]=;
for(i=;i<t1;i++)
angle[i-]=Angle(ch1[]-ch1[],ch1[i]-ch1[]);
for(i=;i<t2;i++)
{
j=sgn(Cross(ch1[]-ch1[],ch2[i]-ch1[]));
if(j<||(j==&&Dot(ch1[]-ch1[],ch2[i]-ch1[])<))continue;
j=sgn(Cross(ch1[t1-]-ch1[],ch2[i]-ch1[]));
if(j>||(j==&&Dot(ch1[t1-]-ch1[],ch2[i]-ch1[])<))continue;
x=Angle(ch1[]-ch1[],ch2[i]-ch1[]);
m=lower_bound(angle,angle+t1-,x)-angle;
if(m==)j=;
else j=m-;
k=sgn(Cross(ch1[j+]-ch2[i],ch1[j+]-ch2[i]));
if(k>=)break;
}
if(i<t2)return false;
return true;
} Point p1[],p2[],ch1[],ch2[];
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
int cnt1=,cnt2=;
for(int i=;i<n;i++){
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
if(c==){
p1[cnt1++]=Point(x,y);
}
else p2[cnt2++]=Point(x,y);
}
int t1=graham(p1,cnt1,ch1);
int t2=graham(p2,cnt2,ch2);
if(C_S(ch1,t1,ch2,t2)&&C_S(ch2,t2,ch1,t1)) printf("1\n");
else printf("0\n");
}
}