刚学OI的萌新求助KDT淋雨查询
查看原帖
刚学OI的萌新求助KDT淋雨查询
455490
Sharpsmile楼主2023/2/9 11:08

那个找最近点操作慢飞了QAQ,只有找最远点的话跑的飞快,感觉能用的优化都用掉了(?)

code


#include <iostream>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <istream>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <string.h>
#include <map>
#include <unordered_map>
#include <random>
#include <bitset>
//#define int long long
//#define double long double
#define p1(x) x.first
#define p2(x) x.second
#define i128  __int128_t
//#pragma GCC optimize(2)
#define w(x) t[x].w
#define sw(x) t[x].sw
#define siz(x) t[x].siz
#define lc(x) t[x].c[0]
#define rc(x) t[x].c[1]
#define d(x) t[x].d
#define pii pair<int,int>
//若汁记好了以后再用Ctrl+C/+V你就是狗
using namespace std;
const double INF=2e18+7;
const double alpha=0.6;
struct node{
    double mxx,mxy;
    double  mnx,mny;
    double x,y;
    int w,sw;
    int siz;
    bool d;
    int c[2];
    inline double mxd(double x,double y){
        double dx=max(x-mnx,mxx-x),dy=max(y-mny,mxy-y);
        return dx*dx+dy*dy;
    }
    inline double mnd(double x,double y){
        double dx=min(max(0.,mnx-x),max(0.,x-mxx)),dy=min(max(0.,mny-y),max(0.,y-mxy));
        return dx*dx+dy*dy;
    }
    inline double dt(double px,double py){
        double dx=(px-x),dy=py-y;
        return dx*dx+dy*dy;
    }
}t[100300];
inline void upd(int x){
    t[x].mxx=max(t[x].x,max(t[lc(x)].mxx,t[rc(x)].mxx));
    t[x].mxy=max(t[x].y,max(t[lc(x)].mxy,t[rc(x)].mxy));
    t[x].mnx=min(t[x].x,min(t[lc(x)].mnx,t[rc(x)].mnx));
    t[x].mny=min(t[x].y,min(t[lc(x)].mny,t[rc(x)].mny));
    siz(x)=siz(lc(x))+siz(rc(x))+1;
    sw(x)=sw(lc(x))+sw(rc(x))+w(x);
    
}
int cnt,rt;
inline int nnd(double x,double y,int w,bool d){
    t[++cnt]={x,y,x,y,x,y,w,w,1,d};;
    return cnt;
}
int ct;
int p[100300];
inline void trs(int x){
    p[++ct]=x;
    if(lc(x))trs(lc(x));
    if(rc(x))trs(rc(x));
}
inline bool cmpx(int a,int b){
    return t[a].x<t[b].x;
}
inline bool cmpy(int a,int b){
    return t[a].y<t[b].y;
}
inline int rb(int l,int r,bool d){
    if(r<l)return 0;
    if(l==r){
        lc(p[l])=rc(p[l])=0;
        siz(p[l])=1;
        d(p[l])=d;
        sw(p[l])=w(p[l]);
        upd(p[l]);
        return p[l];
    }
    int mid=l+r>>1;
    nth_element(p+l,p+mid,p+r,d?cmpx:cmpy);
    lc(p[mid])=rb(l,mid-1,!d);
    rc(p[mid])=rb(mid+1,r,!d);
    d(p[mid])=d;
    upd(p[mid]);
    return p[mid];
}
inline void RB(int &x){
        ct=0;
        trs(x);
        int d=d(x);
        x=rb(1,ct,d);
}
inline void chk(int &x){
    if(max(siz(lc(x)),siz(rc(x)))>=siz(x)*alpha)RB(x);
}
inline void ins(int &x,double px,double py,int w,int d){
    if(!x){
        x=nnd(px,py,w,d);
        return ;
    }
    if(d){
        if(px<=t[x].x)ins(lc(x),px,py,w,!d);
        else ins(rc(x),px,py,w,!d);
    }
    else{
        if(py<=t[x].y)ins(lc(x),px,py,w,!d);
        else ins(rc(x),px,py,w,!d);
    }
    upd(x);
    chk(x);
}
inline int g(int x,int lx,int ly,int rx,int ry){
    if(!x)return 0;
    if(rx>=t[x].mxx&&ry>=t[x].mxy&&lx<=t[x].mnx&&ly<=t[x].mny)return sw(x);
    int res=0;
    if(rx>=t[x].x&&ry>=t[x].y&&lx<=t[x].x&&ly<=t[x].y)res=w(x);
    if(d(x)){
        if(lx<=t[lc(x)].mxx)res+=g(lc(x),lx,ly,rx,ry);
        if(rx>=t[rc(x)].mnx)res+=g(rc(x),lx,ly,rx,ry);
    }
    else{
        if(ly<=t[lc(x)].mxy)res+=g(lc(x),lx,ly,rx,ry);
        if(ry>=t[rc(x)].mny)res+=g(rc(x),lx,ly,rx,ry);
    }
    return res;
}
priority_queue<double,vector<double>,greater<double>>qmx;
priority_queue<double>qmn;
double mx,mn;
inline void setmx(int k){
   // while(!qmx.empty())qmx.pop();
    mx=-INF;
//    for(int i=1;i<=k;i++)
//        qmx.push(-INF);
}
inline void setmn(int k){
   // while(!qmn.empty())qmn.pop();
    mn=INF;
//    for(int i=1;i<=k;i++)
//        qmn.push(INF);
}
inline void gmx(int x,double px,double py){
    if(!x)return ;

    if(t[x].mxd(px,py)<mx)return ;
   
     double  tmp=t[x].dt(px,py);
    if(t[x].w)
     mx=max(mx,tmp);
    
    if ((lc(x)&&t[lc(x)].mxd(px,py)>t[rc(x)].mxd(px,py))||!rc(x))
         gmx(lc(x),px,py),gmx(rc(x),px,py);
      else gmx(rc(x),px,py), gmx(lc(x),px,py);
    
}
inline void gmn(int x,double px,double py){
    if(!x)return ;
//    cout<<t[x].mnd(px,py)<<" "<<t[x].w<<endl;;
    if(t[x].mnd(px,py)>mn)return ;
    double  tmp=t[x].dt(px,py);
    //cout<<tmp<<" ";
    if(t[x].w)
    mn=min(mn,tmp);
    if((lc(x)&&t[lc(x)].mnd(px,py)<t[rc(x)].mnd(px,py))||!rc(x))
            gmn(lc(x),px,py),gmn(rc(x),px,py);
         else gmn(rc(x),px,py), gmn(lc(x),px,py);
}
int n,q;
pair<double,double >E[100300];
signed main(){
    ios::sync_with_stdio(0);
    freopen("/Users/noip2019/Downloads/P6247_4.in","r",stdin);
//    freopen("","w",stdout);
    t[0]={-INF,-INF,INF,INF,0,0,0,0,0};
    cin>>n;
    mx=-INF;
    mn=INF;
    for(int i=1;i<=n;i++){
        double x,y;
        cin>>x>>y;
        nnd(x,y,1,0);
        p[i]=i;
    }
    ct=n;
    rt=rb(1,ct,0);
    for(int i=1;i<=n;i++){
        t[i].w=0;
        gmx(rt,t[i].x,t[i].y);
        //gmn(rt,t[i].x,t[i].y);
        t[i].w=1;
        //cout<<i<<" ";
    }
    printf("%.2lf %.2lf",sqrt(mn),sqrt(mx));
    return 0;
}
//luoxiang@huayilawfirm.com
2023/2/9 11:08
加载中...