Area Coverage
Time Limit: 10000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 16, Accepted users: 12
Problem 12884 : No special judgement
Problem description

In this day and age, a lot of the spying on other countries is done with the use of satellites and drones equipped with cameras. All these photographs of various sizes and from various sources can be combined to give a picture of the country as a whole.
Given the photographs (that is to say, the rectangular
area covered by each, since the contents of the photographs themselves are of
course top-secret!), can you work out what the total area is of all that is
photographed? Note that certain areas can appear on multiple photographs and
should be counted only once.

Input

On the first line one positive number: the number of test cases, at most 100.
After that per test case:
one line with an integer n (1<=n<=1000):
the number of photographs.
n lines with four space-separated integers x1,
y1, x2 and y2 (0<=x1; y1; x2; y2<=1 000 000, x1 < x2 and y1 < y2):
the coordinates of the southwest and northeast corner, respectively, of each
photograph. The photographs are all rectangular in shape with their other
corners at (x1; y2) and (x2; y1).
The coordinates correspond to a flat
two-dimensional space (i.e. we assume the Earth to be
flat).

Output

Per test case:
one line with an integer: the total area of all that
appears on the photographs.

Sample Input
2
3
0 6 20 16
14 0 24 10
50 50 60 60
2
0 0 20 10
10 4 14 8
Sample Output
376
200
Problem Source
BAPC preliminary 2013

Mean:

在二维平面中,给你一些矩形的左下坐标(x1,y1)和右上坐标(x2,y2),让你求这些矩形面积的并。

analyse:

我们在y轴方向上维护一棵线段树。该线段树的模型是区间覆盖,即应该对像某个区间有没有被覆盖这样的查询,以及添加覆盖和删除覆盖这样的操作---也就是将矩形的左右两边界看作对y轴的覆盖来处理。我们将所有矩形的左右边界按照x坐标升序排序。每个矩形的左边界执行对y轴的覆盖操作,右边界执行对x轴的删除覆盖操作。

每次插入一条线段的时候,我们判断cover值(覆盖的次数),如果>0那么就算面积。

如图:

初始时每条线段的cover都为0;

线段1插入后,所对应的区间cover变为1,当第二条线段插入时,我们先判断一下该区间上的cover值,发现有一段cover是大于0的,所以就将对应的面积(蓝色部分)加入ans中,此时线段2下半部分的cover值变为2,上部分的cover值变为1,还要把线段1的上部分的x坐标更新为线段2的x值;

当第三条直线插入时,同样的道理,加入的是黄色部分的面积;第四条进入时,加入的是紫色部分的面积。

这样,我们只需要在插入前先计算面积,当所有线段插入结束,answer也就出来了。

所以我们的线段树只需要两个函数:build和insert函数。

Time complexity:O(n*logn)

Source code:

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-23-20.40
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define  LL long long
#define  ULL unsigned long long
using namespace std;

const int N = ;
double y[ * N];
struct LINE
{
     double x, y_down, y_up;
     int flag;//左线段or右线段
};
LINE line[N << ];
struct Tree
{
     int x;
     int cover;        // 覆盖次数
     bool flag;         // 是否为叶子节点
     int y_up, y_down;
};
Tree tree[( << ) * ];
bool cmp( LINE a, LINE b )
{
     return a.x < b.x;
}
void build( int l, int r, int x )
{
     tree[x].x = -; //-1表示该区间没有线段
     tree[x].cover = ;
     tree[x].flag = false;
     tree[x].y_up = y[r];
     tree[x].y_down = y[l];
     if( l + == r ) // 叶子结点: (1,2) (2,3) (3,4) (4,5)....
     {
           tree[x].flag = true;
           return;
     }
     int tmp = x << ;
     int mid = ( l + r ) >> ;
     build( l, mid, tmp );
     build( mid, r, tmp + );;
}
double insert( int i, double x, double l, double r, int flag )
{
     if( r <= tree[i].y_down || l >= tree[i].y_up ) //要插入的线段不在该区间
           return ;
     if( tree[i].flag ) //叶子节点
     {
           if( tree[i].cover > )     // 需要求并面积
           {
                 double temp_x = tree[i].x;
                 double ans = ( x - temp_x ) * ( tree[i].y_up - tree[i].y_down ); // 宽*高
                 tree[i].x = x; // 更新树中的x值
                 tree[i].cover += flag;
                 return ans;
           }
           else
           {
                 tree[i].cover += flag;
                 tree[i].x = x;
                 return ;
           }
     }
     double ans1, ans2;
     int tmp = i << ;
     ans1 = insert( tmp, x, l, r, flag );
     ans2 = insert( tmp + , x, l, r, flag );
     return ans1 + ans2;
}

