【POJ1151】【扫描线+线段树】Atlantis
Description
Input
The input file is terminated by a line containing a single 0. Don't process it.
Output
Output a blank line after each test case.
Sample Input
2
10 10 20 20
15 15 25 25.5
0
Sample Output
Test case #1
Total explored area: 180.00
Source
/*
元稹
《离思五首·其四》 曾经沧海难为水,除却巫山不是云。
取次花丛懒回顾,半缘修道半缘君。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = + ;
const int MAXM = + ;
const int INF = ;
const int SIZE = ;
const int maxnode = 0x7fffffff + ;
using namespace std;
struct Line{//扫描线
int flag;//表示是出线还是入线
double x, y1, y2; Line (double a, double b, double c, double d){
flag = (int)d;
x = a;
y1 = b;
y2 = c;
}
bool operator < (const Line &b)const{
return x < b.x;
};
};
struct Seg_Tree{
struct Node{
int l, r, flag;
double ll, rr, len;
}tree[];
vector<double>y, data;
vector<Line>line;
map<double, int>Map;//离散化 void update(int t, int s, int e, int val){
int l = tree[t].l, r = tree[t].r;
if ((l + ) == r) {
tree[t].flag += val;
if (tree[t].flag == ) tree[t].len = ;
else tree[t].len = tree[t].rr - tree[t].ll;
}
else{
int mid = (l + r)>>;
if (s < mid) update(t << , s, e, val);
if (e > mid) update((t << ) | , s, e, val);
tree[t].len = tree[t << ].len + tree[(t << ) | ].len; }
}
void build(int t, int l, int r){
tree[t].l = l;
tree[t].r = r;
tree[t].flag = ;
tree[t].len = ;
//这两个是代表真实的值
tree[t].ll = y[l];
tree[t].rr = y[r];
if ((l + ) == r) return;
//左闭右开
int mid = (l + r)>>;
build((t<<), l, mid);
build((t<<) | , mid, r);
}
void init(){
Map.clear();;
line.clear();
y.clear();
data.clear();
}
}A;
int n; void init(){
A.init();
for (int i = ; i <= n; i++){
double x1, x2, y1, y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
A.line.push_back(Line(x1, y1, y2, ));//1代表入线
A.line.push_back(Line(x2, y1, y2, -));
A.data.push_back(y1);
A.data.push_back(y2);
}
int cnt = ;//离散化
sort(A.data.begin(), A.data.end());
for (int i = ; i < A.data.size(); i++){
if (i == || A.data[i] != A.data[i - ]){
A.Map[A.data[i]] = cnt++;
A.y.push_back(A.data[i]);
}
}
}
void work(){
sort(A.line.begin(), A.line.end());
A.build(, , A.y.size() - );
double Ans = ;
for (int i = ; i < A.line.size(); i++){
if (i != ) Ans += (A.line[i].x - A.line[i - ].x) * A.tree[].len;
A.update(, A.Map[A.line[i].y1], A.Map[A.line[i].y2], A.line[i].flag);
}
printf("Total explored area: %.2lf\n\n" , Ans);
} int main(){ int t = ;
while (scanf("%d", &n) && n){
printf("Test case #%d\n", ++t);
init();
work();
}
return ;
}
【POJ1151】【扫描线+线段树】Atlantis的更多相关文章
- hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积
题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...
- HDU 3642 - Get The Treasury - [加强版扫描线+线段树]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- 【BZOJ3958】[WF2011]Mummy Madness 二分+扫描线+线段树
[BZOJ3958][WF2011]Mummy Madness Description 在2011年ACM-ICPC World Finals上的一次游览中,你碰到了一个埃及古墓. 不幸的是,你打开了 ...
- HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...
- 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树
题目描述 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 输入 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一个整数Q,表示询问数 ...
- P3722 [AH2017/HNOI2017]影魔(单调栈+扫描线+线段树)
题面传送门 首先我们把这两个贡献翻译成人话: 区间 \([l,r]\) 产生 \(p_1\) 的贡献当且仅当 \(a_l,a_r\) 分别为区间 \([l,r]\) 的最大值和次大值. 区间 \([l ...
- ACM学习历程—POJ1151 Atlantis(扫描线 && 线段树)
Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...
- Poj1151&HDU1542 Atlantis(扫描线+线段树)
题意 给定\(n\)个矩形\((x_1,y_1,x_2,y_2)\),求这\(n\)个矩形的面积并 题解 扫描线裸题,可以不用线段树维护,\(O(n^2)\)是允许的. #include < ...
- poj1151 Atlantis——扫描线+线段树
题目:http://poj.org/problem?id=1151 经典的扫描线问题: 可以用线段树的每个点代表横向被矩形上下边分割开的每一格,这样将一个矩形的出现或消失化为线段树上的单点修改: 每个 ...
- 矩形面积并-扫描线 线段树 离散化 模板-poj1151 hdu1542
今天刚看到这个模板我是懵逼的,这个线段树既没有建树,也没有查询,只有一个update,而且区间成段更新也没有lazy标记....研究了一下午,我突然我发现我以前根本不懂扫描线,之所以没有lazy标记, ...
随机推荐
- (转载)Mac和iOS开发资源汇总—更新于2013-07-19
(转载)http://beyondvincent.com/2013/07/18/resources-for-mac-and-ios-developers/ 小引 本文主要汇集一些苹果开发的资源,会经常 ...
- 搜索(剪枝优化):HDU 5113 Black And White
Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...
- 跨浏览器读取XML
这里跨浏览器,但是只能读取字符串XML文档,可以通过Ajax方式load一个XML文档,将文件XML转变为字符串 // 跨浏览器返回XML DOM对象 function getXMLDOM(xmlSt ...
- Unity Skin Shader Optimized
Shader "Skin Shader" { Properties { _MainTex ("Diffuse (RGB)", 2D) = "white ...
- nginx主配置文件 在那找怎么打开
- 计算1到n整数中,字符ch出现的次数
个位ch个数 + 十位ch个数 * 10 + 百位ch个数 * 100:同时如果某一位刚好等于ch,还需要减去多算的一部分值. #include <stdio.h> //整数1到n,字符c ...
- Using QEMU for Embedded Systems Development
http://www.opensourceforu.com/2011/06/qemu-for-embedded-systems-development-part-1/ http://www.opens ...
- 自动布局AutoLayout
1:理解概念 Auto Layout 中文翻译过来意思是 自动布局 ,通过内定的 Constraint (约束)和各项条件来计算出合理的布局.而这个合理的布局,符合我们的的预期和意图. 将我们想象中的 ...
- cmd的copy命令合并多个文件
1.1个a.jpg文件:1个b.php文件(一句话木马)
- php操作Memcache示例
<?php //==============================实例化============================ $mem=new Memcache; //====== ...