bzoj2300: [HAOI2011]防线修建

bzoj2300: [HAOI2011]防线修建

Description

近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了。可是A国上层现在还犹豫不决,到底该把哪些城市作为保护对象呢?又由于A国的经费有限,所以希望你能帮忙完成如下的一个任务:
1.给出你所有的A国城市坐标
2.A国上层经过讨论,考虑到经济问题,决定取消对i城市的保护,也就是说i城市不需要在防线内了
3.A国上层询问对于剩下要保护的城市,修建防线的总经费最少是多少
你需要对每次询问作出回答。注意单位1长度的防线花费为1。
A国的地形是这样的,形如下图,x轴是一条河流,相当于一条天然防线,不需要你再修建
A国总是有两个城市在河边,一个点是(0,0),一个点是(n,0),其余所有点的横坐标均大于0小于n,纵坐标均大于0。A国有一个不在(0,0)和(n,0)的首都。(0,0),(n,0)和首都这三个城市是一定需要保护的。

上图中,A,B,C,D,E点为A国城市,且目前都要保护,那么修建的防线就会是A-B-C-D,花费也就是线段AB的长度+线段BC的长度+线段CD的长度,如果,这个时候撤销B点的保护,那么防线变成下图

Input

第一行,三个整数n,x,y分别表示河边城市和首都是(0,0),(n,0),(x,y)。
第二行,一个整数m。
接下来m行,每行两个整数a,b表示A国的一个非首都非河边城市的坐标为(a,b)。
再接下来一个整数q,表示修改和询问总数。
接下来q行每行要么形如1 i,要么形如2,分别表示撤销第i个城市的保护和询问。

Output

对于每个询问输出1行,一个实数v,表示修建防线的花费,保留两位小数

Sample Input

4 2 1
2
1 2
3 2
5
2
1 1
2
1 2
2

Sample Output

6.47
5.84
4.47

HINT

m<=100000,q<=200000,n>1
所有点的坐标范围均在10000以内, 数据保证没有重点

题目大意

给你一堆点和删除操作,维护凸包上凸壳周长。

知识点:动态凸包

动态凸包大概就是在 O(nlogn) 的时间内维护凸包,支持插入,不支持删除,工具是平衡树(当然是选择STL,不过大佬要手打我也没意见)
算法也不难,大概有极角和水平两种算法。
极角大概就维护极角序,然后插入点的时候lower_bound一下找到前驱后继,然后删除即可。
水平的话比较麻烦,维护水平序之后还要维护上凸壳和下凸壳,方法几乎一样。当然好像可以把上凸壳倒过来(关于x轴对称),维护两个下凸壳。

分析

这题的好处在于只需要维护上凸壳。
删除怎么整?
离线倒过来做,就是插入了。
既然是上凸壳,当然是选择水平序啦。

代码

/**************************************************************
    Problem: 2300
    User: 2014lvzelong
    Language: C++
    Result: Accepted
    Time:420 ms
    Memory:5816 kb
****************************************************************/

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
#include<set>
using namespace std;
const int N = 220000;
int read() {
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int top, m, T, opt[N];
bool vis[N];
double ans, ret[N];
struct point {
    int x, y;
    point(int a = 0, int b = 0) : x(a), y(b) {}
    int operator * (point a) const {return x * a.y - y * a.x;}
    int operator ^ (point a) const {return x * a.x + y * a.y;}
    point operator - (point a) const {return point(x - a.x, y - a.y);}
    bool operator < (point a) const {return x == a.x ? y < a.y : x < a.x;}
}p[N];
set<point>q;
int sqr(int x) {return (long long)x * x;}
int sqr(point p) {return p ^ p;}
double len(point p) {return sqrt(sqr(p));}
void Insert(point p) {
    set<point>::iterator r = q.lower_bound(p), l = r, t;
    --l;
    if((*r - p) * (*l - p) > 0) return ;
    ans -= len(*r - *l);
    t = r; ++r;
    for(;r != q.end();t = r, ++r) {
        if((*r - p) * (*t - p) > 0) break;
        ans -= len(*r - *t);
        q.erase(t);
    }
    for(t = l ;l != q.begin(); t = l) {
        --l; if((*t - p) * (*l - p) > 0) break;
        ans -= len(*l - *t);
        q.erase(t);
    }
    q.insert(p); l = r = t = q.find(p);
    --l; ++r;
    ans += len(*l - *t) + len(*r - *t);
}
int main() {
    point s, t; t.x = read(); s.x = read(), s.y = read(); 
    q.insert(point(0, 0)); q.insert(t); q.insert(s);
    ans += len(s) + len(t - s);
    m = read();
    for(int i = 1;i <= m; ++i) {p[i].x = read(); p[i].y = read();}
    T = read();
    for(int i = 1;i <= T; ++i) {
        opt[i] = read();
        if(opt[i] == 1) vis[opt[i] = read()] = true; 
        else opt[i] = 0;
    }
    for(int i = 1;i <= m; ++i) if(!vis[i]) Insert(p[i]);
    for(int i = T; i; --i) if(opt[i]) Insert(p[opt[i]]);
    else ret[i] = ans;
    for(int i = 1;i <= T; ++i) if(!opt[i]) printf("%.2lf\n", ret[i]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值