int main()
{
     ios_base::sync_with_stdio( false );
     cin.tie( );
     int T;
     cin >> T;
     double x1, y1, x2, y2;
     while( T-- )
     {
           int n;
           cin >> n;
           int index = ;
           for( int i = ; i <= n; i++ )
           {
                 scanf( "%lf %lf %lf %lf", &x1, &y1, &x2, &y2 );
                 y[index] = y1;
                 line[index].x = x1;
                 line[index].y_down = y1;
                 line[index].y_up = y2;
                 line[index].flag = ;
                 index++;
                 y[index] = y2;
                 line[index].x = x2;
                 line[index].y_down = y1;
                 line[index].y_up = y2;
                 line[index].flag = -;
                 index++;
           }
           index--;
           double ans = 0.0;
           sort( line + , line + + index, cmp );
           sort( y + , y + + index );
           build( , index, );
           for( int i = ; i <= index; i++ )
           {
                 ans += insert( , line[i].x, line[i].y_down, line[i].y_up, line[i].flag );
           }
           printf( "%.0lf\n", ans );
     }
     return ;
}

扫描线 + 线段树 : 求矩形面积的并 ---- hnu : 12884 Area Coverage的更多相关文章

  1. POJ 1151Atlantis 扫描线+线段树求矩形面积并

    题目链接 #include <iostream> #include <vector> #include <cstdio> #include <cstring& ...

  2. HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)

    链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...

  3. 【hdu1542】线段树求矩形面积并

    分割线内容转载自http://hzwer.com/879.html ------------------------------------------------------------------ ...

  4. POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并

    题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...

  5. POJ 1151 Atlantis 线段树求矩形面积并 方法详解

    第一次做线段树扫描法的题,网搜各种讲解,发现大多数都讲得太过简洁,不是太容易理解.所以自己打算写一个详细的.看完必会o(∩_∩)o 顾名思义,扫描法就是用一根想象中的线扫过所有矩形,在写代码的过程中, ...

  6. 【hdu1255】线段树求矩形面积交

    题意大概就是上图这个样子.<=100组测试数据,每组<=1000个矩形. 题解: 这个问题怎么解决..做了上一题矩形面积并应该就会了.. 对于每个节点维护3个值: cnt:该节点所代表的这 ...

  7. HDU 1255 覆盖的面积 (扫描线 线段树 离散化 矩形面积并)

    题目链接 题意:中文题意. 分析:纯手敲,与上一道题目很相似,但是刚开始我以为只是把cnt>=0改成cnt>=2就行了,. 但是后来发现当当前加入的线段的范围之前 还有线段的时候就不行了, ...

  8. UVA 11983 Weird Advertisement --线段树求矩形问题

    题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...

  9. HDU 1542.Atlantis-线段树求矩形面积并(离散化、扫描线/线段树)-贴模板

    好久没写过博客了,这学期不是很有热情去写博客,写过的题也懒得写题解.现在来水一水博客,写一下若干年前的题目的题解. Atlantis Time Limit: 2000/1000 MS (Java/Ot ...

随机推荐

  1. 软件测试基本理论-IBM模式

    软件测试基本理论(1) IBM生产模式 1   参考书目 <IBM-从菜鸟到测试架构师-一个测试工程师的成长日记> 出版社:电子工业出版社 印次:2013年6月 作者:IBM主要工程师 2 ...

  2. Wix 安装部署教程(十六) -- 自动生成多语言文件

    因为持续集成需要,所有项目编译完之后生成一个多语言的安装包.之前生成mst文件都是手动操作,而且mst文件必须每次重新和新的安装包“关联”,否则中文的安装包去调用英文的资源的时候就会报类似于“类型转换 ...

  3. IOS Animation-动画基础、深入

    1. Model Layer Tree(模型层树)和Presentation Layer Tree(表示层树) CALayer是动画产生的地方.当我们动画添加到Layer时,是不直接修改layer的属 ...

  4. js笔记——js异常处理

    异常捕获 try...catch结构: try { //需要捕获的代码块 } catch (e) { console.log(e.name + ": " + e.message); ...

  5. Atitit. Api 设计 原则 ---归一化

    Atitit. Api 设计 原则 ---归一化 1.1. 叫做归一化1 1.2. 归一化的实例:一切对象都可以序列化/toString  通过接口实现1 1.3. 泛文件概念.2 1.4. 游戏行业 ...

  6. Gitlab备份、升级、恢复

    一.备份 1.使用Omnibus安装包安装 --gitlab-rake gitlab:backup:create 2.使用源码安装 --./use_gitlab----如果备份失败,PATH路径错误, ...

  7. 批处理集锦——(4)2>nul和1>nul是什么意思?

    >nul 是屏蔽操作成功显示的信息,但是出错还是会显示(即1>nul) 2>nul 是屏蔽操作失败显示的信息,如果成功依旧显示. >nul 2>nul 就是正确的错误的一 ...

  8. js 编码、解码与asp.net 编码、解码

    js对URL提供:escape,encodeURI,encodeURIComponent 的编码方法encodeURIComponent:推荐使用,它是将中文.韩文等特殊字符转换成utf-8格式的ur ...

  9. Oracle 11g系列:SQL Plus与PL/SQL

    SQL Plus是Oracle提供的一种用户接口,是数据库与用户之间进行交互的工具. PL/SQL是Oracle的过程化编程语言. 1.登录SQL Plus 利用Windows的[开始]|[运行]命令 ...

  10. 使用Depth Texture

    使用Depth Textures: 可以将depth信息渲染到一张texture,有些效果的制作会需要scene depth信息,此时depth texture就可以派上用场了. Depth Text ...