题目链接: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+线段树+扫描线)的更多相关文章

  1. Atlantis poj1151 线段树扫描线

    Atlantis poj1151 线段树扫描线 题意 题目给了n个矩形,每个矩形给了左下角和右上角的坐标,矩形可能会重叠,求的是矩形最后的面积. 题解思路 这个是我线段树扫描线的第一题,听了学长的讲解 ...

  2. hdu1542 Atlantis (线段树+扫描线+离散化)

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

  3. POJ1151+线段树+扫描线

    /* 线段树+扫描线+离散化 求多个矩形的面积 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...

  4. poj1151 Atlantis (线段树+扫描线+离散化)

    有点难,扫描线易懂,离散化然后线段树处理有点不太好理解. 因为这里是一个区间,所有在线段树中更新时,必须是一个长度大于1的区间才是有效的,比如[l,l]这是一根线段,而不是区间了. AC代码 #inc ...

  5. P - Atlantis (线段树+扫描线)

      There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Som ...

  6. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

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

  7. HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)

    传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...

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

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

  9. 【POJ1151】Atlantis(线段树,扫描线)

    [POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...

随机推荐

  1. activemq控制面板里的NumberOfPendingMessages、MessagesEnqueued、MessagesDequeued含义

    Number Of Consumers 消费者 这个是消费者端的消费者数量.Number Of Pending Messages 等待消费的消息 这个是当前未出队列的数量.可以理解为总接收数-总出队列 ...

  2. wine update错误 "the cache has no package" error when wine update is available

    网址:https://bugs.launchpad.net/pipelight/+bug/1318321/

  3. str.substring(beginIndex,endIndex)-008

    // 将字符串str前n位放在后面,返回新的字符串 public String headToTail(String str,int n){ if(n==0){ System.out.println(s ...

  4. 在windows搭建react

    1.安装必须的软件 1.Python 2    注意勾选 Add python.exe to Path,选项,这样就可以在安装完成后,不用手动去添加环境变量    安装完,打开cmd.exe,输入py ...

  5. bsxfun函数

    函数功能:两个数组间元素逐个计算的二值操作 使用方法:C=bsxfun(fun,A,B) 两个数组A合B间元素逐个计算的二值操作,fun是函数句柄或者m文件,也可以为如下内置函数: @plus 加@m ...

  6. Java的第一个程序-Hello, World !

    学了一个月的Java,现在总结一下,就算复习了. 一.安装Java环境 这个没啥好说的. 1. 官网下载JDK安装 2. 配置环境变量.注意的是:环境变量配置好以后,如果cmd中运行 java 命令没 ...

  7. [HDU4532]湫秋系列故事——安排座位

    题面在这里 description 有\(n\)种颜色的小球,每种颜色的小球有\(a_i\)个: 要把它们摆成一排,求相邻小球颜色不相同的摆放方案数. 任意两个合理的安排方法,只要有一个位置的同学不同 ...

  8. JUC包中的分而治之策略-为提高性能而生

    一.前言 本次分享我们来共同探讨JUC包中一些有意思的类,包含AtomicLong & LongAdder,ThreadLocalRandom原理. 二.AtomicLong & Lo ...

  9. BZOJ2115:[WC2011]Xor——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2115 https://www.luogu.org/problemnew/show/P4151 这道 ...

  10. Java编程MapReduce实现WordCount

    Java编程MapReduce实现WordCount 1.编写Mapper package net.toocruel.yarn.mapreduce.wordcount; import org.apac ...