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. 分享一个异步任务在遇到IO异常时支持递归回调的辅助方法

    public void TryAsyncActionRecursively<TAsyncResult>( string asyncActionName, Func<Task<T ...

  2. 基于正则的INI读写工具类,支持加密解密

    看到这个标题,有人会问,现在都用xml做配置文件了,谁还用INI文件啊!下面来简单对比一下xml和ini: 1.XML功能强大表达能力强,同时扩展性好. 2.它的主要优势是异构平台的整合.通讯. 3. ...

  3. SQL Server 性能优化之——T-SQL TVF和标量函数

    阅读导航 1. TVF(表-值行数Table-Valued Functions)         a. 创建TVF         b. 使用TVF的低性能T-SQL         c. 使用临时表 ...

  4. 为 Exchange 服务器编写自定义的反垃圾插件

    Exchange 2010 的 Edge Transport 包含了一些 Anti-spam 的 Feature,如图: 都开启了,但是呢,还是会有漏网之鱼,而且把这些邮件自己列为 Junk 也起不了 ...

  5. C++函数指针

    然后,我在想C++函数指针到底有啥用呢.想了又想,我想出来一种,就是将函数地址(函数n)作为函数(函数1)的参数时,便可以在访问函数1的时候就决定了,在函数1里面调用的是哪一个函数(函数1,2,3,4 ...

  6. java Decompiler的bug

    java Decompiler 有一个明显的bug是, 之前的打开jar文件,会被缓存起来,如果再次打开同名jar文件(jar内容替换~!),会导致仍然显示原来内容! . 必须关闭Decompiler ...

  7. 60,000毫秒内对Linux的性能诊断效的方法

    转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0225/168.html?1456484140 60,000 毫秒内对 Linux 的性能诊断 当你 ...

  8. EF架构~豁出去了,为了IOC,为了扩展,改变以前的IRepository接口

    回到目录 使用了4年的IRepository数据仓储接口,今天要改变了,对于这个数据仓储操作接口,它提倡的是简洁,单纯,就是对数据上下文的操作,而直正的数据上下文本身我们却把它忽略了,在我的IRepo ...

  9. salesforce 零基础开发入门学习(十一)sObject及Schema深入

    sObject在salesforce中占有举足轻重的位置,除了在数据库中数据以外,我们还应该关心一下他的元信息.元信息封装在Schema命名空间内. 作为面向对象语言,我们可以畅想一下如果我们是设计人 ...

  10. 事件异步(EAP)使用事件异步处理一些耗时操作

    比如需要下载一些比较大的文件,如果使用会UI卡顿,使用异步可以节省一些时间 下面是一些例子: using System; using System.Collections.Generic; using ...