计算几何

闵可夫斯基和

两个点集 A , B A,B A,B 的闵可夫斯基和是 C = { y + z ∣ y ∈ A , z ∈ B } C=\{y+z|y\in A,z\in B\} C={y+zyA,zB}
两个凸包的闵可夫斯基和也是凸包,两个指针 for 一遍搞起来即可。

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fir first
#define sec second
#define ld long double
using namespace std;
const int N=100010;
typedef pair <int,int> P;
struct node {
	int x,y;
	node(int _x=0,int _y=0) {x=_x,y=_y;}
}a[N],b[N],c[N],st[N],O;
ll operator ^ (node a,node b) {return 1ll*a.x*b.y-1ll*b.x*a.y;}
node operator - (node a,node b) {return node(a.x-b.x,a.y-b.y);}
node operator + (node a,node b) {return node(a.x+b.x,a.y+b.y);}
int ss;
int read()
{
	int x=0;char c=getchar(),flag='+';
	while(!isdigit(c)) flag=c,c=getchar();
	while(isdigit(c)) x=x*10+c-'0',c=getchar();
	return flag=='-'?-x:x;
}
bool cmp(node x,node y)
{
	return ((x-O)^(y-O))>=0;
}
void build(node *a,int &n)
{
	for(int i=2;i<=n;i++) if((a[i].x<a[1].x)||(a[i].x==a[1].x&&a[i].y<a[1].y)) swap(a[i],a[1]);
	O=a[1],sort(a+2,a+n+1,cmp);
	int top=0;
	for(int i=1;i<=n;i++)
	{
		while(top>1&&((st[top]-st[top-1])^(a[i]-st[top-1]))<=0) top--;
		st[++top]=a[i];
	}
	for(int i=1;i<=top;i++) a[i]=st[i];
	n=top;
}
int solve(node x)
{
	if(((x-c[1])^(c[ss]-c[1]))<0||((x-c[1])^(c[2]-c[1]))>0) return 0;
	int l=1,r=ss,ans=ss;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(((c[mid]-c[1])^(x-c[1]))>=0) l=mid+1,ans=mid;
		else r=mid-1;
	}
	return ((c[ans]-x)^(c[ans+1]-x))>=0;
}
int main()
	int n=read(),m=read(),q=read();
	for(int i=1;i<=n;i++) a[i].x=read(),a[i].y=read();
	for(int i=1;i<=m;i++) b[i].x=-read(),b[i].y=-read();
	build(a,n),build(b,m);
	
	//想象笔尖戳着一个凸包,沿着另一个凸包边界走一圈,划过的部分内就是闵可夫斯基和 
	a[n+1]=a[1],b[m+1]=b[1];
	int i=1,j=1;
	c[++ss]=a[1]+b[1];
	while(i<=n||j<=m)
	{
		node x=a[i]+b[j+1],y=a[i+1]+b[j];
		if(((x-c[ss])^(y-c[ss]))>0) c[++ss]=x,j++;
		else c[++ss]=y,i++;
	}
	ss--;
	
	while(q--)
	{
		node u;
		u.x=read(),u.y=read();
		cout<<solve(u)<<'\n';
	}
	return 0;
}
判断一个点是否在凸包内 O ( log ⁡ n ) O(\log n) O(logn)

把凸包上的某个点作为原点,把凸包三角剖分,二分到询问点所在的区域,叉积判一下。

极角排序

atan2(y,x) 函数返回辅角主值。
在这里插入图片描述

半平面交

射箭
a x i 2 + b x i ≥ l i ⟹ b ≥ l i x i − a x i ax_i^2+bx_i\geq l_i\Longrightarrow b\geq \frac{l_i}{x_i}-ax_i axi2+bxilibxiliaxi
我们把(a,b)看做点的话,合法的点就是一个半平面。二分答案,求半平面交即可。

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fir first
#define sec second
#define ld long double
#define I inline
using namespace std;
const ld eps=1e-18,inf=1e10;
const int N=200010;
typedef pair <int,int> P;
struct Vec 
{
    ld x,y;
    I Vec (ld _x=0,ld _y=0) {x=_x,y=_y;}
    I Vec operator + (Vec b) {return Vec(x+b.x,y+b.y);}
    I Vec operator - (Vec b) {return Vec(x-b.x,y-b.y);}
    I Vec operator * (ld b) {return Vec(x*b,y*b);}
    I ld operator * (Vec b) {return x*b.x+y*b.y;}
    I ld operator ^ (Vec b) {return x*b.y-y*b.x;}
    I ld ang() {return atan2(y,x);}
};
struct Line 
{
    Vec a,b;int ind;
    I Line () {}
    I Line (Vec _a,Vec _b,int _ind) {a=_a,b=_b,ind=_ind;}
    I ld ang() {return (b-a).ang();}
    
}a[N],b[N],q[N];

Vec cross(Line a,Line b) 
{
    ld ss=(a.a-b.a)^(b.b-b.a),tt=(b.b-b.a)^(a.b-b.a);
    return a.a+(a.b-a.a)*(ss/(ss+tt));
}
int cnt;
bool cmp(Line a,Line b) {return a.ang()<b.ang();}
int read()
{
    int x=0;char c=getchar(),flag='+';
    while(!isdigit(c)) flag=c,c=getchar();
    while(isdigit(c)) x=x*10+c-'0',c=getchar();
    return flag=='-'?-x:x;
}
bool judge(Line a,Line b,Line c)
{
    Vec x=cross(a,b);
    return ((x-c.a)^(c.b-c.a))>-eps;	//我觉得应该是大于-eps但是这样过不了qwq,大于0才能过
}
bool check(int mid)
{
    int ss=0,h=1,t=0;
    for(int i=1;i<=cnt;i++) if(a[i].ind<=mid) b[++ss]=a[i];
    q[++t]=b[1],q[++t]=b[2];
    for(int i=3;i<=ss;i++)
    {
        while(h<t&&judge(q[t-1],q[t],b[i])) t--;
        while(h<t&&judge(q[h],q[h+1],b[i])) h++;
        q[++t]=b[i];
    }
    while(h<t&&judge(q[t-1],q[t],q[h])) t--;
    while(h<t&&judge(q[h],q[h+1],q[t])) h++;
    return t-h>=2;
}
int main()
{
    int n=read();
    for(int i=1;i<=n;i++)
    {
        ld x=read(),l=read(),r=read();
        a[++cnt]=Line(Vec(0,l/x),Vec(1,l/x-x),i);
        a[++cnt]=Line(Vec(1,r/x-x),Vec(0,r/x),i);
    }
    a[++cnt]=Line(Vec(-eps,0),Vec(-eps,1),0);
    a[++cnt]=Line(Vec(0,inf),Vec(-1,inf),0);
    a[++cnt]=Line(Vec(-inf,1),Vec(-inf,0),0);
    a[++cnt]=Line(Vec(0,eps),Vec(1,eps),0);
    sort(a+1,a+cnt+1,cmp);
    int l=1,r=n,ans=0;
    while(l<=r)
    {
        int mid=l+r>>1;
        if(check(mid)) ans=mid,l=mid+1;
        else r=mid-1;
    }
    cout<<ans;
    return 0;
}
/*by DT_Kang*/

GUGUGU

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值