bzoj2300: [HAOI2011]防线修建【动态凸包】

解题思路:

把询问离线倒序处理,维护只加点的动态凸包即可。

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int getint()
{
    int i=0,f=1;char c;
    for(c=getchar();(c!='-')&&(c<'0'||c>'9');c=getchar());
    if(c=='-')f=-1,c=getchar();
    for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
    return i*f;
}

const int N=200005;
struct point
{
    double x,y;
    point(){}
    point(int _x,int _y):x(_x),y(_y){}
    inline friend point operator - (const point &a,const point &b)
    {return point(a.x-b.x,a.y-b.y);}
    inline friend double operator * (const point &a,const point &b)
    {return a.x*b.y-a.y*b.x;}
    inline friend bool operator < (const point &a,const point &b)
    {return a.x<b.x;}
    inline double dis(){return sqrt(x*x+y*y);}
}p[N];
struct query{int op,id;}q[N];
int n,m,cnt,del[N];double totlen;
vector<double>ans;
set<point>S;
set<point>::iterator it;

void add(point a)
{
    point pre,suf,t;
    it=S.lower_bound(a),suf=*it,pre=*--it;
    if((suf-a)*(pre-a)>=0)return;
    totlen-=(pre-suf).dis();
    it=S.lower_bound(a),suf=*it;
    while((++it)!=S.end())
    {
        t=*it;
        if((t-a)*(suf-a)<=0)S.erase(S.find(suf)),totlen-=(t-suf).dis(),suf=t;
        else break;
    }
    it=--S.lower_bound(a),pre=*it;
    while(it!=S.begin())
    {
        t=*--it;
        if((t-a)*(pre-a)>=0)S.erase(S.find(pre)),totlen-=(pre-t).dis(),pre=t;
        else break;
    }
    totlen+=(a-pre).dis()+(a-suf).dis(),S.insert(a);
}

int main()
{
    //freopen("lx.in","r",stdin);
    p[1]=point(0,0),p[2]=point(getint(),0),p[3].x=getint(),p[3].y=getint();
    S.insert(p[1]),S.insert(p[2]),S.insert(p[3]);
    totlen=(p[3]-p[1]).dis()+(p[3]-p[2]).dis();
    n=getint();
    for(int i=1;i<=n;i++)p[i].x=getint(),p[i].y=getint();
    m=getint();
    for(int i=1;i<=m;i++)
    {
        q[i].op=getint();
        if(q[i].op==1)q[i].id=getint(),del[q[i].id]=1;
    }
    for(int i=1;i<=n;i++)if(!del[i])add(p[i]);
    for(int i=m;i;i--)
    {
        if(q[i].op==2)ans.push_back(totlen);
        else add(p[q[i].id]),del[q[i].id]=0;
    }
    for(int i=ans.size()-1;i>=0;i--)printf("%0.2f\n",ans[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值