HDU1542 Atlantis(矩形面积并)
#pragma warning (disable:4996)
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#define maxn 150
using namespace std; int n;
struct Segment
{
double l, r, x;
int li, ri;
bool isleft;
bool operator < (const Segment &b) const{
return x < b.x;
}
}s[maxn*3]; int top;
double ydis[800];
int ytop = 0; struct Node
{
int cov;
int l, r;
double len;
}N[3000]; void build(int i, int L, int R)
{
N[i].cov = 0;
N[i].l = L; N[i].r = R;
N[i].len = ydis[R] - ydis[L];
if (N[i].r-N[i].l<=1){
return;
}
int M = (L + R) >> 1;
build(i << 1, L, M);
build(i << 1 | 1, M, R);
} void pushDown(int i)
{
if (N[i].r-N[i].l<=1) return;
if (N[i].cov!=0){
N[i << 1].cov += N[i].cov;
N[i << 1 | 1].cov += N[i].cov;
N[i].cov = 0;
}
} void add(int i, int L, int R,int val)
{
if (N[i].l == L&&N[i].r == R){
N[i].cov += val;
return;
}
pushDown(i);
int M = (N[i].l + N[i].r) >> 1;
if (R <= M) add(i << 1, L, R, val);
else if (L >= M) add(i << 1 | 1, L, R, val);
else {
add(i << 1, L, M, val);
add(i << 1 | 1, M, R, val);
}
} double query(int i)
{
pushDown(i);
if (N[i].r - N[i].l <= 1&&N[i].cov>0) return N[i].len;
else if (N[i].r - N[i].l <= 1) return 0;
return query(i << 1) + query(i << 1 | 1);
} int main()
{
int ca = 0;
while (cin >> n&&n)
{
double x1, y1, x2, y2; top = 0; ytop = 0;
for (int i = 0; i < n; i++){
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
s[top].x = x1; s[top].l = y1; s[top].r = y2; s[top++].isleft = true;
s[top].x = x2; s[top].l = y1; s[top].r = y2; s[top++].isleft = false;
ydis[ytop++] = y1; ydis[ytop++] = y2;
}
sort(ydis, ydis + ytop);
ytop = unique(ydis, ydis + ytop) - ydis;
sort(s, s + top);
for (int i = 0; i < top; i++){
s[i].li = lower_bound(ydis, ydis + ytop, s[i].l) - ydis;
s[i].ri = lower_bound(ydis, ydis + ytop, s[i].r) - ydis;
}
ydis[ytop] = ydis[ytop - 1];
build(1, 0, ytop); double ans = 0;double lx = s[0].x;
for (int i = 0; i < top; i++){
ans += query(1)*(s[i].x - lx);
if (s[i].isleft){
add(1, s[i].li, s[i].ri ,1);
}
else{
add(1, s[i].li, s[i].ri, -1);
}
lx = s[i].x;
}
printf("Test case #%d\n", ++ca);
printf("Total explored area: %.2lf\n", ans);
puts("");
}
return 0;
}
HDU1542 Atlantis(矩形面积并)的更多相关文章
- hdu1542 Atlantis(矩阵面积的并)
这个题算是我的第一个扫描线的题,扫描线算是一种思想吧,用到线段树+离散化.感觉高大上. 主要参考了这位大神的博客. http://www.cnblogs.com/kuangbin/archive/20 ...
- 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)
[题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...
- HDU1542 扫描线(矩形面积并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- (HDU 1542) Atlantis 矩形面积并——扫描线
n个矩形,可以重叠,求面积并. n<=100: 暴力模拟扫描线.模拟赛大水题.(n^2) 甚至网上一种“分块”:分成n^2块,每一块看是否属于一个矩形. 甚至这个题就可以这么做. n<=1 ...
- POJ 1151 Atlantis 矩形面积求交/线段树扫描线
Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...
- POJ-1151 Atlantis 矩形面积并
题目链接:http://poj.org/problem?id=1151 扫描线+离散+线段树,线段树每个节点保存的是离散后节点右边的线段. //STATUS:C++_AC_16MS_208KB #in ...
- poj1151 Atlantis && cdoj 1600艾尔大停电 矩形面积并
题目: Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23758 Accepted: 8834 Des ...
- hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)
传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...
随机推荐
- 在WPF程序中将控件所呈现的内容保存成图像(转载)
在WPF程序中将控件所呈现的内容保存成图像 转自:http://www.cnblogs.com/TianFang/archive/2012/10/07/2714140.html 有的时候,我们需要将控 ...
- Ubuntu下sqlite3的安装及使用
Sqlite是一款轻型的数据库,实现了多数SQL-92标准,包括事务(原子性,一致性,隔离性和持久性 ACID),触发器与多数复杂查询.对于一个移动手持设备的应用开发者,Sqlite是居家旅行必备数据 ...
- Android--将字节数转化为B,KB,MB,GB的方法
//将字节数转化为MB private String byteToMB(long size){ long kb = 1024; long mb = kb*1024; long gb = mb*1024 ...
- 35.在PCB中删除元件
在PCB Editor里面,如果想进行什么操作,首先得点击这个命令,再点击你要操作的区域/元件,最后右键选择"Done",这样你才能完成一个操作.
- VS2013中如何更改主题颜色(深色)和恢复默认的窗口布局
1.通常情况下,我们会根据个人爱好更改VS2013的主题颜色,一开始我喜欢白色,后来我偏爱深色. 依次选择:工具->选项->常规->主题->深色->确定,ok 2.我们在 ...
- [转]Similarities between Hibernate and JDBC objects
- php对图片反色处理
今天有个需求用php对图片进行反色,和转灰,之前不知道可不可行,后来看到了imagefilter()函数,用来转灰绰绰有余,好强大: imagefilter($im, IMG_FILTER_GRAYS ...
- JRebel: ERROR Could not define reloadable class 'com.sun.proxy.$Proxy118': java.lang.OutOfMemoryError: PermGen space
MyEclipse由于配置了JRebel,所以是它报错,不过根本问题还是:java.lang.OutOfMemoryError: PermGen space 现在按照经验调整内存大小. 在MyEcli ...
- C++中定义比较函数的三种方法
原文地址:http://fusharblog.com/3-ways-to-define-comparison-functions-in-cpp/ C++编程优与Pascal的原因之一是C++中存在ST ...
- BZOJ 1012: [JSOI2008]最大数maxnumber 线段树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能: ...