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 ...
随机推荐
- 微软职位内部推荐-Sr SDE for Win Apps Ecosystem
微软近期Open的职位: Job posting title: Senior Software Design Engineer Location: China, Beijing Level: 63 D ...
- LintCode-Search 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...
- Could not find file '..\bin\hibernate.cfg.xml'解决方法:
web.config: 解决方法:
- 《C++Primer》复习——with C++11 [1]
1.头文件中不应包含using声明,因为头文件的内容会拷贝到所有引用到他的文件中去,如果头文件里有谋个using声明,那么每个使用了该头文件的文件就会有这个声明,由于不经意间包含了一些名字,反而可能产 ...
- ios 图形学习笔记
一.显示文本: 1.UIFont,用于设置显示的字体 初始化方法:fontNamesForFamilyName:(类方法) fontWithName:size:(类方法) 2.绘制文本的方法: NSS ...
- 【BZOJ】【1529】 【POI2005】ska Piggy banks
本来以为是tarjan缩点……但是64MB的空间根本不足以存下原图和缩点后的新图.所以呢……并查集= = orz hzwer MLE的tarjan: /************************ ...
- oracle——merge
一.概述 使用merge声明从一个或者更多个表或视图中筛选记录,以用来更新或者插入到一个表或视图中.你可以指定条件以决定是执行update操作还是insert操作到目标表或视图中. 这个声明是一个 ...
- CSS reset--重置样式
在一般我们写一个自己自定义的HTML的时候,我们会清除样式或者说重置样式 重置样式.清除浏览器默认样式,一切全部用自己的设置,并配置适合设计的基础样式 下面给出所有一般需要清除的样式: html,bo ...
- Caffe训练好的网络对图像分类
对于训练好的Caffe 网络 输入:彩色or灰度图片 做minist 下手写识别分类,不能直接使用,需去除均值图像,同时将输入图像像素归一化到0-1直接即可. #include <caffe/c ...
- uva 1344
这本来是暑假集训做过的一个题 现在做来 就三种情况 1.田忌最快的比齐王最快的快 就用最快的比最快的 2.田忌最慢的比齐王最慢的快 就用最慢的比最慢的 3.上两种情况都不符合 用田忌最慢的去比齐王最快 ...