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} ...
随机推荐
- "Programming"和"Programming"是同一个"Programming"吗?
什么意思? C语言没有专门的字符串类型,但是,它同样可以处理字符串.本文不是讨论字符串的使用,而是讨论C字符串之间的关系.如题,在C语言代码中,如果定义#define STR = "Prog ...
- 使用 libevent 和 libev 提高网络应用性能——I/O模型演进变化史
构建现代的服务器应用程序需要以某种方法同时接收数百.数千甚至数万个事件,无论它们是内部请求还是网络连接,都要有效地处理它们的操作. 有许多解决方案,但事件驱动也被广泛应用到网络编程中.并大规模部署在高 ...
- 非关系型数据库SequoiaDB虚拟机下应用再探
上一次浅谈了SequoiaDB在虚拟机上的安装及在web下图形界面的基本操控,现在来体验命令行操作及运行samples文件. 基本DDL操作 首先在视窗窗口同时按Ctrl+Alt+T,进入到命令行窗口 ...
- PHP调用WEBSERVICE接口常见问题答疑以及总结
最近的工作项目中,接触到了很多的政府 微信开发项目.对方的外包公司都是使用JAVA作为开发语言,然后通过WEBSERVICE进行接口返回数据到我的项目中.一般情况下,能在浏览器打开并显示数据的接口是直 ...
- php中$this、static、final、const、self 等几个关键字的用法
<?phpclass A { public static function get_self(){ return new self(); } public static function get ...
- How to Call a synchronize function asynchronizly in C#
How to call a function asynchronizly in C# # Page1- Delegate.begininvoke, endinvoke BeginInvoke and ...
- firebreath 在谷歌和火狐浏览器下的调试 以及打包
在寻找插件开发资料的过程中找到了一个开发浏览器插件的开源项目——firebreath firebreath的安装以及测试我就不再叙述了,可以参考大神的文章 . http://www.blogjava. ...
- JAVA栈实例—括号匹配
import java.util.Stack; public class test { public static void main(String[] args){ System.out.print ...
- 读《JavaScript语言精粹》的一些感言
最近看了<JavaScript语言精粹>,并且连着看了两遍,如果非要用言语形容的话,那我只能用4个字来形容:相见恨晚.其中的一些经验经过这么多年的摸索其实也了然,但是作者用这么浅薄的书把有 ...
- ios-UIWebView中js和oc代码的互调
webview是ios中显示远程数据的网页控件,webview能显示的内容很多,MP4.文本.pdf等等: 关于js和oc代码的互相调用 1:oc中调用js代码; >>oc中调用js代码很 ...