题目链接: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. jquery拖拽插件 tableDnD

    http://www.jb51.net/article/39481.htm http://www.poluoluo.com/jzxy/201307/232615.html

  2. Liniux系统下目录的权限意义

    访问者及其基本权限 Linux系统内的文件访问者有三种身份,分别是: a) 文件和文件目录的所有者: u---User(所有权);b) 文件和文件目录的所有者所在的组的用户: g---Group;c) ...

  3. java封装的方法

    java封装是由Java是面向对象程序设计语言的性质决定的,面向对象程序设计语言的三大特性之一就是封装.封装其实就是包装的意思,从专业的角度来看,就是把对象的所有组成部分组合在一起,保护私有属性. 如 ...

  4. shell是什么,各种shell的初步认识,适用于初学者

    shell是什么?有什么用处?怎么用?我相信,这是大部分人刚接触到shell都有过的疑问.下面小编为大家讲解一下自己的讲解,希望能对大家有所帮助. 什么是shell? shell就是系统内核的一层壳, ...

  5. Itext中 根据html生成Word文件,包含图片

    package cn.com.wzf; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.Str ...

  6. 老李分享:Android -自动化埋点 1

    老李分享:Android -自动化埋点   当我们开发一款Android应用上线后,希望能收集一些用户操作的行为数据,比如用户在某个页面点击了多少次,在某个控件被点击了多少次,在某个页面停 留了多少时 ...

  7. JAVA加密算法系列-BASE64

    package ***; import java.io.IOException; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encode ...

  8. myeclipse2017破解失败解决办法

    最近,笔者安装的myeclipse2017破解出了问题,破解本来是很简单的事,就是几步而已,但是一直出问题,现在安利一波myeclipse2017版破解失败解决办法.诸如下图:()因为笔者已经破解好了 ...

  9. java 客户端发起http请求

    package com.mall.core.utils.http; import org.apache.commons.lang.StringUtils; import org.apache.http ...

  10. Centos7多网卡绑定操作,通过nmcli命令操作。

    运行 ip link 命令查看系统中可用的接口1.创建bond网卡nmcli con add type team con-name team0 ifname team0 config '{" ...