zoukankan      html  css  js  c++  java
  • BZOJ2300 [HAOI2011] 防线修建

    @(XSY)[凸包]

    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以内, 数据保证没有重点

    Solution

    對於一些只有刪除操作的問題, 考慮離線倒序解決.
    比如說這一題, 在凸包上刪點非常困難, 但是可以把問題倒序來做, 變為在凸包上加點.
    用一個數據結構來維護凸包上的點(這題用的是set), 每次插入一個點時, 通過查詢以及刪除其左右兩邊的點, 維護凸包.

    #include<cstdio>
    #include<cctype>
    #include<set>
    #include<cstring>
    #include<cmath>
    using namespace std;
    
    inline int read()
    {
    	int x = 0, flag = 1;
    	char c;
    	while(! isdigit(c = getchar()))
    		if(c == '-')
    			flag *= - 1;
    	while(isdigit(c))
    		x = x * 10 + c - '0', c = getchar();
    	return x * flag;
    }
    
    const int M = 1 << 17, Q = 1 << 18;
    
    struct status
    {
    	double x, y;
    	
    	inline status(double _x = 0, double _y = 0)
    	{
    		x = _x, y = _y;
    	}
    	
    	inline friend int operator <(status a, status b)
    	{
    		return a.x == b.x ? a.y < b.y : a.x < b.x;
    	}
    	
    	inline friend status operator -(status a, status b)
    	{
    		return status(a.x - b.x, a.y - b.y);
    	}
    	
    	inline friend double operator *(status a, status b)
    	{
    		return a.x * b.y - a.y * b.x;
    	}
    }a[M];
    
    set<status> s;
    
    int tag[M];
    int opt[Q];
    
    double ans;
    
    inline double getDistance(status a, status b)
    {
    	return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    }
    
    
    inline void addVertex(status x)
    {
    	s.insert(x);
    	set<status>::iterator L = s.find(x), R = s.find(x), p;
    	L --, R ++;
    	s.erase(x);
    	
    	if((*R - *L) * (x - *L) < 0)
    		return;
    	
    	ans -= getDistance(*L, *R);
    	
    	for(; ; )
    	{
    		p = R, R ++;
    		
    		if(R == s.end())
    			break;
    		
    		if((*R - x) * (*p - x) > 0)
    			break;
    			
    		ans -= getDistance(*p, *R);
    		s.erase(p);
    	}
    	
    	for(; ; )
    	{
    		if(L == s.begin())
    			break;
    			
    		p = L, L --;
    		
    		if((x - *p) * (*p - *L) > 0)
    			break;
    			
    		ans -= getDistance(*p, *L);
    		s.erase(p);
    	}
    	
    	s.insert(x);
    	L = R = s.find(x);
    	L --, R ++;
    	ans += getDistance(*L, x) + getDistance(x, *R);
    }
    
    double out[Q];
    
    int main()
    {
    	#ifndef ONLINE_JUDGE
    	freopen("BZOJ2300.in", "r", stdin);
    	freopen("BZOJ2300.out", "w", stdout);
    	#endif
    	
    	int n = read(), x = read(), y = read();
    	int m = read();	
    	
    	for(int i = 0; i < m; i ++)
    		a[i].x = read(), a[i].y = read();
    	
    	int q = read();
    	memset(tag, 0, sizeof(tag));
    	
    	for(int i = 0; i < q; i ++)
    	{
    		int flag = read() - 1;
    		opt[i] = flag ? - 1 : read() - 1;
    		
    		if(~ opt[i])
    			tag[opt[i]] = 1;
    	}
    	
    	ans = getDistance(status(0, 0), status(x, y)) + getDistance(status(x, y), status(n, 0));
    	s.insert(status(0, 0)), s.insert(status(n, 0)), s.insert(status(x, y));
    	
    	for(int i = 1; i < m; i ++)
    		if(! tag[i])
    			addVertex(a[i]);
    			
    	for(int i = q - 1; ~ i; i --)
    	{
    		if(~ opt[i])
    			addVertex(a[opt[i]]);
    		else
    			out[i] = ans;
    	}
    	
    	for(int i = 0; i < q; i ++)
    		if(opt[i] == - 1)
    			printf("%.2lf
    ", out[i]);
    }
    
  • 相关阅读:
    Servlet的数据库访问
    Servlet 网页重定向
    Intellij idea创建javaWeb以及Servlet简单实现
    Tomcat
    QQ简易版
    单例
    centos7 jdk安装
    centos7 allure安装
    centos中执行apt-get命令提示apt-get command not found
    centos mysql使用踩过的坑
  • 原文地址:https://www.cnblogs.com/ZeonfaiHo/p/6431259.html
Copyright © 2011-2022 走看看