[BZOJ5317][凸包][闵科夫斯基和]JSOI2018:部落战争

BZOJ5317

闵科夫斯基和模板题吧,求出闵科夫斯基和然后判断每个询问在不在里面就好了
JSOI2018就比较好,有简单一点的题也有难题,比较均衡

Code:

#include<bits/stdc++.h>
#define db double
#define ll long long
#define eps 1e-11
using namespace std;
inline int read(){
	int res=0,f=1;char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-') f=-f;ch=getchar();}
	while(isdigit(ch)) {res=(res<<1)+(res<<3)+(ch^48);ch=getchar();}
	return res*f;
}
const int N=3e5+5;
struct point{
	int x,y;
	point(){}
	point(int _x,int _y):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 point operator + (const point &a,const point &b){return point(a.x+b.x,a.y+b.y);}
	friend inline ll operator *	(const point &a,const point &b) {return 1ll*a.x*b.y-1ll*a.y*b.x;}
	friend inline point operator * (const point &a,const int &b) {return point(a.x*b,a.y*b);}
	inline ll dis() {return 1ll*x*x+1ll*y*y;}
}a[N],b[N],c[N],tt;
int n,m,q;
inline bool operator < (const point &a,const point &b){if(a.y==b.y) return a.x<b.x;return a.y<b.y;}
inline bool cmp(point a,point b){
	ll t=(a-tt)*(b-tt);
	if(t==0) return (a-tt).dis()<(b-tt).dis();
	else return t>0;
}
int main(){
	n=read();m=read();q=read();
	for(int i=1;i<=n;i++) a[i].x=read(),a[i].y=read();
	for(int i=2;i<=n;i++) if(a[i]<a[1]) swap(a[i],a[1]);
	tt=a[1];
	sort(a+2,a+n+1,cmp);
	a[++n]=a[1];
	int top=1;
	for(int i=2;i<=n;i++){
		while(top>=2 && (a[top]-a[top-1])*(a[i]-a[top-1])<=0) --top;
		a[++top]=a[i];
	}
	n=top;
	for(int i=1;i<=m;i++) b[i].x=read(),b[i].y=read(),b[i]=b[i]*(-1);
	for(int i=2;i<=m;i++) if(b[i]<b[1]) swap(b[i],b[1]);
	tt=b[1];
	sort(b+2,b+m+1,cmp);
	b[++m]=b[1],top=1;
	for(int i=2;i<=m;i++){
		while(top>=2 && (b[top]-b[top-1])*(b[i]-b[top-1])<=0) --top;
		b[++top]=b[i];
	}
	m=top;
	int pn=1,pm=1;
	c[1]=a[1]+b[1];
	for(int i=2;i<=n+m-1;i++){
		if(pn==n){
			point tmp=b[pm+1]-b[pm];pm++;
			c[i]=c[i-1]+tmp;
		}
		else if(pm==m){
			point tmp=a[pn+1]-a[pn];pn++;
			c[i]=c[i-1]+tmp;
		}
		else{
			point tmp=a[pn+1]-a[pn],tnp=b[pm+1]-b[pm];
			if(tmp*tnp>=0) c[i]=c[i-1]+tmp,pn++;
			else c[i]=c[i-1]+tnp,pm++;
		}
	}
	n=n+m-1;
	top=1;
	for(int i=2;i<=n;i++){
		while(top>=2 && (c[top]-c[top-1])*(c[i]-c[top-1])<=0) --top;
		c[++top]=c[i];
	}
	n=top;
	for(int i=2;i<=n-1;i++)
		if(c[i].y>=c[i-1].y && c[i].y>=c[i+1].y){m=i;break;}
	for(int i=1;i<=q;i++){
		point p;p.x=read();p.y=read();
		if(p.y<c[1].y || p.y>c[m].y) {puts("0");continue;}
		if(p.y==c[1].y){
			int vl=c[1].x,vr=c[1].x;
			if(p.y==c[2].y) vr=c[2].x;
			printf("%d\n",p.x>=vl && p.x<=vr);
			continue;
		}
		if(p.y==c[m].y){
			int vl=c[m].x,vr=c[m].x;
			if (p.y==c[m+1].y) vl=c[m+1].x;
			printf("%d\n",p.x>=vl && p.x<=vr);
			continue;
		}
		db vl,vr;
		int l=1,r=m;
		while(l<r){
			int mid=(l+r+1)/2;
			if(c[mid].y<=p.y) l=mid;
			else r=mid-1;
		}
		vr=c[l].x+(db)(c[l+1].x-c[l].x)/(c[l+1].y-c[l].y)*(p.y-c[l].y);
		l=m,r=n;
		while(l<r){
			int mid=(l+r+1)/2;
			if(c[mid].y>=p.y) l=mid;
			else r=mid-1;
		}
		vl=c[l].x+(db)(c[l+1].x-c[l].x)/(c[l+1].y-c[l].y)*(p.y-c[l].y);
		printf("%d\n",p.x>=vl-eps && p.x<=vr+eps);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值