离线然后倒着做就变成了支持加点的动态凸包...用平衡树维护上凸壳...时间复杂度O(NlogN)

-----------------------------------------------------------------------

#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<algorithm>
 
using namespace std;
 
#define sqr(x) ((x) * (x))
#define Dist(a, b) sqrt(sqr(a.x - b.x) + sqr(a.y - b.y))
#define K(a, b) ((double) (a.y - b.y) / (a.x - b.x))
 
const int maxn = 100009;
const double eps = 1e-9;
 
double ans[maxn], res;
int N, Q, q[maxn], n;
bool F[maxn];
 
inline int read() {
char c = getchar();
for(; !isdigit(c); c = getchar());
int ret = 0;
for(; isdigit(c); c = getchar())
ret = ret * 10 + c - '0';
return ret;
}
 
struct P {
int x, y;
P(int _x = 0, int _y = 0) : x(_x), y(_y) {
}
bool operator < (const P &t) const {
return x < t.x || (x == t.x && y < t.y);
}
bool operator == (const P &t) const {
return x == t.x && y == t.y;
}
bool operator != (const P &t) const {
return x != t.x || y != t.y;
}
} p[maxn];
 
struct Node {
Node *ch[2];
P o;
int r;
} pool[maxn], *pt, *Root, *Null;
 
void Init_Treap() {
pt = pool;
Null = pt++;
Null->ch[0] = Null->ch[1] = Null;
Root = Null;
}
 
Node* newNode(P o) {
pt->o = o;
pt->r = rand();
pt->ch[0] = pt->ch[1] = Null;
return pt++;
}
 
void Rotate(Node*&t, int d) {
Node* o = t->ch[d ^ 1];
t->ch[d ^ 1] = o->ch[d];
o->ch[d] = t;
t = o;
}
 
void Delete(Node*&t, P o) {
int d = (t->o == o ? -1 : (t->o < o));
if(d == -1) {
if(t->ch[0] != Null && t->ch[1] != Null) {
int _d = (t->ch[0]->r > t->ch[1]->r);
Rotate(t, _d), Delete(t->ch[_d], o);
} else
t = (t->ch[0] != Null ? t->ch[0] : t->ch[1]);
} else
Delete(t->ch[d], o);
}
 
void Insert(Node*&t, P o) {
if(t == Null) {
t = newNode(o);
} else {
int d = (t->o < o);
Insert(t->ch[d], o);
if(t->ch[d]->r > t->r) Rotate(t, d ^ 1);
}
}
 
Node* Pred(P o) {
Node* ret;
for(Node* t = Root; t != Null; ) if(t->o < o) {
ret = t, t = t->ch[1];
} else
t = t->ch[0];
return ret;
}
 
Node* Succ(P o) {
Node* ret;
for(Node* t = Root; t != Null; ) if(o < t->o) {
ret = t, t = t->ch[0];
} else
t = t->ch[1];
return ret;
}
 
bool chk(P o) {
for(Node* t = Root; t != Null; ) {
if(t->o.x == o.x) return o.y > t->o.y;
t = (t->o < o ? t->ch[1] : t->ch[0]);
}
return true;
}
 
void Add(P o) {
Node *L = Pred(o), *R = Succ(o), *LL, *RR;
if(R->o.x == o.x) return;
if(L->o.x == o.x) {
LL = Pred(L->o);
res += Dist(LL->o, o) + Dist(R->o, o) - Dist(L->o, R->o) - Dist(LL->o, L->o);
Delete(Root, L->o);
L = Pred(o);
} else if(K(R->o, o) - K(L->o, o) > eps) {
return;
} else 
res += Dist(L->o, o) + Dist(R->o, o) - Dist(L->o, R->o);
Insert(Root, o);
if(L->o != P(0, 0)) {
LL = Pred(L->o);
while(L->o != P(0, 0) && K(o, L->o) - K(LL->o, L->o) > eps) {
Delete(Root, L->o);
res += Dist(LL->o, o) - Dist(LL->o, L->o) - Dist(L->o, o);
L = LL, LL = Pred(L->o);
}
}
if(R->o != P(n, 0)) {
RR = Succ(R->o);
while(R->o != P(n, 0) && K(RR->o, R->o) - K(R->o, o) > eps) {
Delete(Root, R->o);
res += Dist(RR->o, o) - Dist(RR->o, R->o) - Dist(R->o, o);
R = RR, RR = Succ(R->o);
}
}
}
 
void Work() {
for(int i = 0; i < N; i++)
if(!F[i]) Add(p[i]);
for(int i = Q; i--; ) if(~q[i]) {
if(F[q[i]]) Add(p[q[i]]);
F[q[i]] = false;
} else
ans[i] = res;
for(int i = 0; i < Q; i++)
if(!~q[i]) printf("%.2lf\n", ans[i]);
}
 
void Init() {
n = read();
int x = read(), y = read();
Init_Treap();
Insert(Root, P(0, 0));
Insert(Root, P(n, 0));
Insert(Root, P(x, y));
res = sqrt(x * x + y * y) + sqrt(sqr(x - n) + y * y);
N = read();
for(int i = 0; i < N; i++)
p[i].x = read(), p[i].y =read();
memset(F, 0, sizeof F);
Q = read();
for(int i = 0; i < Q; i++) if((q[i] = read()) != 2) {
F[--(q[i] = read())] = true;
} else
q[i] = -1;
}
int main() {
Init();
Work();
return 0;
}

