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. 算法:POJ1008 Maya Calendar

    此题非常水,不做说明. package practice; import java.io.BufferedInputStream; import java.util.Scanner; /** * @a ...

  2. 【读书笔记】-- JavaScript数组

    数组是一段线性分配的内存,它通过整数计算偏移并访问其中的元素.大多数的语言都会要求一个数组的元素是相同类型,但JavaScript数组可以包含任意类型. var misc = ['string', n ...

  3. angularjs移除不必要的$watch

    在我们的web page,特别是移动设备上,太多的angular $watch将可能导致性能问题.这篇文章将解释如何去移除额外的$watch提高你的应用程序性能. $watch如果不再使用,我们最好将 ...

  4. 关于Xcode5的离线帮助

    关于Xcode的离线帮助文档,网上找到的许多都是Xcode4的资料,Xcode5貌似将文档搬到了Help菜单里,而不是原先的<Window> - <Organizer> - & ...

  5. 有shi以来最详细的正则表达式入门教程

    本篇文章文字内容较多,但是要学习正则就必须耐心读下去,正则表达式是正则表达式其实并没有想像中的那么困难,但是想要熟练的掌握它,还是需要下功夫勤加练习的.这里讲一些正则表达式的语法和学习方法,大家还要多 ...

  6. java提高篇(四)-----理解java的三大特性之多态

    面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 继承 ...

  7. 爱上MVC系列~前端验证与后端数据有效性验证

    回到目录 有一句话,在10年前就是真理,到现在也一直都是,“前端验证可以没有,但后端验证必须要有”,这句话相信大家都没有意见吧,前端验证一般指通过JS方式实现的,友好的,个性的验证方式,而后端验证是指 ...

  8. [Spring框架] Spring中的 ContextLoaderListener 实现原理.

    前言: 这是关于Spring的第三篇文章, 打算后续还会写入AOP 和Spring 事务管理相关的文章, 这么好的两个周末 都在看code了, 确实是有所收获, 现在就来记录一下. 在上一篇讲解Spr ...

  9. fir.im Weekly - 600个 Android 开源项目汇总

    本期 Weekly 收集了一些热度资源,包含 Android.iOS 开发工具与源码分享,程序员也应该了解的产品运营.设计等 Tips ,希望对你有帮助. 600个Android开源项目汇总 勤劳的 ...

  10. 谈谈主函数main

    我们来看一下主函数 public class HelloWorld{ public static void main(String[] args){ System.out.println(" ...