题目链接:P2521 [HAOI2011]防线修建

题意:给定点集

每次有两种操作:

1. 删除一个点 (除开(0, 0), (n, 0), 与指定首都(x, y))

2. 询问上凸包长度

至于为什么是上凸包 “三角形任意两边之和大于第三边”就可以证

我们的套路不包括只做删除的在线凸包问题 所以把询问逆序操作

根据套路 只有插入操作的 对答案贡献不独立的凸包问题 我们使用平衡树维护

其实只用求前驱后继 所以蒟蒻就偷懒用了STL的set

一开始构造最后删完的点集的凸包

每次插入一个点时

如果它在当前凸包内部或凸包上 那么不影响答案 直接忽略

如果在外部 从近到远询问它前面的点 类似于Andrew一开始插入点的做法

再从近到远询问它后面的点 做法同样

记得维护答案

 #include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <set>
using namespace std;
const int N = 1e5 + ;
const int Q = 2e5 + ;
const double eps = 1e-;
struct Node{
int id;
double x, y;
}node[N], stk[N], nl;
set<Node> st;
int rf[N];
struct Query{
int x, y;
}q[Q];
double anss[N];
int as;
int n, m, cx, cy, qs;
int top;
double ans;
bool ask[N]; Node operator - (const Node& x, const Node& y){
return (Node){, x.x - y.x, x.y - y.y};
} bool operator < (const Node& x, const Node& y){
if(fabs(x.x - y.x) < eps) return x.y + eps < y.y;
return x.x + eps < y.x;
} inline double cross(Node x, Node y){
return x.x * y.y - x.y * y.x;
} inline double dis(Node x, Node y){
return sqrt((y.x - x.x) * (y.x - x.x)
+ (y.y - x.y) * (y.y - x.y));
} Node next(Node x){
set<Node> :: iterator it;
it = st.upper_bound(x);
if(it != st.end()) return *it;
else return nl;
} Node prev(Node x){
set<Node> :: iterator it;
it = st.lower_bound(x);
if(it != st.begin()) return *(--it);
else return nl;
} inline void Andrew(){
top = ;
for(int i = ; i <= m; i++) if(!ask[node[i].id]){
while(top > && cross(stk[top] - stk[top - ], node[i] - stk[top]) > eps){
ans -= dis(stk[top], stk[top - ]);
top--;
}
stk[++top] = node[i];
if(top > ) ans += dis(stk[top], stk[top - ]);
}
for(int i = ; i <= top; i++)
st.insert(stk[i]);
} inline void init(){
scanf("%d%d%d%d", &n, &cx, &cy, &m);
for(int i = ; i <= m; i++){
scanf("%lf%lf", &node[i].x, &node[i].y);
node[i].id = i;
}
++m; node[m] = (Node){m, , };
++m; node[m] = (Node){m, 1.0 * n, };
++m; node[m] = (Node){m, 1.0 * cx, 1.0 * cy};
sort(node + , node + m + );
for(int i = ; i <= m; i++){
rf[node[i].id] = i;
}
scanf("%d", &qs);
for(int i = ; i <= qs; i++){
scanf("%d", &q[i].x);
if(q[i].x == ){
scanf("%d", &q[i].y);
ask[q[i].y] = ;
}
else {
as++;
q[i].y = as;
}
}
Andrew();
} inline void ins(Node x){
Node pre = prev(x), pp;
Node nxt = next(x), nn;
if(cross(x - pre, nxt - x) > eps){
return ;
}
ans -= dis(pre, nxt);
while(pre.id != m - ){
pp = prev(pre);
if(cross(pre - pp, x - pre) > eps){
ans -= dis(pp, pre);
st.erase(pre);
pre = pp;
}
else break;
}
ans += dis(pre, x);
while(nxt.id != m - ){
nn = next(nxt);
if(cross(nxt - x, nn - nxt) > eps){
ans -= dis(nxt, nn);
st.erase(nxt);
nxt = nn;
}
else break;
}
ans += dis(x, nxt);
st.insert(x);
} int main(){
init();
for(int i = qs; i >= ; i--)
if(q[i].x == )
ins(node[rf[q[i].y]]);
else
anss[q[i].y] = ans;
for(int i = ; i <= as; i++) printf("%.2lf\n", anss[i]);
return ;
}

附上代码:

P2521 [HAOI2011]防线修建的更多相关文章

  1. [luogu P2521] [HAOI2011]防线修建

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

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

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

  3. Bzoj2300 / 洛谷P2521 [HAOI2011]防线修建

    题目描述 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上层现在还犹豫不决,到底该把哪些城市作为保护对象呢?又由于 ...

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

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

  5. BZOJ 2300: [HAOI2011]防线修建( 动态凸包 )

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

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

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

  7. 【BZOJ2300】[HAOI2011]防线修建 set维护凸包

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

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

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

  9. BZOJ2300[HAOI2011]防线修建——非旋转treap+凸包(平衡树动态维护凸包)

    题目描述 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上层现在还犹豫不决,到底该把哪些城市作为保护对象呢?又由于 ...

随机推荐

  1. 自己实现数据结构系列三---Stack

    一.代码部分 1.定义接口 public interface Stack<E> { int getSize(); boolean isEmpty(); void push(E e); E ...

  2. UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-25: ordinal not in range(128)

    python报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-25: ordinal not in ...

  3. C#实现,C++实现,JS实现 阿拉伯数字金额转换为中文大写金额

    推荐在线编译器  ideone 1. C#实现 :带有负数处理 //把数字金额转换成中文大写数字的函数 //带有负值处理 function changeNumMoneyToChinese(money) ...

  4. Java8 Stream实例--统计出所有含‘张’字的人员的平均年龄

    package com.zhangxueliang.demo; import java.util.ArrayList; import java.util.List; import java.util. ...

  5. SpringBoot 4.SpringBoot 整合 devtools 实现热部署

    一.添加 devtools 依赖 <!-- Spring boot 热部署 : 此热部署会遇到 java.lang.ClassCastException 异常 --> <!-- op ...

  6. AspectJ用注解替换xml配置

    AspectJ基于注解的使用 AspectJ简介 AspectJ是一个基于Java语言的AOP框架,一般 其主要用途:自定义开发 一般情况下spring自动生成代理,要配置aop, 首先确定目标类,a ...

  7. 爬虫实战——Scrapy爬取伯乐在线所有文章

    Scrapy简单介绍及爬取伯乐在线所有文章 一.简说安装相关环境及依赖包 1.安装Python(2或3都行,我这里用的是3) 2.虚拟环境搭建: 依赖包:virtualenv,virtualenvwr ...

  8. 插件 DataTable 创建列表 render参数的详解与如何传递本行数据id

    1.首先 导入DataTable 的插件 2.定义表结构: HTML: <table> <thead> <tr> <th>id</th> & ...

  9. 一、关于a标签伪类中的visited不起作用问题

    一.代码示范 <html> <head> <title>伪类超链接</title> <!--<link href="./test. ...

  10. 洛谷 p1019 单词接龙

    题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...