BZOJ 2300: [HAOI2011]防线修建|set维护凸壳

因为只支持插入操作,所以需要倒过来做..
第一次写维护凸壳的东西…不抄题解..自己YY斜率乱搞(都忘了写叉乘)代码又长常数又大
1A一刻赛艇~
还是找一个靠谱的的模板吧...

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<iostream>
#define N 500050
#define inf 2000000000
using namespace std;
int sc()
{
    int i=0,f=1; char c=getchar();
    while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')i=i*10+c-'0',c=getchar();
    return i*f;
}
set<pair<int,int> >st;
double ans,A[100002];
int a[100002],b[100002],d[100002];
int n,fx,fy,top,m,q;
bool c[100002];
double kk(int x,int y){return sqrt((double)x*x+y*y);}
double cal(int x,int y)
{
    if(!x)return inf;
    return (double)y/x;
}
void add(int x,int y)
{
    set<pair<int,int> >::iterator it,e;
    it=st.lower_bound(make_pair(x,y));
    if((*it).first==x)return;
    bool flag=0;
    while(++it!=st.end())
    {
        pair<int,int> b=*it,a=*--it;
        double l1=cal(a.first-x,a.second-y);
        double l2=cal(b.first-x,b.second-y);
        if(l2<l1)break;
        if(!flag)
        {
            pair<int,int> c=*--it;it++;
            ans-=kk(c.first-a.first,c.second-a.second);
            flag=1;
        }
        ans-=kk(a.first-b.first,a.second-b.second);
        e=it,++it;
        st.erase(e);
    }
    it=st.lower_bound(make_pair(x,y));it--;
    while(it!=st.begin())
    {
        pair<int,int> a=*it,b=*--it;
        double l1=cal(a.first-x,a.second-y);
        double l2=cal(b.first-x,b.second-y);
        if(l2>l1)break;
        if(!flag)
        {
            it++;it++;
            pair<int,int> c=*it;it--;it--;
            ans-=kk(c.first-a.first,c.second-a.second);
            flag=1;
        }
        ans-=kk(a.first-b.first,a.second-b.second);
        e=it,st.erase(++e);
    }
    if(flag) 
    {
        it=st.lower_bound(make_pair(x,y));
        ans+=kk((*it).first-x,(*it).second-y);
        it--;
        ans+=kk((*it).first-x,(*it).second-y);
        st.insert(make_pair(x,y));
    }   
    else
    {
        it=st.lower_bound(make_pair(x,y));
        pair<int,int> a=*it,b=*--it;
        double l1=cal(a.first-x,a.second-y);
        double l2=cal(b.first-x,b.second-y);
        if(l1<l2)
        {
            ans-=kk(a.first-b.first,a.second-b.second);
            ans+=kk((*it).first-x,(*it).second-y);
            it++;
            ans+=kk((*it).first-x,(*it).second-y);
            st.insert(make_pair(x,y));
        }
    }
}
int main()
{
    n=sc(),fx=sc(),fy=sc(),m=sc();
    for(int i=1;i<=m;i++)
        a[i]=sc(),b[i]=sc();
    q=sc();
    for(int i=1;i<=q;i++)
    {
        d[i]=sc();
        if(d[i]==1)c[d[i]=sc()]=1;
        else d[i]=0;
    }
    st.insert(make_pair(0,0));
    st.insert(make_pair(n,0));
    st.insert(make_pair(fx,fy));
    ans=kk(fx,fy)+kk(fx-n,fy);
    for(int i=1;i<=m;i++)
        if(!c[i])add(a[i],b[i]);
    for(int i=q;i;i--)
    {
        if(!d[i])A[++top]=ans;
        else add(a[d[i]],b[d[i]]);
    }
    while(top)printf("%.2lf\n",A[top--]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值