【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标记, ...
随机推荐
- lemon OA 下阶段工作安排
lemon OA 下阶段工作安排 经验总结 lemon OA系统作为一个中型的java web系统,在架构上还是有着很好地可学习的地方.但是由于经验不足,过程比较迂回.如果真的有经验的话,应该可以做到 ...
- Linux下高效编写Shell——shell特殊字符汇总
Linux下无论如何都是要用到shell命令的,在Shell的实际使用中,有编程经验的很容易上手,但稍微有难度的是shell里面的那些个符号,各种特殊的符号在我们编写Shell脚本的时候如果能够用的好 ...
- Linux下把U盘格式化为fat32
在linux下也是支持fat32的,如果U盘中了病毒可以插入linux系统进行格式化比较安全,下面介绍如何在linux下把u盘格式化为fat32的方法 一.执行fdisk -l查看linux设备,我的 ...
- (转载)Mac和iOS开发资源汇总—更新于2013-07-19
(转载)http://beyondvincent.com/2013/07/18/resources-for-mac-and-ios-developers/ 小引 本文主要汇集一些苹果开发的资源,会经常 ...
- HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)
Auxiliary Set Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- Tcxtreelist动态控制列或行是否能够编辑
procedure Tfrmaaa.grd1Editing(Sender: TObject; AColumn: TcxTreeListColumn; var Allow: Boolean);begin ...
- Java---网络蜘蛛-网页邮箱抓取器~源码
刚刚学完Socket,迫不及待的做了这个网页邮箱抓取~~~ 现在有越来越多的人热衷于做网络爬虫(网络蜘蛛),也有越来越多的地方需要网络爬虫,比如搜索引擎.资讯采集.舆情监测等等,诸如此类.网络爬虫涉及 ...
- JavaScript高级程序设计11.pdf
与操作字符串有关的最后一个方法是localeCompare(),这个方法比较两个字符串,按照字符串的字母表中的位置分别返回-1,0,1 var stringValue="yellow&quo ...
- ADO数据库链接
一.数据库操作准备 // --------------------------------------------------------------------------------------- ...
- 全国各城市Uber客服联系方式(电话、邮箱、微博)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...