题目链接: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. C++设计模式-Flyweight享元模式

    Flyweight享元模式 作用:运用共享技术有效地支持大量细粒度的对象. 内部状态intrinsic和外部状态extrinsic: 1)Flyweight模式中,最重要的是将对象分解成intrins ...

  2. Sqlserver 自定义表类型定义,使用,删除

    --创建用户自定义表类型CREATE TYPE dbo.CustomerTable AS TABLE ( Id int NOT NULL, Name char(10) NULL, PRIMARY KE ...

  3. python IDLE编程时遇到Python Error: Inconsistent indentation detected! 解决方法

    仔细检查了几遍代码,发现indent没有错误! 之后试将所有indent都用空格代替,程序就跑起来了. 具体原因可能是IDLE环境内的Tab键有小bug.

  4. 关于android 5.0对开发带来的影响

    由于google推出了android5.0的系统,在app的安装方式,权限的管理方面与之前的系统有较大的区别.自己做的app也有体会.现在记录一些要注意的东西,防止忘记: 1.5.0改变了对自定义权限 ...

  5. 【MongoDB】MongoDB 3.2 SCRAM-SHA-1验证方式

    新版本已取消addUser方法,改使用createUser方法 官方地址:https://docs.mongodb.com/manual/tutorial/create-users/ 官方地址:htt ...

  6. Could not parse mapping document from input stream hibernate配置异常

    十二月 , :: 下午 org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context ...

  7. Java是目前最广泛的_______编程语言

    计算机网络 注意:在配置环境变量时,classpath是为找文件服务的

  8. 特殊js事件

    1:点击enter事件 $(document).keypress(function(e) { // 回车键事件 if(e.which == 13) { submitForm(); } }); 2:JQ ...

  9. 第四章ppt课后作业

    字符串加密解密: 源代码: package tutorial04String; import javax.swing.JOptionPane; public class Wordsecret { pu ...

  10. Theme皮肤文件(json解析、多文件管理)

    一  官方教程 http://developer.egret.com/cn/github/egret-docs/extension/EUI/skin/theme/index.html 二 thm主题文 ...