题目链接:http://poj.org/problem?id=1151

题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积

题目思路:矩形面积并。

代码如下:

#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
const int N = ;
struct Line
{
int l, r, flag;
double h;
Line(){}
Line(int l, int r, int flag, double h):l(l), r(r), flag(flag), h(h) {}
bool operator<(Line &b) const
{
return h<b.h;
}
}; int n;
vector<double> vec;
vector<Line>line; struct node
{
int l, r, flag;
double len;
};
node tree[N << ]; void build(int l, int r, int k)
{
tree[k].l = l, tree[k].r = r, tree[k].len = , tree[k].flag = ;
if(l == r - )
return;
int mid = (l + r) >> ;
build(l, mid, k<<);
build(mid, r, k<<|);
} void updata_up(int k)
{
if(tree[k].flag)
tree[k].len = vec[tree[k].r-] - vec[tree[k].l-];
else if (tree[k].l == tree[k].r - )
tree[k].len = ;
else
tree[k].len = tree[k<<].len + tree[k<<|].len;
} void updata(int l, int r, int k, int x)
{
if(tree[k].l >= l && tree[k].r <= r)
{
tree[k].flag += x;
updata_up(k);
return;
} if(tree[k<<].r > l)
updata(l, r, k<<, x);
if(tree[k<<|].l < r)
updata(l, r, k<<|, x); updata_up(k);
} int main()
{
int cases = ;
while(scanf("%d", &n),n)
{
vec.clear();
line.clear();
double x1[], y1[], x2[], y2[];
for(int i=; i<n; ++ i)
{
scanf("%lf%lf%lf%lf", &x1[i], &y1[i], &x2[i], &y2[i]);
vec.push_back(x1[i]);
vec.push_back(x2[i]);
}
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
for(int i=; i<n; ++ i)
{
int x = lower_bound(vec.begin(), vec.end(), x1[i]) - vec.begin() + ;
int y = lower_bound(vec.begin(), vec.end(), x2[i]) - vec.begin() + ; line.push_back(Line(x, y, , y1[i]));
line.push_back(Line(x, y, -, y2[i]));
}
sort(line.begin(), line.end());
build(, vec.size() + , );
double ans = ;
for(int i=; i<line.size(); ++ i)
{
if(i != )
ans += (line[i].h - line[i-].h)*tree[].len;
updata(line[i].l, line[i].r, , line[i].flag);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n", ++ cases, ans);
}
}

POJ 1151 Atlantis(线段树-扫描线,矩形面积并)的更多相关文章

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

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

  2. hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  3. POJ 1151 - Atlantis 线段树+扫描线..

    离散化: 将所有的x轴坐标存在一个数组里..排序.当进入一条线段时..通过二分的方式确定其左右点对应的离散值... 扫描线..可以看成一根平行于x轴的直线..至y=0开始往上扫..直到扫出最后一条平行 ...

  4. poj 3277 City Horizon (线段树 扫描线 矩形面积并)

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

  5. hdu1542 Atlantis 线段树--扫描线求面积并

    There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...

  6. HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

    版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...

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

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

  8. POJ 1151 Atlantis 线段树+离散化+扫描线

    这次是求矩形面积并 /* Problem: 1151 User: 96655 Memory: 716K Time: 0MS Language: G++ Result: Accepted */ #inc ...

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

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

  10. hdu 1828 Picture(线段树扫描线矩形周长并)

    线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

随机推荐

  1. 从主机访问虚拟机上的Apache

    问题:VMWARE上安装的CentOS6.4,安装Apache,启动后,虚拟机上能访问,主机能ping通,但无法访问Apache. 原因:防火墙设置,配置iptables,开放apache的端口80

  2. 将txt文件转换成EXCEL文件的方法

    地址:http://wenku.baidu.com/view/fcdbe8cca1c7aa00b52acbad.html 1.在EXCEL程序中点击“打开”,将文件类型选择为“文本文件”,找到以前用过 ...

  3. Android基础

    今天学习Android基本环境及基础知识,正确来说是重新温习Android知识,因为初次接触Android开发已经是两年前的事,如今又回到Android开发依然那么熟悉,依然可以让人很兴奋,Andro ...

  4. Loadrunner---解决乱码问题

    在使用Loadrunner录制和回放时有时候会因为乱码问题报错,且让我们很难定位出脚本问题所在.此事我们做一下分析loadrunner为什么会出现乱码这种情况呢? 1.乱码产生的原因 1)loadru ...

  5. VMware中装Win2012并配置Hyper-v

    VMware中装配Win2012再配置Hyper-v,解决虚拟机监控程序已经在运行中的方法 找到虚拟机存放的位置,用记事本打开Windows Server 2012.vmx,在最后添加两行并保存: h ...

  6. 入门React和Webpack

    最近在学习React.js,之前都是直接用最原生的方式去写React代码,发现组织起来特别麻烦,之前听人说用Webpack组织React组件得心应手,就花了点时间学习了一下,收获颇丰 说说React ...

  7. Li-Fi,LED光无线局域网

    无需WiFi信号,点一盏LED灯就能上网.昨天,复旦大学计算机科学技术学院传出好消 息,一种利用屋内可见光传输网络信号的国际前沿通讯技术在实验室成功实现.研究人员将网络信号接入一盏1W的LED灯珠,灯 ...

  8. ueditor

    1:添加插件包 2:添加文件上传的jar包 3:页面引入ueditor插件 <!-- ueditor --><link type="text/css" href= ...

  9. OpenVZ VPS加速方案–Final Speed

    body,td { font-family: 微软雅黑; font-size: 10pt }   OpenVZ VPS加速方案–Final Speed OpenVZ VPS加速方案–Final Spe ...

  10. [转]在WPF中区别TextBlock和Label

    TextBlock和Label都是用来显示少量数据的.好多文章对Label存在的描述都是它允许使用"快速获取"."快速获取"就是允许你用Alt加上其它的按键快速 ...