题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255

求矩形面积的交的线段树题目,刚做了求并的题目,再做这个刚觉良好啊,只要再加一个表示覆盖次数大于1次的长度变量即可

代码:

 #include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
class node
{
public:
int l,r;
int c;
double cnt;
double more ;//表示被覆盖次数大于一次的长度
double lf,rf;
};
node segTree[maxn*];
class line
{
public:
double x,y1,y2;
int f;
};
line le[maxn*];
bool cmp(line a, line b)
{
return a.x< b.x;
}
double y[maxn*]; void build(int num,int l, int r)
{
segTree[num].l=l;
segTree[num].r=r;
segTree[num].cnt=;
segTree[num].more==;
segTree[num].lf=y[l];
segTree[num].rf=y[r];
if(l+==r) return;
int mid=(l+r)/;
build(num*,l,mid);
build(num*+,mid,r); } void calen(int num)
{
if(segTree[num].c >=)
{
segTree[num].more=segTree[num].cnt=segTree[num].rf-segTree[num].lf;
return ;
}
else if(segTree[num].c==)
{
segTree[num].cnt=segTree[num].rf-segTree[num].lf;
if(segTree[num].l+ ==segTree[num].r) segTree[num].more=;
else segTree[num].more=segTree[num*].cnt+segTree[num*+].cnt;
}
else
{
if(segTree[num].l+ ==segTree[num].r )
{
segTree[num].cnt=segTree[num].more=;
}
else
{
segTree[num].cnt=segTree[num*].cnt+segTree[num*+].cnt;
segTree[num].more=segTree[num*].more+segTree[num*+].more;
}
}
}
void update(int num, line e)
{
if(e.y1 == segTree[num].lf && e.y2==segTree[num].rf)
{
segTree[num].c+=e.f;
calen(num);
return ;
}
if(e.y2 <= segTree[num*].rf) update(num*,e);
else if(e.y1 >= segTree[num*+].lf) update(num*+,e);
else
{
line temp=e;
temp.y2=segTree[num*].rf;
update(num*,temp);
temp=e;
temp.y1=segTree[num*+].lf;
update(num*+,temp);
}
calen(num);
}
int main()
{
int t;
int n;
double x1,x2,y1,y2; scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int num=;
for(int i=;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
le[num].x=x1;
le[num].y1=y1;
le[num].y2=y2;
le[num].f=;
y[num]=y1;
num++;
le[num].x=x2;
le[num].y1=y1;
le[num].y2=y2;
le[num].f=-;
y[num]=y2;
num++;
}
sort(le+,le+num,cmp);
sort(y+,y+num);
build(,,num-);
update(,le[]);
double ans=;
for(int i=;i<num;i++)
{
ans+=segTree[].more*(le[i].x - le[i-].x);
update(,le[i]);
}
printf("%.2lf\n",ans);
}
return ; }

hdu1255 覆盖的面积 线段树+里离散化求矩形面积的交的更多相关文章

  1. POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)

    题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...

  2. HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/HDU-1255 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<= ...

  3. HDU 1255 覆盖的面积 (线段树+扫描线+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题意很清楚,就是让你求矩阵之间叠加层数大于1的矩形块的面积和. 因为n只有1000,所以我离散化 ...

  4. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

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

  5. 覆盖的面积 HDU - 1255 线段树+扫描线+离散化 求特定交叉面积

    #include<cstdio> #include<map> #include<algorithm> using namespace std; ; struct N ...

  6. HDU 1255 覆盖的面积(线段树:扫描线求面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题目大意:给你若干个矩形,让你求这些矩形重叠两次及以上的部分的面积. 解题思路:模板题,跟HDU ...

  7. poj1151 Atlanis 线段树+离散化求矩形面积的并

    题目链接:http://poj.org/problem?id=1151 很经典的题目,网上有很多模板代码,自己理解了一天,然后很容易就敲出来了... 代码: #include<iostream& ...

  8. POJ 1151 Atlantis(经典的线段树扫描线,求矩阵面积并)

    求矩阵的面积并 采用的是区间更新 #include <iostream> #include <stdio.h> #include <string.h> #inclu ...

  9. POJ 1151 线段树+扫描线(计算矩形面积并)

    前一篇博客有了讲解就不再叙述了 #include<cstdio> #include<cstring> #include<cmath> #include<ios ...

随机推荐

  1. SSH相关小应用

    1.隐藏值:<s:hidden name="bbsTopic.id" value="%{bbsTopic.id}"></s:hidden> ...

  2. iOS开发之自定义弹出的键盘

    self.inputField.inputView = myView 按文本框弹出的键盘不再是普通文字输入键盘,而是我们设置的myView.一般把这个方法写在viewDiLoad方法中. 也可以在键盘 ...

  3. iOS开发之pch文件

    项目的Supporting files文件夹下面有个“工程名-Prefix.pch”文件,也是一个头文件 pch头文件的内容能被项目中的其他所有源文件共享和访问 一般在pch文件中定义一些全局的宏 在 ...

  4. Dive in python Chapter4 实例

    def info(object,spacing=10,collapse=1): """Print methods and doc strings. Takes modul ...

  5. [python爬虫]爬取学校教务处成绩

    学校教务处网站 登陆窗口 表单数据 观察登陆窗口和提交的表单数据可知只要将账号.密码.验证码正确赋值提交即可模拟登陆. 账号和密码都有,问题的关键就在验证码上. 右键验证码图片审查观察源码如下图: 刚 ...

  6. opencv配置(win10+VS2015+opencv3.1)

    Step 1:准备工作 a.win10 b.vs2015 c.opencv3.1[从http://opencv.org/downloads.html下载] Step 2.开始安装 a. 双击openc ...

  7. Python之路-python介绍

    一.Python及其他语言 有很多种分类方法,其中一种是按照解释型和编译型来划分的. 编译型:例如C,C++ 优点:运行效率高 缺点:依赖编译平台 (不能跨平台,开发效率低) 解释型:例如shell, ...

  8. 持续集成:TestNG中case之间的关系

    持续集成:TestNG中case之间的关系   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq: ...

  9. Mybatis(一) mybatis入门

    学习了hibernate这个持久层框架之后,在来学习Mybatis简直是无压力,因为Mybatis入门门栏很低,如果学习过了hibernate的话,对于Mybatis的学习很简单了,如果没学习过hib ...

  10. 时间同步方法及几个可用的NTP服务器地址

    大家都知道计算机电脑的时间是由一块电池供电保持的,而且准确度比较差经常出现走时不准的时候.通过互联网络上发布的一些公用网络时间服务器NTP server,就可以实现自动.定期的同步本机标准时间. 依靠 ...