QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#578016#8082. Minimum Euclidean DistanceAuroraaWA 0ms3892kbC++206.8kb2024-09-20 16:00:212024-09-20 16:00:22

Judging History

你现在查看的是最新测评结果

  • [2024-09-20 16:00:22]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3892kb
  • [2024-09-20 16:00:21]
  • 提交

answer

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
#define int long long
#define double long double
const int N=1e5+5;
const double pi=acos(-1);
const double eps=1e-8;
int sgn(double x){
    if(fabs(x)<=eps)return 0;
    return x<0?-1:1;
}
struct Point{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
    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};}
    double cross(const Point&a)const{return x*a.y-y*a.x;}
    int toleft(const Point&a)const{return sgn(cross(a));}
    double dot(const Point&a)const{return x*a.x+y*a.y;}
    bool operator<(const Point&a)const{
        if(x==a.x)return y<a.y;
        return x<a.x;
    }
    bool operator==(const Point&a)const{return x==a.x && y==a.y;}
    double dis2(const Point&a)const{return (x-a.x)*(x-a.x)+(y-a.y)*(y-a.y);}
    double dis(const Point&a)const{return sqrt(dis2(a));}
    double len()const{return sqrt(x*x+y*y);}
};
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);
    }
    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)<0;
    }
    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());
    }
};
struct Convex{
    vector<Point>p;
    // -1 在边上 | 0 在外部 | 1 在内部
    int is_in(const Point&a)const{
        if(p.size()==1)return a==p[0]?-1:0;
        if(p.size()==2)
        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 find_max(const Point u){
        int l=0,r=p.size()-1;
        // x 的极角序小于 y
        auto cmp=[&](const Point&x,const Point&y){
            return (x-u).toleft(y-u)==1;
        };
        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;
    }
    int find_min(const Point u){
        int l=0,r=p.size()-1;
        // x 的极角序大于 y
        auto cmp=[&](const Point&x,const Point&y){
            return (y-u).toleft(x-u)==1;
        };
        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;
    }
};
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);
    }
//    cin>>m;
    Convex con=Andrew(vec);
    vector<Point>p=con.p;
    n=p.size();
    double cir=0,area=0;
    for(int i=0;i<n;i++){
        cir+=p[i].dis(p[(i+1)%n]);
        area+=p[i].cross(p[(i+1)%n]);
    }
    area/=2.0l;
    while(m--){
        int x1,y1,x2,y2;
        cin>>x1>>y1>>x2>>y2;
        Point a((x1+x2)/2.0l,(y1+y2)/2.0l);
        double R=a.dis(Point(x1,y1));
        double ans=R*R/2.0l;
        if(con.is_in(a)){
            cout<<fixed<<setprecision(10)<<ans<<endl;
            continue;
        }
        auto nxt=[&](int x){return x==(n-1)?0:x+1;};
        auto pre=[&](int x){return x==0?(n-1):x-1;};
        int mx=con.find_max(a),mn=con.find_min(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]).cross(a-p[mx])),op2=sgn((p[mn]-p[pre(mn)]).cross(a-p[pre(mn)]));
        if(mx!=mn && op1 != op2){
            int l=mx,r=mn-1;
            if(r<l)r+=n;
            while(l<r){
                int mid=(l+r+1)>>1;
                int p1=sgn((p[(mid+1)%n]-p[mid%n]).cross(a-p[mid%n]));
                if(p1==op1)l=mid;
                else r=mid-1;
            }
            l%=n;
            dis=min(dis,Segment(p[l],p[nxt(l)]).dis(a));
        }
        ans+=dis*dis;
        cout<<fixed<<setprecision(10)<<ans<<endl;

//        int mx=con.find_max(a),mn=con.find_min(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]).cross(a-p[mx])),op2=sgn((p[mn]-p[pre(mn)]).cross(a-p[pre(mn)]));
//        if(mx!=mn && op1 != op2){
//            int l=mx,r=mn-1;
//            if(r<l)r+=n;
//            while(l<r){
//                int mid=(l+r+1)>>1;
//                int p1=sgn((p[(mid+1)%n]-p[mid%n]).cross(a-p[mid%n]));
//                if(p1==op1)l=mid;
//                else r=mid-1;
//            }
//            l%=n;
//            dis=min(dis,Segment(p[l],p[nxt(l)]).dis(a));
//        }

//        double s=cir*dis+pi*dis*dis+area;
//        int t=0;
//        while(s>area){
//            t++;
//            s/=2;
//        }
//        cout<<t<<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: 3892kb

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: -100
Wrong Answer
time: 0ms
memory: 3864kb

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:

796.5000000000
235.0000000000
1051.2500000000
66.6250000000
426.3750000000
1211.8750000000
686.8750000000
521.8750000000
689.6250000000
436.2500000000

result:

wrong answer Except 589.500000000000, but found 796.500000000000!QAQ