专题二树形结构 H - Atlantis

  1. 题目
    There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
    Input
    The input consists of several test cases. Each test case starts with a line containing a single integer n (1 <= n <= 100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
    The input file is terminated by a line containing a single 0. Don't process it.
    Output
    For each test case, your program should output one section. The first line of each section must be "Test case #k", where k is the number of the test case (starting with 1). The second one must be "Total explored area: a", where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
    Output a blank line after each test case.
    Sample
    Inputcopy Outputcopy
    2
    10 10 20 20
    15 15 25 25.5
    0
    Test case #1
    Total explored area: 180.00 
  2. 思路
    扫描线模板题,不过不好意思的是我虽然大概还是知道扫描线怎么实现的,真操作还是只会套模板,之后要好好研究
    注意输入输出是浮点数
  3. 代码
    #include<cstdio>
    #include<iostream>
    #include<string>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    #define Clear(a) memset(a,0,sizeof(a))
    
    const long long mod = 1e9+7;
    
    using namespace std;
    
    const long long N = 8e5+100;
    double x1[N], x2[N], z1[N], z2[N], kz[N], kx[N], ans;
    long long n;
    
    struct ss{ double z1, z2, v;long long x;}line[N];
    
    bool cmp_by_x(ss a, ss b){
    	return a.x < b.x;
    }
    
    struct{
    	double l[N], r[N], v[N], s[N], ss[N];
    
    	void up(long long p){
    		if(v[p])
    			s[p] = ss[p];
    		else if(l[p] == r[p])
    			s[p] = 0;
    		else
    			s[p] = s[p << 1] + s[p << 1 | 1];
    	}
    
    	void bu(long long p, long long ll, long long rr){
    		l[p] = ll, r[p] = rr;
    
    		if(ll == rr){
    			ss[p] = kz[ll] - kz[ll - 1];
    			return ;
    		}
    
    		long long m = (ll + rr) >> 1;
    
    		bu(p << 1, ll, m);
    		bu(p << 1 | 1, m + 1, rr);
    
    		ss[p] = ss[p << 1] + ss[p << 1 | 1];
    	}
    
    	void add(long long p, long long ll, long long rr, double x){
    		if(ll <= l[p] && r[p] <= rr){
    			v[p] += x;
    			up(p);
    			return ;
    		}
    
    		long long m = r[p << 1];
    
    		if(ll <= m)
    			add(p << 1, ll, rr, x);
    		if(rr >= m + 1)
    			add(p << 1 | 1, ll, rr, x);
    
    		up(p);
    	}
    }tr;
    
    int main()
    {
    	int q=1;
    	while(scanf("%lld", &n)!=EOF&&n!=0)
    	{
    		ans = 0;
    		for(long long i = 1; i <= n; i ++){
    			scanf("%lf %lf %lf %lf", &x1[i], &z1[i], &x2[i], &z2[i]);
    //			if(x1[i]>x2[i])swap(x1[i],x2[i]);
    //			if(z1[i]>z2[i])swap(z1[i],z2[i]);
    		}
    
    		for(long long i = 1; i <= n; i ++)
    			kx[i] = x1[i], kx[i + n] = x2[i],
    			kz[i] = z1[i], kz[i + n] = z2[i];
    
    		sort(kx + 1, kx + n * 2 + 1);
    		sort(kz + 1, kz + n * 2 + 1);
    
    		for(long long i = 1; i <= n; i ++){
    			x1[i] = lower_bound(kx + 1, kx + n * 2 + 1, x1[i]) - kx;
    			x2[i] = lower_bound(kx + 1, kx + n * 2 + 1, x2[i]) - kx;
    			z1[i] = lower_bound(kz + 1, kz + n * 2 + 1, z1[i]) - kz;
    			z2[i] = lower_bound(kz + 1, kz + n * 2 + 1, z2[i]) - kz;
    		}
    
    		for(long long i = 1; i <= n; i ++){
    			line[i].z1 = line[i + n].z1 = z1[i];
    			line[i].z2 = line[i + n].z2 = z2[i];
    			line[i].x = x1[i], line[i + n].x = x2[i];
    			line[i].v = 1,     line[i + n].v = -1;
    		}
    
    		sort(line + 1, line + n * 2 + 1, cmp_by_x);
    
    		tr.bu(1, 1, n * 2);
    
    		for(long long i = 1; i <= n * 2; i ++){
    			ans += ((kx[line[i].x] - kx[line[i - 1].x]) * tr.s[1]) ;
    			tr.add(1, line[i].z1 + 1, line[i].z2, line[i].v);
    		}
    
    		printf("Test case #%d\n", q);
    		q++;
    		printf("Total explored area: %.2lf\n\n", ans);
    	}
    	return 0;
    }
posted @ 2022-02-06 23:26  Benincasa  阅读(31)  评论(0编辑  收藏  举报