跳到正文

题解

题解:P2521 [HAOI2011] 防线修建

manboo约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年6月11日
收录
2026年6月13日
更新
2026年11月6日
篇幅
约 7 分钟
获赞
4
题目
P2521

版本

  1. v1当前

设首都坐标为 (sx,sy)(sx,sy)。

正难则反,考虑将操作离线下来后,从后往前进行操作。

那么,所有的删点操作就变成加点操作,也就是:最终的图含有 (0,0),(n,0),(sx,sy)(0,0),(n,0),(sx,sy) 以及其他始终存在的点,从最终的图开始,每次进行加点和查询操作。

查询操作其实就是查询凸包的周长,我们需要维护凸包里的点,现在考虑怎么动态维护凸包。

首先,将 (0,0),(n,0),(sx,sy)(0,0),(n,0),(sx,sy) 以及其他始终存在的点插入凸包是好做的,这是静态凸包,将这些点按横坐标从小到大排序后正常维护即可,详见模版题。

显然,凸包必然有至少三个点。

然后考虑加点操作,设加入的点为 (x,y)(x,y)。

先考虑特殊情况,如果已经维护好的凸包中,含有横坐标为 xx 的点 (x,y′)(x,y'),那么当且仅当 y′<yy'<y 时,(x,y)(x,y) 会更新凸包,否则直接跳过即可。

我们在已经维护好的凸包里找到,横坐标小于 xx 的最右点 AA,横坐标大于 xx 的最左点 BB,这两个点是必然存在的,可以用二分查找。若 A,BA,B 在凸包上直接相连,且 (x,y)(x,y) 在线段 ABAB 上方,那么同样也会更新凸包。

更新凸包就好做了,对于点 AA,不断向左扩展即可,由于都是排好序的,和普通凸包思路一样;对于点 BB 同理向右扩展。更新的同时,删点需要让答案减去其对应的连边,加点需要让答案加上新的边。

一个问题是,如何保证首都必然会处于凸包里呢?

很简单,在一开始的凸包里,强制首都必选。那么,后面每次更新时,必然会保证首都在凸包里了。

此外,由于用 vector 维护凸包信息时,插入点是 O(n)O(n) 的,会超时,所以要用 set 维护。就是 set 不能直接用下标,需要指针,有点麻烦,代码似乎也有点【数据删除】,细节非常多,切掉这道题并不难,只要学过凸包且有查错的耐心。

时间复杂度 O(nlog⁡n)O(n\log n)。

C++
#define double long double
const int N = 100005;
int n, m, q;
double ans;
struct node{
	int x, y;
	bool operator<(const node &other)const{
		return x == other.x ? y < other.y : x < other.x;
	}
	bool operator==(const node &other)const{
		return x == other.x && y == other.y;
	}
	bool operator!=(const node &other)const{
		return x != other.x || y != other.y;
	}
}s, o[N];
bool exist[N];
struct Acquire{
	int op, u;
	double ans;
}acq[N << 1];
map<int, int> mp;
set<node> ch;
set<node>::iterator it1, it2, itl, itll, itr, itrr;
inline double dis(node a, node b){
	return sqrtl(1.0 * (a.x - b.x) * (a.x - b.x) + 1.0 * (a.y - b.y) * (a.y - b.y));
}
inline bool check(node a, node b, node c){
	return 1ll * (c.y - b.y) * (b.x - a.x) >= 1ll * (c.x - b.x) * (b.y - a.y);
}
signed main(){
	n = read(), s.x = read(), s.y = read();
	m = read();
	for(int i = 1; i <= m; i++){
		o[i] = node{read(), read()};
		exist[i] = true;
	}
	q = read();
	// 将操作离线 
	for(int i = 1; i <= q; i++){
		int op = read(), u = 0;
		if(op == 1){
			u = read();
			exist[u] = false;
		}
		acq[i] = Acquire{op, u, 0};
	}
	// 维护最终图,注意每个横坐标只保留最高点即可 
	mp[0] = 0;
	mp[n] = 0;
	mp[s.x] = s.y;
	for(int i = 1; i <= m; i++){
		if(exist[i]){
			mp[ o[i].x ] = max(mp[ o[i].x ], o[i].y);
		}
	}
	for(auto &abc : mp){
		node now = node{abc.first, abc.second};
		if(ch.empty());
		else if(ch.size() == 1){
			ans += dis(*ch.begin(), now);
		}
		else{
			it2 = ch.end(), it1 = ch.end();
			it2 --, it2 --, it1 --;
			while(check(*it2, *it1, now)){
				ans -= dis(*it2, *it1);
				ch.erase(*it1);
				it1 = it2;
				if(*it2 == *ch.begin()) break;
				it2 --;
			}
			ans += dis(*it1, now);
		}
		ch.insert(now);
	}
	// 从后往前加点和查询
	for(int i = q; i; i--){
		if(acq[i].op == 2){
			acq[i].ans = ans;
		}
		else{
			node &now = o[ acq[i].u ];
			itr = ch.upper_bound(now), itl = itr;
			itl --;
			node l = *itl, r = *itr;
			if(r.x == now.x) continue;
			if(l.x == now.x){
				node mid = l;
				l = *(-- itl);
				ans -= dis(l, mid) + dis(mid, r);
				ch.erase(mid);
			}
			else{
				double k = 1.0 * (l.y - r.y) / (l.x - r.x), b = l.y - k * l.x;
				if(k * now.x + b >= now.y) continue;
				ans -= dis(*itl, *itr);
			}
			itrr = itr;
			itrr ++;
			if(itrr != ch.end()){
				while(check(now, *itr, *itrr)){
					ans -= dis(*itr, *itrr);
					ch.erase(*itr);
					itr = itrr;
					itrr ++;
					if(itrr == ch.end()) break;
				}
			}
			ans += dis(*itr, now);
			if(itl != ch.begin()){
				itll = itl;
				itll --;
				while(check(*itll, *itl, now)){
					ans -= dis(*itll, *itl);
					ch.erase(*itl);
					itl = itll;
					if(itll == ch.begin()) break;
					itll --;
				}
			}
			ans += dis(*itl, now);
			ch.insert(now);
		}
	}
	for(int i = 1; i <= q; i++){
		if(acq[i].op == 2){
			printf("%.2Lf\n", acq[i].ans);
		}
	}
	return 0;
}

警示后入:set 在删除一个元素时,其他元素的地址不变!指针仍然会指向原本的值!所以不用单独修改指针!

原文