• AcWing 247 亚特兰蒂斯 (扫描线)


    https://www.acwing.com/problem/content/description/249/

    线段树经典题

    首先因为坐标可能是小数,所以要先把坐标离散化

    将每个矩形拆成左右两条线段,将每条线段按横坐标排序
    线段树每个节点维护两个信息:(cnt)(len)
    其中 (cnt) 表示该节点代表的线段(注意只是该节点所代表的([l,r])这条线段,而不是所有子节点)
    (len) 表示这条线段上被覆盖的总长度((len) 是包括所有子节点的长度)

    更新的时候,如果该节点的 (cnt) 大于 (0), 就令(len)等于该线段代表的长度
    否则,(len)等于子树的(len)之和

    因为修改操作不会对线段树上该节点的子节点产生影响,所以不需要使用懒标记

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    #include<stack>
    #include<queue>
    using namespace std;
    typedef long long ll;
    
    const int maxn = 20010;
    
    int n, cnt, tot, Case;
    int val[maxn];
    double ans;
    double x1[maxn], y3[maxn], x2[maxn], y2[maxn], Y[maxn];
    
    struct Node{
    	double x;
    	int y1, y2, v;
    	
    	bool operator < (const Node &a) const { // 结构体内重载小于号 
    		return x < a.x;
    	}
    }s[maxn << 1];
    
    struct SEG{
    	int cnt;
    	double len;
    }t[maxn << 4];
    
    void pushup(int i, int l, int r){
    	if(t[i].cnt){
    		t[i].len = Y[r + 1] - Y[l];
    	}else {
    		t[i].len = t[i << 1].len + t[i << 1 | 1].len;
    	}
    }
    
    void modify(int i, int k, int l, int r, int x, int y){
    	if(x <= l && r <= y){
    		t[i].cnt += k;
    		if(t[i].cnt) t[i].len =  Y[r + 1] - Y[l];
    		else t[i].len = t[i << 1].len + t[i << 1 | 1].len;
    		return;
    	}
    	
    	int mid = (l + r) >> 1;
    	if(x <= mid) modify(i << 1, k, l, mid, x, y);
    	if(y > mid) modify(i << 1 | 1, k, mid + 1, r, x, y);
    	pushup(i, l, r);
    }
    
    void init(){
    	cnt = 0; tot = 0; ans = 0; ++Case;
    	for(int i = 1; i <= n; ++i){
    		scanf("%lf%lf%lf%lf", &x1[i], &y3[i], &x2[i], &y2[i]);
    		Y[++cnt] = y3[i], Y[++cnt] = y2[i];
    	}
    	sort(Y + 1, Y + 1 + cnt);
    	cnt = unique(Y + 1, Y + 1 + cnt) - Y - 1;
    		
    	for(int i = 1; i <= n; ++i){
    		s[++tot].x = x1[i];
    		s[tot].y1 = lower_bound(Y + 1, Y + 1 + cnt , y3[i]) - Y;
    		s[tot].y2 = lower_bound(Y + 1, Y + 1 + cnt , y2[i]) - Y;
    		s[tot].v = 1;
    		
    		s[++tot].x = x2[i];
    		s[tot].y1 = lower_bound(Y + 1, Y + 1 + cnt , y3[i]) - Y;
    		s[tot].y2 = lower_bound(Y + 1, Y + 1 + cnt , y2[i]) - Y;
    		s[tot].v = -1;
    	}
    	
    	sort(s + 1, s + 1 + tot);
    }
    
    void solve(){
    	for(int i = 1; i <= tot; ++i){
    		ans += t[1].len * (s[i].x - s[i-1].x);
    //		printf("%d %d %lf
    ",s[i].y1, s[i].y2 - 1, t[1].len);
    		modify(1, s[i].v, 1, cnt, s[i].y1, s[i].y2 - 1);
    	}
    	printf("Test case #%d
    ", Case);
    	printf("Total explored area: %.2lf
    ",ans);
    	printf("
    "); 
    }
    
    ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }
    
    int main(){
    	Case = 0;
    	while(scanf("%d", &n) && n){
    		init();
    		solve();
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    Java 基础知识(五)
    Java 基础知识(四)
    Java 基础知识(三)
    Java 基础知识(二)
    python 默认参数潜在的问题
    python中统计计数的几种方法和Counter的介绍
    简单的总结一下到底什么是python
    python中的__solots__方法
    python中生成器和迭代器以及可迭代对象的区别
    游览器访问一个网址的全过程
  • 原文地址:https://www.cnblogs.com/tuchen/p/13958593.html
Copyright © 2020-2023  润新知