-----------------------------------------------------------------------

2300: [HAOI2011]防线修建

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 623  Solved: 330
[Submit][Status][Discuss]

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

HINT

6.47

5.84

4.47

数据范围:

30%的数据m<=1000,q<=1000

100%的数据m<=100000,q<=200000,n>1

所有点的坐标范围均在10000以内, 数据保证没有重点

Source

BZOJ 2300: [HAOI2011]防线修建( 动态凸包 )的更多相关文章

  1. 【题解】P2521 [HAOI2011]防线修建(动态凸包)

    [题解]P2521 [HAOI2011]防线修建(动态凸包) 凸包是易插入不好删除的东西,按照剧情所以我们时光倒流 然后问题就是维护凸包的周长,支持加入 本来很简单,但是计算几何就是一些小地方经验不足 ...

  2. 【bzoj2300】【Luogu P2521】 [HAOI2011]防线修建 动态凸包,平衡树,Set

    一句话题意:给你一个凸包,每次可以插入一个点或者询问周长. 动态凸包裸题嘛,用\(Set\)实现.最初每个点坐标做乘三处理,便于取初始三角形的重心作为凸包判定原点. #include <bits ...

  3. bzoj 2300 [HAOI2011]防线修建 set动态维护凸包

    题目大意 动态删点,求凸包周长 分析 反过来变成动态加点 用set维护平衡树 具体是找到凸包上左右两点 拆开 就可以把左边当作顺时针求的一个凸包,右边当作逆时针求的一个凸包,像栈那样出set就好了 注 ...

  4. bzoj 2300: [HAOI2011]防线修建 凸包

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=2300 题解 这道题让我们维护一个支持动态删除点的上凸壳 并且告诉了我们三个一定不会被删除 ...

  5. bzoj 2300 : [HAOI2011]防线修建

    set动态维护凸包 #include<iostream> #include<cstdio> #include<cstring> #include<algori ...

  6. BZOJ 2300 [HAOI2011]防线修建 ——计算几何

    只需要倒着插入,然后维护一个凸包就可以了. 可以用来学习set的用法 #include <map> #include <set> #include <cmath> ...

  7. 【BZOJ 2300】 2300: [HAOI2011]防线修建 (动态凸包+set)

    2300: [HAOI2011]防线修建 Description 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上 ...

  8. bzoj2300#2300. [HAOI2011]防线修建

    题解:带删点的维护凸包,1.删点2.查询凸包周长 题解:倒着做就成了带加点的维护凸包,加点时维护一下周长就没了 //#pragma GCC optimize(2) //#pragma GCC opti ...

  9. bzoj千题计划236:bzoj2300: [HAOI2011]防线修建

    http://www.lydsy.com/JudgeOnline/problem.php?id=2300 维护动态凸包,人懒用的set 用叉积判断,不要用斜率 #include<set> ...

随机推荐

  1. BZOJ-1923-外星千足虫-SDOI2010

    描写叙述 分析 首先看上去这貌似是一个高斯消元的题目, 直觉吧- 每次给出的就相当于是一个方程. 然后非常easy想到n条虫子n个x, x_i的系数为0表示这个方程中没有i, 否则为1. 然后系数乘以 ...

  2. win10系统 Visual Studio 2013 Color Theme Editor插件 安装出错

    下载这个版本,用vs2013打开安装即可:http://pan.baidu.com/s/1hrcfY1A

  3. 一台服务器同时搭建IIS和WAMP,利用WAMP 80端口转发

    打开wamp 里面的 httpd.conf 文件,找到以下四个语句,取消注释 #LoadModule proxy_module modules/mod_proxy.so -->LoadModul ...

  4. js 小练习之indexOf

    闲来无事~ 写点小练习 function zz(arr, item) { var a=arr.join("") var b=a.indexOf(item) alert(b) } z ...

  5. 改良版的SQL Service 通用存储过程分页

    上次写了通用存储过程.感觉还是有很大的BUG.就是条件不能参数画化.这个BUG可以说是致命的.但是我一直想在用什么方法能解决这个东西.其实我只是想写少量的代码来做更多的事情.我想能不能传集合给存储过程 ...

  6. c# 根据中文汉字获取到拼音

    public static String ConvertToPinyin(String str) { if (String.IsNullOrEmpty(str)) return String.Empt ...

  7. 指针直接赋值为整型AND利用宏定义求结构体成员偏移量

    首先我们要更正一个很熟悉的概念,那就是指针不仅仅是“地址”,指针还有一个很重要的特性,那就是“类型”. 指针初始化时,“=”的右操作数; 除外,该语句表示指针为空): 所以 ; 这样的代码是不允许的. ...

  8. 工作备份 build.gradle

    apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion '22.0.0' de ...

  9. Ubuntu各种软件的安装

    普通的例如g++.deadbeef等源中有的软件,可以用apt-get安装 sudo apt-get install XXX 还有很多直接在software center搜索下载 对于下载来源代码需要 ...

  10. windbg 调试技巧

    技巧一:在加载名卸载的时候下断点 1. 加载某个DLL 的时候下断点的WinDBG 命令: sxe ld:[dll name] 然后按F5,进行刷新,再使用lmf 查看装载的Dll名称. 2.  卸载 ...