题意:
二维平面给出n条线段,每次提问一个点,输出到这个点距离最短的线段的编号。
题解:
数据随机,就暴力+RP了。。。
坐标范围是2^16,将坐标每2^8一个分块,总共有2^16个块。
读入一条线段时,将这条线段经过的所有块都加上当前线段的编号。
读入一个询问,找到他属于第几块,然后枚举这个块中包含的编号,只在这些线段中求点到线段的最短距离。
但是,可能距离最短的线段不在这个块里,我们就将这个块周围的8个块都检查一下好了。
运气好,1次过样例,1次就AC。
代码:
#include<bits/stdc++.h>
#define N 257
#define INF 0x3f3f3f3f
#define eps 1e-10
#define pi 3.141592653589793
#define mod 998244353
#define LL long long
#define pb push_back
#define cl clear
#define si size
#define lb lower_bound
#define mem(x) memset(x,0,sizeof x)
#define sc(x) scanf("%d",&x)
#define scc(x,y) scanf("%d%d",&x,&y)
#define sccc(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;
int dcmp(double x){if (fabs(x)<eps)return 0;else return x<0?-1:1;}
struct Point
{
double x,y;
bool operator < (const Point &a)const
{return dcmp(x-a.x)<0 || (dcmp(x-a.x)==0 && dcmp(y-a.y)<0);}
Point(double x=0,double y=0):x(x),y(y){ }
void read(){scanf("%lf%lf",&x,&y);}
};
typedef Point Vector;
Vector operator + (Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator - (Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator * (Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator / (Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator == (Vector a,Vector b){return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;}
double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;} //点积
double Length(Vector a){return sqrt(Dot(a,a));}
double Angle(Vector a,Vector b){return acos(min(1.0,max(-1.0,Dot(a,b)/Length(a)/Length(b))));}// 范围[0,180]
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;} //叉积
Point Normal(Point a){ return a/Length(a); }
Vector Rotate(Vector a,double rad)// 向量 a 逆时针旋转 rad
{return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));}
struct Seg
{
Point a,b;
}c[10010];
//点到线段的距离,p到线段ab的距离,若距离不存在,返回pa,pb中较短的线段的距离
double DistanceToSegment(Point p,Point a,Point b)
{
if (a==b)return Length(p-a);
Vector v1=b-a,v2=p-a,v3=p-b;
if (dcmp(Dot(v1,v2))<0)return Length(v2);
else if (dcmp(Dot(v1,v3))>0)return Length(v3);
else return fabs(Cross(v1,v2))/Length(v1);
}
vector<int> a[N*N];
int f(double x,double y)
{
int xx=round(x);int yy=round(y);
return (yy>>8)*(1<<8)+(xx>>8);
}
double ans=1e10;int s;
int t;Point e;
void sdfsd()
{
for (auto i:a[t])
{
double dis=DistanceToSegment(e,c[i].a,c[i].b);
if (dcmp(dis-ans)<0)
{
ans=dis;
s=i;
}else
if (dcmp(dis-ans)==0 && i<s) s=i;
}
}
int main()
{
int n,m;
while(~scc(n,m))
{
mem(a);
for (int i=1;i<=n;i++)
{
c[i].a.read();c[i].b.read();
Vector v=c[i].b-c[i].a;
v=v/256; int t=-1;
for (int j=0;j<257;j++)
{
Point h=c[i].a+v*j;
int tt=f(h.x,h.y);
if (t!=tt) a[tt].pb(i);
t=tt;
}
}
while(m--)
{
ans=1e9;
e.read();
int tt=f(e.x,e.y);
t=tt; sdfsd();
t=tt+(1<<8);
if (t>=0 && t<N*N )sdfsd();
t++;
if (t>=0 && t<N*N )sdfsd();
t-=2;
if (t>=0 && t<N*N )sdfsd();
t=tt-(1<<8);
if (t>=0 && t<N*N )sdfsd();
t++;
if (t>=0 && t<N*N )sdfsd();
t-=2;
if (t>=0 && t<N*N )sdfsd();
t=tt+1;
if (t>=0 && t<N*N )sdfsd();
t=tt-1;
if (t>=0 && t<N*N )sdfsd();
printf("%d\n",s);
}
}
}