Atlantis(POJ1151+线段树+扫描线)
题目链接:http://poj.org/problem?id=1151
题目:


题意:求所有矩形的面积,重合部分只算一次。
思路:扫描线入门题,推荐几篇学扫描线的博客:
1.http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html
2.https://blog.csdn.net/qq_38786088/article/details/78633478
3.https://blog.csdn.net/lwt36/article/details/48908031(强烈推荐!)
代码实现如下:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef unsigned long long ull; #define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f; inline int read() {//读入挂
int ret = , c, f = ;
for(c = getchar(); !(isdigit(c) || c == '-'); c = getchar());
if(c == '-') f = -, c = getchar();
for(; isdigit(c); c = getchar()) ret = ret * + c - '';
if(f < ) ret = -ret;
return ret;
} int n, t;
double x1, y_1, x2, y_2;
double y[]; struct Line {
double y1, y2, x;
int flag;
bool operator < (const Line& a) const {
return x < a.x;
}
}line[]; struct node {
int l, r, cover;
double lf, rf, len;
}segtree[maxn*]; void push_up(int i) {
if(segtree[i].cover > ) {
segtree[i].len = segtree[i].rf - segtree[i].lf;
} else if(segtree[i].l + == segtree[i].r) {
segtree[i].len = ;
} else {
segtree[i].len = segtree[i*].len + segtree[i*+].len;
}
} void build(int i, int l, int r) {
segtree[i].l = l, segtree[i].r = r;
segtree[i].lf = y[l], segtree[i].rf = y[r];
segtree[i].cover = segtree[i].len = ;
if(l + == r) return;
int mid = (l + r) >> ;
build(i * , l, mid);
build(i * + , mid, r);
} void update(int i, double l, double r, int flag) {
if(segtree[i].lf == l && segtree[i].rf == r) {
segtree[i].cover += flag;
push_up(i);
return;
}
if(l >= segtree[i * + ].lf) {
update(i * + , l, r, flag);
} else if(r <= segtree[i * ].rf) {
update(i * , l, r, flag);
} else {
update(i * , l, segtree[i*].rf, flag);
update(i * + , segtree[i*+].lf, r, flag);
}
push_up(i);
} int main() {
//FIN;
int icase = ;
while(~scanf("%d", &n) && n) {
t = ;
printf("Test case #%d\n", ++icase);
for(int i = ; i <= n; i++,t++) {
scanf("%lf%lf%lf%lf", &x1, &y_1, &x2, &y_2);
line[t].x = x1;
line[t].y1 = y_1;
line[t].y2 = y_2;
line[t].flag = ;
y[t] = y_1;
line[++t].x = x2;
line[t].y1 = y_1;
line[t].y2 = y_2;
line[t].flag = -;
y[t] = y_2;
}
sort(line + , line + t);
sort(y + , y + t);
build(, , t - );
double ans = ;
update(, line[].y1, line[].y2, line[].flag);
for(int i = ; i < t; i++) {
ans += segtree[].len * (line[i].x - line[i-].x);
update(, line[i].y1, line[i].y2, line[i].flag);
}
printf("Total explored area: %.2f\n\n", ans);
}
return ;
}
Atlantis(POJ1151+线段树+扫描线)的更多相关文章
- Atlantis poj1151 线段树扫描线
Atlantis poj1151 线段树扫描线 题意 题目给了n个矩形,每个矩形给了左下角和右上角的坐标,矩形可能会重叠,求的是矩形最后的面积. 题解思路 这个是我线段树扫描线的第一题,听了学长的讲解 ...
- hdu1542 Atlantis (线段树+扫描线+离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- POJ1151+线段树+扫描线
/* 线段树+扫描线+离散化 求多个矩形的面积 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...
- poj1151 Atlantis (线段树+扫描线+离散化)
有点难,扫描线易懂,离散化然后线段树处理有点不太好理解. 因为这里是一个区间,所有在线段树中更新时,必须是一个长度大于1的区间才是有效的,比如[l,l]这是一根线段,而不是区间了. AC代码 #inc ...
- P - Atlantis (线段树+扫描线)
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Som ...
- HDU 1542 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} ...
- POJ 1151 Atlantis(线段树-扫描线,矩形面积并)
题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...
- 【POJ1151】Atlantis(线段树,扫描线)
[POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...
随机推荐
- python学习笔记07:自定义类型
class person: def __init__(self,name,age,weight): self.name = name self.age = age self.weight = weig ...
- php mongodb扩展 其他扩展也类似
MongoDBPHP 扩展 本教程将向大家介绍如何在Linux.window.Mac平台上安装MongoDB扩展. Linux上安装 MongoDB PHP扩展 在终端上安装 你可以在linux中执行 ...
- PHP中如何使用Redis接管文件存储Session详解
https://www.jb51.net/article/151580.htm 前言 php默认使用文件存储session,如果并发量大,效率会非常低.而redis对高并发的支持非常好,可以利用red ...
- JAVA学习之HashCode
public native int hashCode(); 返回该对象的哈希码值.支持此方法是为了提高哈希表(例如 java.util.Hashtable 提供的哈希表)的性能. 一.HashCode ...
- Winform程序部署方式总结一——ClickOnce发布
针对Winform程序,介绍两种常用打包方式:ClickOnce和Windows Installer 应用程序如下: 一.ClickOnce发布 1.找到需要发布的项目文件,右击,从弹出的快捷菜单中找 ...
- 【bzoj4300】绝世好题 dp
题目描述 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len). 输入 输入文件共2行. 第一行包括一个整数n. 第二行包括n个 ...
- 【bzoj1593】[Usaco2008 Feb]Hotel 旅馆 线段树区间合并
题目描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...
- 楼房 洛谷1382 && codevs2995
P1382 楼房 题目描述 地平线(x轴)上有n个矩(lou)形(fang),用三个整数h[i],l[i],r[i]来表示第i个矩形:矩形左下角为(l[i],0),右上角为(r[i],h[i]).地平 ...
- Greenlet-手动切换
yield()是自己写的协程,Greenlet( )是已经封装好了的协程. 协程:遇到 I/O 操作就切换到别的地方了(先去处理其他携程去了).等原协程的 I/O 操作一完成就切回去.这样就把 I/O ...
- BZOJ5338:[TJOI2018]异或——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5338 现在有一颗以1为根节点的由n个节点组成的树,树上每个节点上都有一个权值vi. 现在有Q 次操 ...