题目链接: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. iOS开发之pch文件

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

  2. Android Doze模式启用和恢复

    从Android 6.0(API level 23)开始,Android提出了两个延长电池使用时间的省电特性给用户.用户管理可以在没有充电的情况下管理app的行为.当用户一段时间没有使用手机的时候,D ...

  3. go单元测试进阶篇

    作者介绍:熊训德(英文名:Sundy),16年毕业于四川大学大学并加入腾讯.目前在腾讯云从事hadoop生态相关的云存储和计算等后台开发,喜欢并专注于研究大数据.虚拟化和人工智能等相关技术. 本文档说 ...

  4. insmod: can't insert 'led.ko': invalid module format详细解释

    insmod: can't insert 'led.ko': invalid module format 之前在Imx257学习版固件编写的驱动想直接移植imx257核心板的开发板上.以为2个板子的源 ...

  5. Xmpp实现简单聊天系列 --- ①openfire部署

    1. 下载最新的openfire安装文件 官方下载站点:http://www.igniterealtime.org/downloads/index.jsp#openfire 2. 下载完成后,执行你的 ...

  6. sublime Text3+emmet(快速开发)

    sublime软件使用Emmet插件快速编写CSS样式                    基本的CSS样式编写时,很多样式只需输入首字母即可得到不带属性值的CSS样式,像上面说到的margin.而 ...

  7. Android 布局(线性布局、相对布局)

    一.线性布局(LinearLayout) <LinearLayout****</LinearLayout>1. orientation(布局方向)value=0 horizontal ...

  8. 24(java_io from keyboard)

    public class ReadFromKB{ public static void main(String args[]) { try { byte bArray[]=new byte[128]; ...

  9. 腾讯云上Selenium用法示例

    欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者:崔庆才 前言 在上一节我们学习了PhantomJS 的基本用法,归根结底它是一个没有界面的浏览器,而且运 ...

  10. 实现自动登录:Filter 实现思路和方式

    当你勾选(记住登录状态),用cookie保存用户名和密码.不勾选,cookie失效. 所有的页面都要经过autoLoginFilter.java 的过滤器,在这类中,必须要判断cookies不为nul ...