题目描述
近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了。可是A国上层现在还犹豫不决,到底该把哪些城市作为保护对象呢?又由于A国的经费有限,所以希望你能帮忙完成如下的一个任务:
给出你所有的A国城市坐标
A国上层经过讨论,考虑到经济问题,决定取消对i城市的保护,也就是说i城市不需要在防线内了
A国上层询问对于剩下要保护的城市,修建防线的总经费最少是多少
你需要对每次询问作出回答。注意单位1长度的防线花费为1。
A国的地形是这样的,形如下图,x轴是一条河流,相当于一条天然防线,不需要你再修建
A国总是有两个城市在河边,一个点是(0,0),一个点是(n,0),其余所有点的横坐标均大于0小于n,纵坐标均大于0。A国有一个不在(0,0)和(n,0)的首都。(0,0),(n,0)和首都这三个城市是一定需要保护的。
输入输出格式
输入格式:
第一行,三个整数n,x,y分别表示河边城市和首都是(0,0),(n,0),(x,y)。
第二行,一个整数m。
接下来m行,每行两个整数a,b表示A国的一个非首都非河边城市的坐标为(a,b)。
再接下来一个整数q,表示修改和询问总数。
接下来q行每行要么形如1 i,要么形如2,分别表示撤销第i个城市的保护和询问。
输出格式:
对于每个询问输出1行,一个实数v,表示修建防线的花费,保留两位小数
输入输出样例
输入样例
4 2 1
2
1 2
3 2
5
2
1 1
2
1 2
2
解题分析:
可以很容易地发现此题只需要维护一个半凸包, 而其他城市的横坐标均大于0小于n这一条件给我们查找坐标提供了很大的方便。但是问题在于如果我们正向每次删点进行凸包维护, 代码将会变得十分冗长难写, 复杂度也难以保证, 所以我们反向思考, 将修改、查询操作离线下来, 先将所有将被删除的点删掉, 再求凸包, 然后从后向前逐个加点、更新。在这里博主使用set维护凸包上的点,用lower_bound查找插入点两边的点。
代码如下:
#include <cstdio>
#include <cctype>
#include <cstring>
#include <iostream>
#include <cmath>
#include <set>
#include <algorithm>
#include <cstdlib>
#define R register
#define gc getchar()
#define W while
#define IN inline
#define db double
#define MX 100005
#define EPS 1e-8
using namespace std;
struct pt
{
db x, y;
};
IN pt operator + (const pt &x, const pt &y){return (pt){x.x + y.x, x.y + y.y};}
IN pt operator - (const pt &x, const pt &y){return (pt){x.x - y.x, x.y - y.y};}
IN bool operator < (const pt &x, const pt &y){return x.x == y.x ? x.y < y.y : x.x < y.x;}
IN db operator * (const pt &x, const pt &y){return x.x * y.y - x.y * y.x;}
IN db dist (const pt &x){return sqrtl(x.x * x.x + x.y * x.y);}
set <pt> convex;
set <pt> ::iterator now, nex, pre, halt;
pt data[MX], deal[MX], con[MX];
int top, dot, query, n, x, y, tot, reply;
db ans, result[MX];
bool out[MX];
void get_con()
{
sort(deal, deal + dot);
for (R int i = 0; i < dot; ++i)
{
W (top > 1 && (con[top - 1] - con[top - 2]) * (deal[i] - con[top - 2]) < 0) top--;
con[top++] = deal[i];
}
int limit = top;
for (R int i = dot - 2; i >= 0; --i)
{
W (top > limit && (con[top - 1] - con[top - 2]) * (deal[i] - con[top - 2]) < 0) top--;
con[top++] = deal[i];
}
if(dot > 1) top--;
for (R int i = top; i > 1; --i)
ans += dist(con[i] - con[i - 1]);
}
struct Command
{
bool typ;
int num;
}command[MX];
IN void oout()//中序输出方便调试
{
set <pt> :: iterator itt;
for (itt = convex.begin(); itt != convex.end(); itt++)
{
printf("%.2lf %.2lf\n", itt->x, itt->y);
}
}
int main()
{
int a;
scanf("%d%d%d", &n, &x, &y);
scanf("%d", &dot);
for (R int i = 0; i < dot; ++i) scanf("%lf%lf", &data[i].x, &data[i].y);
data[dot++] = (pt){0, 0};
data[dot++] = (pt){n, 0};
data[dot++] = (pt){x, y};
tot = dot;
scanf("%d", &query);
for (R int i = 1; i <= query; ++i)
{
scanf("%d", &a);
if(a == 1)
{
scanf("%d", &a);
out[--a] = true;
command[i].typ = false;
command[i].num = a;
dot--;
continue;
}
command[i].typ = true, reply++;
}
int cnt = 0, maxreply = reply;
for (R int i = 0; i < tot; ++i)
{
if(!out[i]) deal[cnt++] = data[i];
}//重新记录需要计入凸包的点
get_con();
for (R int i = 0; i <= top; ++i)
{convex.insert(con[i]);}
for (R int i = query; i; --i)
{
if(command[i].typ)//查询则直接输出
{
result[reply--] = ans;
continue;
}
else
{
now = convex.lower_bound(data[command[i].num]);
if(now -> x > data[command[i].num].x) now--;
//确保now指向的点在查找点的左边
nex = now;
nex++;
if ((*nex - *now) * (data[command[i].num] - *now) <= 0) continue;
ans -= dist(*nex - *now);
halt = now;
now ++;
W (233)
{
nex = now, nex++;
if (nex == convex.end())
{
ans += dist((pt){n, 0} - data[command[i].num]);
break;//到达边界
}
if ((*now - *nex) * (data[command[i].num] - *nex) <= 0)
{
ans -= dist(*now - *nex);//原凸包上的点被覆盖
convex.erase(now);
now = nex;
continue;
}
else
{
ans += dist(*now - data[command[i].num]);
break;
}
}
now = halt;
if(now->x > data[command[i].num].x) now--;
W (666)
{
if (now == convex.begin())
{
ans += dist((pt){0, 0} - data[command[i].num]);
break;
}
pre = now, pre--;
if((*now - *pre) * (data[command[i].num] - *pre) > 0)
{
ans -= dist(*now - *pre);
convex.erase(now);
now = pre;
continue;
}
else
{
ans += dist(*now - data[command[i].num]);
break;
}
}
convex.insert(data[command[i].num]);
}
}
for (R int i = 1; i <= maxreply; ++i)
printf("%.2lf\n", result[i]);
return 0;
}
接下来分析坑点:
- lower_bound返回的是第一个以升序可以插入的地方, 即并不是返回第一个小于查找值的地方, 而是返回第一个大于查找值的地方。
- set的 convex.end()并没有保存任何数值, 但convex.begin()保存的是第一个值, 判断时应注意。