Code Chef DARTSEGM(计算几何+凸包)

题面

传送门

题解

好眼熟丫……

一月月赛最后一题……,代码都不用改……

//minamoto
#include<bits/stdc++.h>
#define R register
#define fi first
#define se second
#define ll long long
#define pb push_back
#define IT vector<pair<node,int> >::iterator
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
    R int res,f=1;R char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
    return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R ll x){
    if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
    while(z[++Z]=x%10+48,x/=10);
    while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=200005,S=15,L=15005;
struct node{
    int x,y;
    inline node(){}
    inline node(R int xx,R int yy):x(xx),y(yy){}
    inline node operator +(const node &b)const{return node(x+b.x,y+b.y);}
    inline node operator -(const node &b)const{return node(x-b.x,y-b.y);}
    inline ll operator *(const node &b)const{return 1ll*x*b.y-1ll*y*b.x;}
    inline bool operator <(const node &b)const{return x<b.x||x==b.x&&y<b.y;}
    inline ll norm(){return 1ll*x*x+1ll*y*y;}
}p[N];
struct Convex{
    vector<node>up,dw;
    inline Convex(){}
    inline Convex(const vector<node> &b){up=b,dw=b,build();}
    Convex operator +(const Convex &b)const{
        Convex c;
        c.up.resize(up.size()+b.up.size());
        merge(up.begin(),up.end(),b.up.begin(),b.up.end(),c.up.begin());
        c.dw.resize(dw.size()+b.dw.size());
        merge(dw.begin(),dw.end(),b.dw.begin(),b.dw.end(),c.dw.begin());
        c.build();
        return c;
    }
    void build(){
        int n=up.size(),m=0;
        fp(i,0,n-1){
            while(m>1&&(up[i]-up[m-2])*(up[m-1]-up[m-2])<=0)--m;
            up[m++]=up[i];
        }
        up.resize(m);
        n=dw.size(),m=0;
        fp(i,0,n-1){
            while(m>1&&(dw[i]-dw[m-2])*(dw[m-1]-dw[m-2])>=0)--m;
            dw[m++]=dw[i];
        }
        dw.resize(m);
    }
    ll solve(){
        vector<node>st=dw;
        fd(i,up.size()-2,0)st.pb(up[i]);
        int n=st.size()-1;ll res=0;
        for(R int i=0,j=1;i<n;++i){
            while((st[j+1]-st[i]).norm()>(st[j]-st[i]).norm())j=(j+1)%n;
            cmax(res,(st[j]-st[i]).norm()),cmax(res,(st[j+1]-st[i+1]).norm());
        }
        return res;
    }
}st[L][17];
int n,m,Log[L],rt[N];vector<pair<node,int> >c[L];
Convex rmq(int l,int r){
    if(l>r)return Convex();
    int k=Log[r-l+1];
    return st[l][k]+st[r-(1<<k)+1][k];
}
int main(){
//  freopen("testdata.in","r",stdin);
//  freopen("testdata.out","w",stdout);
    n=read(),m=(n-1)/S+1;
    fp(i,1,n)p[i].x=read(),p[i].y=read(),rt[i]=(i-1)/S+1;
    fp(i,1,m){
        int l=(i-1)*S+1,r=min(n,i*S);
        fp(j,l,r)c[i].pb(make_pair(p[j],j));
        sort(c[i].begin(),c[i].end());
        vector<node>point;
        for(IT it=c[i].begin();it!=c[i].end();++it)point.pb(it->fi);
        st[i][0]=Convex(point);
    }
    fp(i,2,m)Log[i]=Log[i>>1]+1;
    fp(j,1,Log[m])fp(i,1,m-(1<<j)+1)st[i][j]=st[i][j-1]+st[i+(1<<(j-1))][j-1];
    int q=read(),l,r;
    while(q--){
        l=read(),r=read();
        if(rt[l]==rt[r]){
            vector<node>point;
            for(IT it=c[rt[l]].begin();it!=c[rt[l]].end();++it)
                if(it->se>=l&&it->se<=r)point.pb(it->fi);
            Convex res(point);
            print(res.solve());
        }else{
            vector<node>pointl,pointr;
            for(IT it=c[rt[l]].begin();it!=c[rt[l]].end();++it)
                if(it->se>=l&&it->se<=r)pointl.pb(it->fi);
            for(IT it=c[rt[r]].begin();it!=c[rt[r]].end();++it)
                if(it->se>=l&&it->se<=r)pointr.pb(it->fi);
            Convex res=rmq(rt[l]+1,rt[r]-1)+Convex(pointl)+Convex(pointr);
            print(res.solve());
        }
    }
    return Ot(),0;
}
posted @ 2019-04-18 13:01  bztMinamoto  阅读(152)  评论(0编辑  收藏  举报
Live2D