【洛谷P4557】【JSOI2018】—战争(Minkowski Sum)
补模板,无题解
#include<bits/stdc++.h>
using namespace std;
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
const int N=100005;
struct pt{
double x,y;
pt(double _x=0,double _y=0):x(_x),y(_y){}
friend inline pt operator +(const pt &a,const pt &b){
return pt(a.x+b.x,a.y+b.y);
}
friend inline pt operator -(const pt &a,const pt &b){
return pt(a.x-b.x,a.y-b.y);
}
friend inline double operator *(const pt &a,const pt &b){
return a.x*b.y-a.y*b.x;
}
inline double calc(){
return x*x+y*y;
}
}p[N],p1[N],p2[N],t1[N],t2[N],q[N];
inline bool comp(const pt &a,const pt &b){
double res=(a-q[1])*(b-q[1]);
return (res==0)?((a-q[1]).calc()<(b-q[1]).calc()):res<0;
}
inline int graham(pt *p,int n){
int top=0;
for(int i=1;i<=n;i++)q[i]=p[i];
for(int i=2;i<=n;i++)if(q[i].x<q[1].x||(q[i].x==q[1].x&&q[i].y<q[1].y))swap(q[i],q[1]);
sort(q+2,q+n+1,comp),top=1;
for(int i=2;i<=n;i++){
while(top>=2&&(q[i]-q[top-1])*(q[top]-q[top-1])<=0)top--;
q[++top]=q[i];
}
for(int i=1;i<=top;i++)p[i]=q[i];p[top+1]=q[1];
return top;
}
int n,m,Q,tot;
inline int query(int x,int y){
pt now=pt(x,y);
if((now-p[1])*(p[2]-p[1])<0||(now-p[1])*(p[tot]-p[1])>0)return 0;
int l=2,r=tot,res=2;
while(l<=r){
int mid=((l+r)>>1);
if((p[mid]-p[1])*(now-p[1])<=0)l=mid+1,res=mid;
else r=mid-1;
}
return (p[res+1]-p[res])*(now-p[res])<=0;
}
int main(){
#ifdef Stargazer
freopen("lx.cpp","r",stdin);
#endif
n=read(),m=read(),Q=read();
for(int i=1;i<=n;i++)p1[i].x=read(),p1[i].y=read();
for(int i=1;i<=m;i++)p2[i].x=-read(),p2[i].y=-read();
n=graham(p1,n),m=graham(p2,m);
for(int i=1;i<=n;i++)t1[i]=p1[i+1]-p1[i];
for(int i=1;i<=m;i++)t2[i]=p2[i+1]-p2[i];
int f1=1,f2=1;p[tot=1]=p1[1]+p2[1];
while(f1<=n&&f2<=m)p[tot+1]=p[tot]+((t1[f1]*t2[f2])<=0?t1[f1++]:t2[f2++]),tot++;
while(f1<=n)p[tot+1]=p[tot]+t1[f1++],tot++;
while(f2<=m)p[tot+1]=p[tot]+t2[f2++],tot++;
tot=graham(p,tot);
for(int i=1;i<=Q;i++){
int x=read(),y=read();
cout<<query(x,y)<<'\n';
}
}