bzoj2300: [HAOI2011]防线修建(set+凸包)

传送门
题意:动态维护凸包周长。


思路:
见这篇求面积的吧反正都是一个套路。
代码:

#include
#define int long long
#define ri register int
using namespace std;
inline int read(){
    int ans=0;
    bool f=1;
    char ch=getchar();
    while(!isdigit(ch))f^=(ch=='-'),ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return f?ans:-ans;
}
typedef long long ll;
const int N=2e5+5;
struct pot{
    ll x,y;
    double ang;
    pot(ll _x=0,ll _y=0):x(_x),y(_y){}
    pot(ll _x,ll _y,double _ang):x(_x),y(_y),ang(_ang){}
    friend inline pot operator+(const pot&a,const pot&b){return pot(a.x+b.x,a.y+b.y);}
    friend inline pot operator-(const pot&a,const pot&b){return pot(a.x-b.x,a.y-b.y);}
    friend inline ll operator^(const pot&a,const pot&b){return a.x*b.y-a.y*b.x;}
    friend inline bool operator<(const pot&a,const pot&b){return a.ang<b.ang;}
    inline double mod(){return sqrt((double)(x*x+y*y));}
}a[N];
inline double dist(const pot&a,const pot&b){return (a-b).mod();}
set<pot>S;
typedef set<pot>::iterator It;
double sum=0;
int n,m;
bool vis[N];
struct qry{int op,id;}ask[N];
vector<double>ans;
inline pot Pre(const pot&x){
    if(S.count(x)>0)return x;
    It it=S.lower_bound(x);
    if(it==S.begin())it=S.end();
    return *--it;
}
inline pot Suf(const pot&x){
    It it=S.upper_bound(x);
    if(it==S.end())it=S.begin();
    return *it;
}
inline bool in(const pot&x){
    pot suf=Suf(x),pre=Pre(x);
    return ((x-pre)^(suf-pre))<=0;
}
inline void insert(const pot&x){
    if(in(x))return;
    pot suf=Suf(x),pre=Pre(x);
    sum-=dist(suf,pre);
    while(1){
        pot p1=Pre(x),p2;
        S.erase(p1);
        p2=Pre(x);
        if(((x-p1)^(p2-p1))>=0){S.insert(p1);break;}
        sum-=dist(p1,p2);
    }
    while(1){
        pot p1=Suf(x),p2;
        S.erase(p1);
        p2=Suf(x);
        if(((x-p1)^(p2-p1))<=0){S.insert(p1);break;}
        sum-=dist(p1,p2);
    }
    sum+=dist(Pre(x),x),sum+=dist(Suf(x),x);
    S.insert(x);
}
signed main(){
    srand(time(NULL));
    int xxx=rand(),yyy=rand(),zzz=rand(),ttt=xxx+yyy+zzz;
    a[1]=pot(0,0),a[2]=pot(read(),0),a[3].x=read(),a[3].y=read();
    double X,Y;
    X=(double)(a[1].x*xxx+a[2].x*yyy+a[3].x*zzz)/ttt;
    Y=(double)(a[1].y*xxx+a[2].y*yyy+a[3].y*zzz)/ttt;
    for(ri i=1;i<=3;++i)S.insert(pot(a[i].x,a[i].y,atan2(Y-a[i].y,X-a[i].x)));
    sum=dist(a[2],a[3])+dist(a[3],a[1]);
    n=read();
    for(ri i=1;i<=n;++i)a[i].x=read(),a[i].y=read();
    m=read();
    for(ri i=1;i<=m;++i){
        ask[i].op=read();
        if(ask[i].op==1)vis[ask[i].id=read()]=1;
    }
    for(ri i=1;i<=n;++i)if(!vis[i])insert(pot(a[i].x,a[i].y,atan2(Y-a[i].y,X-a[i].x)));
    for(ri i=m,id;i;--i){
        if(ask[i].op==1){
            id=ask[i].id;
            insert(pot(a[id].x,a[id].y,atan2(Y-a[id].y,X-a[id].x)));
        }
        else ans.push_back(sum);
    }
    for(ri i=ans.size()-1;~i;--i)printf("%.2lf\n",ans[i]);
    return 0;
}


你可能感兴趣的:(#,凸包,#,计算几何,#,STL)