题目链接: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. struct TABLE

    struct TABLE { TABLE() {} /* Remove gcc warning */ TABLE_SHARE *s; handler *file; TABLE *next, *prev ...

  2. 宏ut_2pow_round

    计算 m的整数倍 不大于n #define ut_2pow_round(n, m) ((n) & ~((m) - 1)) #include <stdio.h>#include &l ...

  3. SharePoint的安装配置

    安装环境 1. Window server 2008 r2(sp2) OS.2. MS SQL Server 2008 r2.3. Office2010.4. IIS7以上.5. 确认服务器已经加入域 ...

  4. Unit testing Cmockery 简单使用

    /********************************************************************** * Unit testing Cmockery 简单使用 ...

  5. 使php支持mbstring库

    多国语言并存就意味着多字节,PHP内置的字符串长度函数strlen无法正确处理中文字符串,它得到的只是字符串所占的字节数.对于GB2312的中文编码,strlen得到的值是汉字个数的2倍,而对于UTF ...

  6. 【C#学习笔记】读SQL Server2008

    using System; using System.Data.SqlClient; namespace ConsoleApplication { class Program { static voi ...

  7. .gitignore的使用:首次创建及事后添加无法生效.

    问题:使用sourceTree做版本管理后, Xcode工程每次打开, 都会有几个xcodeworkspace之类的文件提示改动了. 如果想ignore掉, 有两种情况: 1:整个工程没有纳入版本管理 ...

  8. poj 1087 A Plug for UNIX

    题目描述:现在由你负责布置Internet联合组织首席执行官就职新闻发布会的会议室.由于会议室修建时被设计成容纳全世界各地的新闻记者,因此会议室提供了多种电源插座用以满足(会议室修建时期)各国不同插头 ...

  9. Android开发 |常见的内存泄漏问题及解决办法

    在Android开发中,内存泄漏是比较常见的问题,有过一些Android编程经历的童鞋应该都遇到过,但为什么会出现内存泄漏呢?内存泄漏又有什么影响呢? 在Android程序开发中,当一个对象已经不需要 ...

  10. 解决32位plsql客户端连接不64位Oracle11g上数据库

    一.解决方案 因为本人安装的是64位的Oracle,plsql 是32位的故连接不上.网上有方法能连接. 1. 文件下载 下载PLSQL_Developer地址 http://pan.baidu.co ...