HDU 1255 覆盖的面积 (扫描线 线段树 离散化 矩形面积并)
题意:中文题意。
分析:纯手敲,与上一道题目很相似,但是刚开始我以为只是把cnt》=0改成cnt>=2就行了,、
但是后来发现当当前加入的线段的范围之前 还有线段的时候就不行了,因为虽然现在都不等于
2,但是之前的那个线段加上现在的已经覆盖2次了。
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL __int64
#define lson l, mid, 2*rt
#define rson mid+1, r, 2*rt+1
const int maxn = +;
using namespace std;
int n;
double y[maxn];
struct node
{
int l, r, c;
double cnt, lf, rf, more; //cnt还是代表覆盖的长度,增加了more代表两次及以上覆盖的长度
}tr[*maxn];
struct Line
{
double x, y1, y2;
int f;
}line[maxn];
bool cmp(Line a, Line b)
{
return a.x < b.x;
}
void build(int l, int r, int rt)
{
tr[rt].l = l; tr[rt].r = r;
tr[rt].c = ; tr[rt].cnt = ;
tr[rt].more = ;
tr[rt].rf = y[r]; tr[rt].lf = y[l];
if(l+==r) return;
int mid = (l+r)/;
build(l, mid, *rt);
build(mid, r, *rt+);
}
void calen(int rt)
{
if(tr[rt].c==)
{
if(tr[rt].l+==tr[rt].r)
{
tr[rt].cnt = ; tr[rt].more = ;
}
else
{
tr[rt].cnt = tr[*rt].cnt+tr[*rt+].cnt;
tr[rt].more = tr[*rt].more+tr[*rt+].more;
}
}
if(tr[rt].c==) //注意这一步是关键
{
tr[rt].cnt = tr[rt].rf-tr[rt].lf;
if(tr[rt].l+==tr[rt].r) //因为没有注意是否到最后,错了一遍
tr[rt].more = ;
else
tr[rt].more = tr[*rt].cnt + tr[*rt+].cnt; //为1的时候如果下面也有就加上
}
if(tr[rt].c>=)
{
tr[rt].more = tr[rt].rf-tr[rt].lf;
tr[rt].cnt = tr[rt].more;
}
}
void update(int rt, Line e)
{
if(e.y1==tr[rt].lf && e.y2==tr[rt].rf)
{
tr[rt].c += e.f;
calen(rt);
return;
}
if(e.y2<=tr[*rt].rf) update(*rt, e);
else if(e.y1>=tr[*rt+].lf) update(*rt+, e);
else
{
Line tmp;
tmp = e;
tmp.y2 = tr[*rt].rf; update(*rt, tmp);
tmp = e;
tmp.y1 = tr[*rt+].lf; update(*rt+, tmp);
}
calen(rt);
}
int main()
{
int t, i, cnt;
double x1, x2, y1, y2, ans;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
cnt = ; ans = ;
for(i = ; i <= n; i++)
{
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line[cnt].x = x1; line[cnt].y1 = y1;
line[cnt].y2 = y2; line[cnt].f = ;
y[cnt++] = y1;
line[cnt].x = x2; line[cnt].y1 = y1;
line[cnt].y2 = y2; line[cnt].f = -;
y[cnt++] = y2;
}
sort(y+, y+cnt);
sort(line+, line+cnt, cmp);
cnt --;
build(, cnt, );
update(, line[]);
for(i = ; i <= cnt; i++)
{
ans += tr[].more*(line[i].x - line[i-].x);
update(, line[i]);
}
printf("%.2lf\n", ans);
}
return ;
}
HDU 1255 覆盖的面积 (扫描线 线段树 离散化 矩形面积并)的更多相关文章
- HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)
链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...
- POJ 1151Atlantis 扫描线+线段树求矩形面积并
题目链接 #include <iostream> #include <vector> #include <cstdio> #include <cstring& ...
- hdu 5091 给定矩形覆盖尽量多点 扫描线+线段树
http://acm.hdu.edu.cn/showproblem.php?pid=5091 给你10000以内的敌舰的坐标(即分别为x,y),要求用W*H的矩形去围住一个区域,使得这个区域内的敌舰最 ...
- POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...
- 【hdu1542】线段树求矩形面积并
分割线内容转载自http://hzwer.com/879.html ------------------------------------------------------------------ ...
- POJ 1151 Atlantis 线段树求矩形面积并 方法详解
第一次做线段树扫描法的题,网搜各种讲解,发现大多数都讲得太过简洁,不是太容易理解.所以自己打算写一个详细的.看完必会o(∩_∩)o 顾名思义,扫描法就是用一根想象中的线扫过所有矩形,在写代码的过程中, ...
- HDU 1542:Atlantis(扫描线+线段树 矩形面积并)***
题目链接 题意 给出n个矩形,求面积并. 思路 使用扫描线,我这里离散化y轴,按照x坐标从左往右扫过去.离散化后的y轴可以用线段树维护整个y上面的线段总长度,当碰到扫描线的时候,就可以统计面积.这里要 ...
- POJ1151Atlantis 矩形面积并[线段树 离散化 扫描线]
Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21734 Accepted: 8179 Descrip ...
- POJ 1151Atlantis 矩形面积并[线段树 离散化 扫描线]
Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21734 Accepted: 8179 Descrip ...
随机推荐
- JGibbLDA:java版本的LDA(Latent Dirichlet Allocation)实现、修改及使用
转载自:http://blog.csdn.net/memray/article/details/16810763 一.概述 JGibbLDA是一个java版本的LDA(Latent Dirichl ...
- python 发邮件 utf-8
import smtplib from operator import itemgetter, attrgetter from email.mime.text import MIMEText from ...
- Windows下JNI执行步骤
问题描述: java JNI使用 问题解决: JNI编写步骤: (1)编写带有native声明的方法的java类 (2)使用javac编译所写的java类,然后使用javah生成扩展名 ...
- ReplicaManager之DelayedOperation
DelayedOperation包括两种:DelayedFetch和DelayedProduce,它们的存在是由Kafka Protocol决定的,而Kafka Protocol是由实际需求决定的…… ...
- 山寨小小军团开发笔记 之 Arrow Projectile
好久没怎么更新博客了,今天抽空来一篇,讨论一下弓箭的轨迹生成. 一.原理 弓箭的轨迹本质就是一个数学问题,使用一个 bezier 曲线公式就可以插值生成.得到轨迹后,做一个lookAt就可以了. 二. ...
- NGINX的奇淫技巧 —— 3. 不同域名输出不同伺服器标识
NGINX的奇淫技巧 —— 3. 不同域名输出不同伺服器标识 ARGUS 1月13日 发布 推荐 0 推荐 收藏 6 收藏,707 浏览 大家或许会有这种奇葩的需求...要是同一台主机上, 需要针对不 ...
- ios学习笔记block回调的应用(一个简单的例子)
一.什么是Blocks Block是一个C级别的语法以及运行时的一个特性,和标准C中的函数(函数指针)类似,但是其运行需要编译器和运行时支持,从ios4.0开始就很好的支持Block. 二. ...
- sqlserver 空间数据类型
--.建立有空间数据的表 create table x ( v ,) primary key, geog geography not null, geogWKT as geog.STAsText() ...
- MQTT之 Mosquitto hello world的使用
服务端发布消息模式,客户端订阅: 终端一中启动 moquitto 服务器 shallbeThatIshallbe:mosquitto iamthat$ 1427293344: Opening ipv4 ...
- 如何在linux系统下对文件夹名有空格的文件夹进行操作
http://www.2cto.com/os/201409/335119.html 在Windows操作系统中可以轻易地创建\移动\删除文件夹名带有空格的文件夹, 而在linux则需要进行一些特殊的处 ...