POJ 1151 HDU 1542 Atlantis(扫描线)
题目大意就是:去一个地方探险,然后给你一些地图描写叙述这个地方,每一个描写叙述是一个矩形的右下角和左上角。地图有些地方是重叠的。所以让你求出被描写叙述的地方的总面积。
扫描线的第一道题,想了又想,啸爷还给我讲了讲,最终有点理解了啊。
先说扫描线:书上说扫描线不是一个物体。而是一个概念。
在计算几何中的作用类似于图论中的bfs与dfs。所以还是须要多做题目来体会一下啊。
这道题目的做法是:离散化x坐标。然后依照y坐标的大小进行排序,每一条保存它的左边界的位置与右边界的位置。以及自身的高度。
还有就是假设是下边初始为1。上边初始为-1。
接下来就是扫描线了:依照y值排序后的数组,開始遍历。
先二分查找它在离散数组中下表的位置。找到之后按他保存的边界的标记。进行更新。这里的区间更新用的是线段树的维护。我们以离散化后数组建树。
每一个节点保存它此时有多少个上界与下界的和,表示他是否存在矩形。
假设存在的话每一个节点中用sun数组保存这个矩形x值的差值(通过这个离散区间的下标,做减法就是离散的x的差值)。最后的时候乘上y的差值。
就是扫描到的矩阵的面积。
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17207 | Accepted: 6549 |
Description
total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
Input
100000;0 <= y1 < y2 <= 100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don't process it.
Output
(i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case.
Sample Input
2
10 10 20 20
15 15 25 25.5
0
Sample Output
Test case #1
Total explored area: 180.00
Source
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-12
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x) using namespace std; const int maxn = 5010; struct node
{
double l, r, h;
int x;
} f[maxn]; double sum[maxn<<2];
int cnt[maxn];
double dc[maxn]; bool cmp(node a, node b)
{
return a.h < b.h;
} int Find(double x, double a[], int n)
{
int l = 0;
int r = n-1;
while(l <= r)
{
int mid = (l+r)/2;
if(a[mid] == x) return mid;
if(a[mid] > x) r = mid-1;
else l = mid+1;
}
return -1; } void Up(int l, int r, int site)
{
if(cnt[site]) sum[site] = dc[r+1]-dc[l];
else if(l == r) sum[site] = 0;
else sum[site] = sum[site<<1]+sum[site<<1|1];
} void Update(int l, int r, int L, int R, int d, int site)
{
if(L <= l && r <= R)
{
cnt[site] += d;
Up(l, r, site);
return;
}
int mid = (l+r)>>1;
if(L <= mid) Update(l, mid, L, R, d, site<<1);
if(R > mid) Update(mid+1, r, L, R, d, site<<1|1);
Up(l, r, site);
} int main()
{
int n;
int Case = 1;
while(cin >>n)
{
if(!n) break;
double x1, y1, x2, y2;
int m = 0;
for(int i = 0; i < n; i++)
{
scanf("%lf %lf %lf %lf",&x1, &y1, &x2, &y2);
dc[m] = x1;
f[m].l = x1;
f[m].r = x2;
f[m].h = y1;
f[m++].x = 1;
dc[m] = x2;
f[m].l = x1;
f[m].r = x2;
f[m].h = y2;
f[m++].x = -1;
}
sort(dc, dc+m);
sort(f, f+m, cmp);
int k = unique(dc, dc+m)-dc;
memset(cnt, 0 , sizeof(cnt));
memset(sum, 0 , sizeof(sum));
double ans = 0;
for(int i = 0; i < m-1; i++)
{
int l = Find(f[i].l, dc, k);
int r = Find(f[i].r, dc, k)-1;
if(l <= r) Update(0, k-1, l, r, f[i].x, 1);
ans += sum[1]*(f[i+1].h-f[i].h);
}
printf("Test case #%d\n",Case++);
printf("Total explored area: %.2f\n\n",ans);
}
return 0;
}
POJ 1151 HDU 1542 Atlantis(扫描线)的更多相关文章
- POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...
- (HDU 1542) Atlantis 矩形面积并——扫描线
n个矩形,可以重叠,求面积并. n<=100: 暴力模拟扫描线.模拟赛大水题.(n^2) 甚至网上一种“分块”:分成n^2块,每一块看是否属于一个矩形. 甚至这个题就可以这么做. n<=1 ...
- HDU 1542 Atlantis(矩形面积并)
HDU 1542 Atlantis 题目链接 题意:给定一些矩形,求面积并 思路:利用扫描线,因为这题矩形个数不多,直接暴力扫就能够了.假设数据大.就要用线段树 代码: #include <cs ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- hdu 1542 Atlantis(线段树,扫描线)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 1542 Atlantis(段树&扫描线&面积和)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- (中等) HDU 1542 Atlantis,扫描线。
Problem Description There are several ancient Greek texts that contain descriptions of the fabled is ...
- HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- LeetCode695--岛屿的最大面积
''' 岛屿的最大面积 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被水包围着 ...
- grunt与requirejs结合使用
// 多个js压缩成一个js // Project configuration. module.exports = function(grunt) { // 使用严格模式 'use strict'; ...
- luogu2805 [NOI2009]植物大战僵尸
想象一下,要搞掉一个植物,必须先搞掉另一些植物--我们可以发现这是一个最大权闭合子图的问题. 最大权闭合子图的话,太空飞行计划问题是一个入门题,可以一看. 然而我们手玩一下样例就会惊恐地发现,保护关系 ...
- ubuntu上传项目到github
https://blog.csdn.net/ajianyingxiaoqinghan/article/details/70544159
- [luoguP3608] [USACO17JAN]Balanced Photo平衡的照片(树状数组 + 离散化)
传送门 树状数组裸题 #include <cstdio> #include <cstring> #include <iostream> #include <a ...
- 刷题总结——烽火传递(单调队列+dp)
题目: 题目描述 烽火台又称烽燧,是重要的防御设施,一般建在险要处或交通要道上.一旦有敌情发生,白天燃烧柴草,通过浓烟表达信息:夜晚燃烧干柴,以火光传递军情.在某两座城市之间有 n 个烽火台,每个烽火 ...
- hdu4035 Maze (树上dp求期望)
dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点1处(概率为ki) 2.找到出口,走出迷宫 ...
- 洛谷 [P3812] 线性基
异或空间下的线性基模版 异或空间下求线性基,本质还是高斯消元,参见 http://www.cnblogs.com/Mr-WolframsMgcBox/p/8562924.html 求最大值是一个贪心的 ...
- 标准C程序设计七---22
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- PEP8 Python编码规范(转)
一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...