QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#578130 | #8082. Minimum Euclidean Distance | Auroraa | AC ✓ | 10ms | 4116kb | C++20 | 7.7kb | 2024-09-20 16:50:00 | 2024-09-20 16:50:01 |
Judging History
answer
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
#define int long long
#define double long double
const double eps=1e-8;
const double pi=acos(-1);
int sgn(double x){ //符号
if(abs(x)<=eps)return 0;
return x<0?-1:1;
}
struct Point{ //点与向量
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
bool operator==(const Point&a)const{
return (sgn(x-a.x)==0&&sgn(y-a.y)==0);
}
bool operator<(const Point&a)const{
if(sgn(x-a.x)==0)return sgn(y-a.y)==-1;
return sgn(x-a.x)==-1;
}
bool operator>(const Point&a)const{return !((*this<a)||(*this==a));}
Point operator+(const Point&a)const{return {x+a.x,y+a.y};}
Point operator-(const Point&a)const{return {x-a.x,y-a.y};}
Point operator-()const{return {-x,-y};}
Point operator*(double t)const{return {x*t,y*t};}
Point operator/(double a)const{return {x/a,y/a};}
void input(){cin>>x>>y;}
double dot(const Point&a)const{return x*a.x+y*a.y;} //点积
double cross(const Point&a)const{return x*a.y-y*a.x;} //叉积
double len2()const{return dot(*this);} //长度平方
double len()const{return sqrt(len2());} //长度
int toleft(const Point&a)const{return sgn(cross(a));} //当前向量到 a 是逆时针旋转
double dis2(const Point&a)const{return ((*this)-a).len2();} //两点距离平方
double dis(const Point&a)const{return sqrt(dis2(a));} //两点距离
};
typedef Point Vector;
struct Line{ //直线
Point p;
Vector v;
Line(){}
Line(Point p,Vector v):p(p),v(v){}
double dis(Point a)const{return abs(v.cross(a-p)/v.len());} //点到直线距离
Point inter(const Line&a)const{ //两直线交点
return p+v*((a.v.cross(p-a.p))/(v.cross(a.v)));
}
bool operator<(const Line&a)const{ // 直线极角排序,极角相同时靠左的在前
if(sgn(atan2(v.y,v.x)-atan2(a.v.y,a.v.x))==0)return (a.p-p).cross(a.p+a.v-p)>0;
else return atan2(v.y,v.x)<atan2(a.v.y,a.v.x);
}
};
struct Segment{ //线段
Point a,b;
Segment(){}
Segment(Point a,Point b):a(a),b(b){}
bool operator<(const Segment &s)const{ //按左端点横坐标排序
return make_pair(a,b)<make_pair(s.a,s.b);
}
double dis(const Point&p)const{ //点到线段距离
if(sgn((p-a).dot(b-a))==-1||sgn((p-b).dot(a-b))==-1)return min(p.dis(a),p.dis(b));
Point p0=a,v=b-a;
return abs(v.cross(p-p0)/v.len());
}
// 判断点是否在线段上
// -1 点在线段端点 | 0 点不在线段上 | 1 点严格在线段上
int is_on(const Point&p)const{
if(p==a || p==b)return -1;
return (p-a).toleft(p-b)==0 && (p-a).dot(p-b)<-eps;
}
};
struct Polygon{ //多边形
vector<Point> p; //逆时针顺序存储
int nxt(const int x)const{return x==(p.size()-1)?0:(x+1);} //下个点
int pre(const int x)const{return x==0?(p.size()-1):(x-1);} //上个点
double area()const{ //多边形面积,顺时针存点为负,逆时针存点为正
double sum=0;
for(int i=0;i<p.size();i++)
sum+=p[i].cross(p[nxt(i)]);
return sum/2;
}
double circ()const{ //多边形周长
double sum=0;
for(int i=0;i<p.size();i++)
sum+=p[i].dis(p[nxt(i)]);
return sum;
}
};
struct Convex:Polygon{ //凸多边形
double Calipers()const{ //凸包直径 (旋转卡壳)
double ans=0;
if(p.size()==2)return p[0].dis(p[1]);
int now=0;
for(int i=0;i<p.size();i++){
Line l(p[i],p[i]-p[nxt(i)]);
while(l.dis(p[now])<=l.dis(p[nxt(now)]))now=nxt(now);
ans=max(ans,max(p[i].dis(p[now]),p[nxt(i)].dis(p[now])));
}
return ans;
}
// O(logn) 判断点是否在凸包内
// -1 点在凸包边上 | 0 点在凸包外 | 1 点在凸包内
int is_in(const Point a)const{
if(p.size()==1)return a==p[0]?-1:0;
if(p.size()==2)return Segment(p[0],p[1]).is_on(a)?-1:0;
if(a==p[0])return -1;
if((p[1]-p[0]).toleft(a-p[0])==-1 || (p.back()-p[0]).toleft(a-p[0])==1)return 0;
auto cmp=[&](const Point&u,const Point&v){
return (u-p[0]).toleft(v-p[0])==1;
};
int pos=lower_bound(p.begin()+1,p.end(),a,cmp)-p.begin();
if(pos==1)return Segment(p[0],p[1]).is_on(a)?-1:0;
if(pos==p.size()-1 && Segment(p[0],p[pos]).is_on(a))return -1;
if(Segment(p[pos-1],p[pos]).is_on(a))return -1;
return (p[pos]-p[pos-1]).toleft(a-p[pos-1])>0;
}
int get(const Point a,int op)const{ // op=1 极角序最大的切点 | op=-1 极角序最小的切点
int l=0,r=p.size()-1;
auto cmp=[&](const Point&x,const Point&y){
return (x-a).toleft(y-a)==op;
};
if(cmp(p[0],p.back())){ // 极值在 p[back]
while(l<r){
int mid=(l+r)>>1;
if(cmp(p[r],p[mid]) && cmp(p[mid+1],p[mid]))r=mid;
else l=mid+1;
}
}
else{ // 极值在 p[0]
while(l<r){
int mid=(l+r+1)>>1;
if(cmp(p[l],p[mid]) && cmp(p[mid-1],p[mid]))l=mid;
else r=mid-1;
}
}
return l;
}
// O(logn) 点到凸包切线,返回两个切点在凸包中的下标
pair<int,int>get_tan(const Point&a)const{
return {get(a,1),get(a,-1)};
}
// O(logn) 点到凸包距离
double dis(const Point&a)const{
if(is_in(a)==-1 || is_in(a)==1)return 0;
if(p.size()==1)return p[0].dis(a);
if(p.size()==2)return Segment(p[0],p[1]).dis(a);
auto[mx,mn]=get_tan(a);
double dis=min(Segment(p[mx],p[nxt(mx)]).dis(a),Segment(p[pre(mn)],p[mn]).dis(a));
int op1=sgn((p[nxt(mx)]-p[mx]).dot(a-p[mx]));
int op2=sgn((p[mn]-p[pre(mn)]).dot(a-p[pre(mn)]));
if(mx!=mn && op1!=op2){
int l=mx,r=mn-1;
if(r<l)r+=p.size();
while(l<r){
int mid=(l+r+1)>>1,t=mid%p.size();
int p1=sgn((p[nxt(t)]-p[t]).dot(a-p[t]));
if(p1==op1)l=mid;
else r=mid-1;
}
l%=p.size();
dis=min(dis,Segment(p[l],p[nxt(l)]).dis(a));
}
return dis;
}
};
Convex Andrew(vector<Point>p){
vector<Point>st;
Convex ans;
sort(p.begin(), p.end());
auto check=[&](const vector<Point>&st,Point u){
Point tmp1=st.back(),tmp2=*prev(st.end(),2);
return (tmp1-tmp2).toleft(u-tmp1)<=0;
};
for(auto u:p){
while(st.size()>1 && check(st,u))st.pop_back();
st.push_back(u);
}
int k=st.size();
p.pop_back();
reverse(p.begin(), p.end());
for(auto u:p){
while(st.size()>k && check(st,u))st.pop_back();
st.push_back(u);
}
st.pop_back();
ans.p=st;
return ans;
}
void solve(){
int n,m;
cin>>n>>m;
vector<Point>vec;
for(int i=1;i<=n;i++){
int x,y;
cin>>x>>y;
vec.emplace_back(x,y);
}
Convex con= Andrew(vec);
while(m--){
int x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
Point p1(x1,y1),p2(x2,y2);
Point p=(p1+p2)/2;
cout<<fixed<<setprecision(10)<<p.dis2(p1)/2.0l+con.dis(p)*con.dis(p)<<endl;
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int Te=1;
// cin>>Te;
while(Te--){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3820kb
input:
4 3 0 0 1 0 1 1 0 1 0 0 1 1 1 1 2 2 1 1 2 3
output:
0.2500000000 0.7500000000 1.8750000000
result:
ok Your answer is acceptable!^ ^
Test #2:
score: 0
Accepted
time: 0ms
memory: 3836kb
input:
48 10 -30 0 -29 -4 -28 -7 -27 -9 -25 -12 -22 -16 -21 -17 -17 -20 -14 -22 -12 -23 -9 -24 -5 -25 -4 -25 0 -24 3 -23 5 -22 8 -20 12 -17 13 -16 16 -12 18 -9 19 -7 20 -4 21 0 21 1 20 5 19 8 18 10 16 13 13 17 12 18 8 21 5 23 3 24 0 25 -4 26 -5 26 -9 25 -12 24 -14 23 -17 21 -21 18 -22 17 -25 13 -27 10 -28 ...
output:
589.5000000000 51.4705882353 1051.2500000000 66.6250000000 174.1250000000 562.6750000000 272.3942307692 287.3850000000 689.6250000000 436.2500000000
result:
ok Your answer is acceptable!^ ^
Test #3:
score: 0
Accepted
time: 10ms
memory: 3956kb
input:
5000 5000 -50000000 0 -49999885 -49450 -49999770 -85675 -49999604 -122394 -49999391 -157604 -49999130 -192731 -49998803 -229143 -49998399 -267196 -49997956 -303872 -49997469 -339362 -49996891 -377221 -49996257 -414903 -49995577 -451819 -49994843 -488600 -49994059 -524941 -49993173 -563137 -49992252 ...
output:
2214785369560632.8750000000 1632645104370924.3748779297 3954739966640761.3750000000 5405105667896787.7500000000 817274719687553.0313110352 902260846427661.0824584961 3194363161448624.1250000000 1619744446324385.1250000000 363457485421825.2500000000 4776425533214308.6240234375 8267595460255072.723144...
result:
ok Your answer is acceptable!^ ^
Test #4:
score: 0
Accepted
time: 8ms
memory: 4116kb
input:
2224 5000 -500000 0 -499999 -30 -499998 -59 -499997 -87 -499996 -114 -499995 -140 -499994 -165 -499993 -189 -499992 -212 -499991 -234 -499990 -255 -499989 -275 -499988 -294 -499987 -312 -499986 -329 -499985 -345 -499984 -360 -499982 -389 -499981 -403 -499979 -430 -499978 -443 -499976 -468 -499975 -4...
output:
931340796015.3750000596 410570465847.7500000000 225774975043.7500000000 686588110927.3750000000 803635163394.8750000000 440321806244.7499999702 781364862674.5000000596 303496624306.7500000000 146653887864.7500000000 1361017661096.7500000000 409649028457.5000000000 417747460932.7500000000 46509181005...
result:
ok Your answer is acceptable!^ ^
Test #5:
score: 0
Accepted
time: 7ms
memory: 3952kb
input:
4672 5000 -300 0 -299 -43 -298 -85 -297 -126 -296 -166 -295 -205 -294 -243 -293 -280 -292 -316 -291 -351 -290 -385 -289 -418 -288 -450 -287 -481 -286 -511 -285 -540 -284 -568 -283 -595 -282 -621 -281 -646 -280 -670 -279 -693 -278 -715 -276 -758 -275 -779 -273 -820 -272 -840 -270 -879 -269 -898 -267 ...
output:
356616.5000000000 121018.5000000000 0.2500000000 189.6250000000 103099.6250000000 83253.1250000000 131701.2500000000 58352.5000000000 355863.1250000000 197638.8597240473 605772.4121621622 2062.4458977408 113763.2500000000 134694.5000000000 74679.6520547945 114481.2500000000 60577.2500000000 7456.250...
result:
ok Your answer is acceptable!^ ^
Test #6:
score: 0
Accepted
time: 5ms
memory: 3924kb
input:
576 5000 -300 0 -299 -15 -298 -29 -297 -42 -296 -54 -295 -65 -294 -75 -293 -84 -292 -92 -290 -107 -289 -114 -287 -127 -286 -133 -284 -144 -283 -149 -280 -163 -278 -172 -275 -185 -274 -189 -270 -204 -267 -215 -265 -222 -262 -232 -258 -245 -257 -248 -252 -262 -248 -273 -245 -281 -240 -294 -238 -299 -2...
output:
189295.2500000000 377943.2944162437 299473.0000000000 243821.9171974522 559270.9922279793 100367.5923396675 472743.1250000000 374450.6250000000 77260.6250000000 106891.2307692308 193578.1250000000 98895.0654145078 124020.0000000000 296138.8750000000 1209.1250000000 480040.6250000000 133543.970689655...
result:
ok Your answer is acceptable!^ ^
Test #7:
score: 0
Accepted
time: 4ms
memory: 3848kb
input:
336 5000 -300 0 -299 -11 -298 -21 -297 -30 -296 -38 -295 -45 -294 -51 -292 -62 -291 -67 -289 -76 -288 -80 -285 -91 -283 -98 -280 -108 -279 -111 -275 -122 -272 -130 -270 -135 -267 -142 -263 -151 -258 -162 -257 -164 -251 -175 -246 -184 -242 -191 -239 -196 -234 -204 -227 -215 -225 -218 -218 -228 -213 -...
output:
478.1250000000 408.1250000000 1341.2500000000 861.2500000000 4210.0000000000 1709.1250000000 846.2500000000 1389.1250000000 722.5000000000 753.1250000000 574.2500000000 1167.1250000000 439.6250000000 5650.2500000000 619.6250000000 2664.5000000000 2138.6250000000 2138.6250000000 1226.2500000000 1226....
result:
ok Your answer is acceptable!^ ^
Test #8:
score: 0
Accepted
time: 5ms
memory: 4040kb
input:
336 5000 -300 0 -299 -11 -298 -21 -297 -30 -296 -38 -295 -45 -294 -51 -292 -62 -291 -67 -289 -76 -288 -80 -285 -91 -283 -98 -280 -108 -279 -111 -275 -122 -272 -130 -270 -135 -267 -142 -263 -151 -258 -162 -257 -164 -251 -175 -246 -184 -242 -191 -239 -196 -234 -204 -227 -215 -225 -218 -218 -228 -213 -...
output:
95.1250000000 8382.6250000000 77361.1250000000 142408.1250000000 98056.2500000000 110581.2500000000 20413.0000000000 1253.1250000000 64468.6250000000 8915.6250000000 93179.1250000000 26286.2500000000 35118.2500000000 129681.2500000000 59545.6250000000 49997.9103773585 1685.1250000000 58020.625000000...
result:
ok Your answer is acceptable!^ ^
Test #9:
score: 0
Accepted
time: 10ms
memory: 3956kb
input:
5000 5000 -50000000 0 -49999912 -33572 -49999824 -59400 -49999710 -83347 -49999578 -105149 -49999382 -130924 -49999166 -154591 -49998916 -178069 -49998599 -203894 -49998262 -228398 -49997905 -251647 -49997466 -277451 -49997003 -302413 -49996511 -326907 -49995964 -352128 -49995393 -376671 -49994795 -...
output:
481990667522174063.0000000000 900047257776892012.9375000000 84250235108292086.0468750000 357963472278544291.7500000000 758024710210129399.3750000000 651805790712522724.3750000000 422072215223185896.7500000000 571948904059660677.3437500000 685946954834850006.6875000000 781017527404628198.5000000000 3...
result:
ok Your answer is acceptable!^ ^
Test #10:
score: 0
Accepted
time: 10ms
memory: 3896kb
input:
5000 5000 -50000000 0 -49999762 -138397 -49999461 -244153 -49999007 -349713 -49998392 -456086 -49997577 -566637 -49996632 -673023 -49995462 -784273 -49994137 -894156 -49992625 -1005080 -49990945 -1115094 -49989066 -1226720 -49987021 -1337531 -49984788 -1449227 -49982415 -1559155 -49979887 -1668061 -...
output:
259053256470500481.8593750000 472297859897907116.9062500000 271976522374482758.8750000000 930648882061706004.8750000000 110596174224097027.8750000000 385963660067947077.5000000000 441658538323309453.6562500000 259108189662189395.8750000000 379723545376251013.7187500000 43293951022380795.8750000000 2...
result:
ok Your answer is acceptable!^ ^
Extra Test:
score: 0
Extra Test Passed