题目链接:http://poj.org/problem?id=1151

题意是平面上给你n个矩形,让你求矩形的面积并。

首先学一下什么是扫描线:http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html

这是别人的blog,写的挺好的。然后明白扫描线之后呢,接下来就很简单了,只需要一次一次求面积然后累加就好了。这题离散化之后,数据的范围更小了(因为n只有100),单点更新就行了。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int MAXN = 2e2 + ;
struct data {
double x1 , x2;
double y;
int xx1 , xx2 , flag; //flag: 1表示加 -1表示减 xx1 xx2是离散化之后的值
bool operator <(const data& cmp) const {
return y < cmp.y;
}
}edge[MAXN << ];
struct segtree {
int l , r , lazy; //lazy表示累加的次数
double val; //val表示长度
}T[MAXN << ];
double x[MAXN];
map <double , int> mp; double min(double a , double b) {
return a > b ? b : a;
} double max(double a , double b) {
return a > b ? a : b;
} void init(int p , int l , int r) {
int mid = (l + r) >> ;
T[p].l = l , T[p].r = r , T[p].val = T[p].lazy = ;
if(r - l == ) {
return ;
}
init(p << , l , mid);
init((p << )| , mid , r);
} void updata(int p , int l , int r , int val) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].r - T[p].l == ) {
if(val == -) {
T[p].lazy--;
T[p].val = T[p].lazy ? (x[T[p].r] - x[T[p].l]) : ;
}
else {
T[p].lazy++;
T[p].val = (x[T[p].r] - x[T[p].l]);
}
return ;
}
if(r <= mid) {
updata(p << , l , r , val);
}
else if(l >= mid) {
updata((p << )| , l , r , val);
}
else {
updata(p << , l , mid , val);
updata((p << )| , mid , r , val);
}
T[p].val = T[p << ].val + T[(p << )|].val;
} int main()
{
int n , ca = ;
double x1 , x2 , y1 , y2;
while(~scanf("%d" , &n) && n) {
int cont = ;
mp.clear();
for(int i = ; i < n ; i++) {
scanf("%lf %lf %lf %lf" , &x1 , &y1 , &x2 , &y2);
edge[i * ].x1 = min(x1 , x2) , edge[i * ].x2 = max(x1 , x2);
edge[i * ].y = min(y1 , y2);
edge[i * ].flag = ;
edge[i * + ].x1 = edge[i * ].x1 , edge[i * + ].x2 = edge[i * ].x2;
edge[i * + ].y = max(y1 , y2);
edge[i * + ].flag = -;
if(!mp[x1]) {
x[++cont] = x1;
mp[x1]++;
}
if(!mp[x2]) {
x[++cont] = x2;
mp[x2]++;
}
}
sort(edge , edge + n * );
sort(x + , x + cont + );
for(int i = ; i < n * ; i++) {
int pos = (lower_bound(x + , x + cont + , edge[i].x1) - x);
edge[i].xx1 = pos;
pos = (lower_bound(x + , x + cont + , edge[i].x2) - x);
edge[i].xx2 = pos;
}
init( , , cont);
double res = ;
updata( , edge[].xx1 , edge[].xx2 , edge[].flag);
for(int i = ; i < n * ; i++) {
res += T[].val * (edge[i].y - edge[i - ].y);
updata( , edge[i].xx1 , edge[i].xx2 , edge[i].flag);
}
printf("Test case #%d\n" , ca++);
printf("Total explored area: %.2f\n\n" , res);
}
}

POJ 1151 Atlantis (扫描线+线段树)的更多相关文章

  1. POJ 1151 Atlantis(线段树-扫描线,矩形面积并)

    题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...

  2. poj 1151 (未完成) 扫描线 线段树 离散化

    #include<iostream> #include<vector> #include<cmath> #include<algorithm> usin ...

  3. POJ 1151 Atlantis(扫描线)

    题目原链接:http://poj.org/problem?id=1151 题目中文翻译: POJ 1151 Atlantis Time Limit: 1000MS   Memory Limit: 10 ...

  4. poj1151 Atlantis——扫描线+线段树

    题目:http://poj.org/problem?id=1151 经典的扫描线问题: 可以用线段树的每个点代表横向被矩形上下边分割开的每一格,这样将一个矩形的出现或消失化为线段树上的单点修改: 每个 ...

  5. POJ 1542 Atlantis(线段树 面积 并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 参考网址:http://blog.csdn.net/sunmenggmail/article/d ...

  6. 【POJ】1151 Atlantis(线段树)

    http://poj.org/problem?id=1151 经典矩形面积并吧.....很简单我就不说了... 有个很神的地方,我脑残没想到: 将线段变成点啊QAQ这样方便计算了啊 还有个很坑的地方, ...

  7. [HDU1542]Atlantis(扫描线+线段树)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  8. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

  9. poj 3277 City Horizon (线段树 扫描线 矩形面积并)

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

随机推荐

  1. Codeforces Round #205 (Div. 2)

    A #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  2. oracle 11g rac 无法自动启动

    如果以上的操作依然不能使数据库资源自动启动,那么参考下面这篇文章修改资源AUTO_START属性. 查看资源状态: crsctl status resource 资源       -p     crs ...

  3. “main cannot be resolved or is not a field”解决方案

    .layout.main总是在layout上有错误提示波浪线. 解决方法: (1) 删除"import android.R;". (2) 勾选上Eclipse中的"Pro ...

  4. BZOJ 2179 FFT快速傅里叶

    fft. #include<set> #include<map> #include<ctime> #include<queue> #include< ...

  5. 关于python中使用mongodb模块,save和insert的小问题

    今天写python脚本的时候发现这样一个问题: import os , string , datetime ,pymongo; conn = pymongo.Connection("127. ...

  6. 如何使用 orachk 工具

    Oracle RAC 安装完毕后的健壮性是一个令人头疼的问题.之前Oracle为之专门推出了raccheck工具,确实方便了我们这些个苦逼的DBA.现在Oracle在raccheck的基础之上又推出了 ...

  7. MySQL 5.6 复制:GTID 的优点和限制(第一部分)

    全局事务标示符(Global Transactions Identifier)是MySQL 5.6复制的一个新特性.它为维护特定的复制拓扑结构下服务器的DBA们大幅度改善他们的工作状况提供了多种可能性 ...

  8. LightOJ 1427 -Repository(ac自动机)

    题意: 求每个模式串在母串中出现的次数 #include <map> #include <set> #include <list> #include <cma ...

  9. 【windows核心编程】DLL相关(2)

    关于DLL的延迟加载 延迟加载DLL,使用的是隐式加载方式,当为exe使用的DLL指定为延迟加载的时候,连接器会将exe的[导入段]中去除该DLL的相关信息,同时在exe中嵌入一个新的[延迟加载段]表 ...

  10. Seam carving 学习笔记

    今天首次接触了图像编辑中的seam carving知识,感觉挺神奇的.虽然我自己可能理解的不是很深刻,但是记录下来,总是好的. seam carving直接翻译过来是“线裁剪”的意思.它的主要用途是对 ...