[日常摸鱼]Luogu2521[HAOI2011]防线修建-set维护凸包

https://www.luogu.org/problemnew/show/2521

题意:维护一个上凸包:删点,查询周长

 


 

 

很容易想到把问题转换为离线:先读入全部操作,记录下最后剩下的点,倒着加点来维护凸包,同时也倒着做询问。

然后问题就变成了怎么维护加点的操作,这题其实只要维护上半个凸包(其实也有一点启发性了吧),用set存凸包的点集,对于要加的点往左右两边一直把不行的点删掉就好了,因为一个点最多被删一次所以加上set的$log$实际复杂度是$O(nlog n)$的而不是$O(n^2)$。

如果要维护整个凸包就根据y来记录一下两边的位置根据上下凸包分类讨论一下来维护。

#include<cstdio>
#include<cmath>
#include<set>
#include<algorithm>
using namespace std;
const int N=100005;
struct q
{
    double ans;
    int op,idx;
}ask[N<<1];
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
}p[N];
inline bool operator <(Point a,Point b)
{
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
inline Point operator  - (Point a,Point b)
{
    return Point(a.x-b.x,a.y-b.y);
}
inline double cross(Point a,Point b)
{
    return a.x*b.y-a.y*b.x;
}
inline double dot(Point a,Point b)
{
    return a.x*b.x+a.y*b.y;
}
inline double lenght(Point a)
{
    return sqrt(dot(a,a));
}
double ans;
set<Point>s;
bool mark[N];
inline void ins(Point x)
{
    set<Point>::iterator r=s.lower_bound(x);
    set<Point>::iterator l=r;l--;
    if(cross(*r-*l,x-*l)<0)return;
    ans-=lenght(*r-*l);
    s.insert(x);
    while(1)
    {
        set<Point>::iterator it=r++;
        if(r==s.end())break;
        if(cross(*r-x,*it-x)>0)break;
        ans-=lenght(*it-*r);
        s.erase(*it); 
    }
    while(1)
    {
        if(l==s.begin())break;
        set<Point>::iterator it=l--;
        if(cross(*l-x,*it-x)<0)break;
        ans-=lenght(*it-*l);
        s.erase(*it);
    }
    l=r=s.find(x);l--;r++;
    ans+=lenght(x-*l)+lenght(x-*r);
}
int main()
{
    int m,q;double x,y,n;
    scanf("%lf%lf%lf",&n,&x,&y); 
    s.insert(Point(0,0));s.insert(Point(n,0));s.insert(Point(x,y));
    ans+=lenght(Point(x,y))+lenght(Point(x-n,y));
    scanf("%d",&m);
    for(register int i=1;i<=m;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
    scanf("%d",&q);
    for(register int i=1;i<=q;i++)
    {
        scanf("%d",&ask[i].op);
        if(ask[i].op==1)
            scanf("%d",&ask[i].idx),mark[ask[i].idx]=1;
    }
    for(register int i=1;i<=m;i++)if(!mark[i])ins(p[i]);
    for(register int i=q;i>=1;i--)
    {
        if(ask[i].op==2)ask[i].ans=ans;
        else ins(p[ask[i].idx]);
    }
    for(register int i=1;i<=q;i++)if(ask[i].op==2)printf("%.2lf\n",ask[i].ans);
    return 0;
}

 

posted @ 2018-01-18 22:53  yoshinow2001  阅读(136)  评论(0编辑  收藏  举报