HDU 1542 Atlantics 线段树+离散化扫描
将 x 轴上的点进行离散化,扫描线沿着 y 轴向上扫描
每次添加一条边不断找到当前状态有效边的长度 , 根据这个长度和下一条边形成的高度差得到一块合法的矩形的面积
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int MAX=+;
int flag[MAX<<];//记录某个区间的下底边个数
double sum[MAX<<];//记录某个区间的下底边总长度
double x[MAX];//对x进行离散化,否则x为浮点数且很大无法进行线段树 struct Seg{//线段
double x1,x2,y;
int d;
Seg(){}
Seg(double x1 , double x2 , double y , int d):x1(x1),x2(x2),y(y),d(d){}
bool operator<(const Seg &a)const{
return y<a.y;
}
}s[MAX]; void updateCur(int n,int left,int right){
if(flag[n]) sum[n] = x[right+] - x[left];//表示该区间整个线段长度可以作为底边
else if(left == right) sum[n] = ;//叶子结点则底边长度为0(区间内线段长度为0)
else sum[n] = sum[n<<] + sum[n<<|];
} void update(int o , int l , int r , int s , int t , int d){
if(s<=l && r<=t){//该区间是当前扫描线段的一部分,则该区间下底边总长以及上下底边个数差更新
flag[o] += d;//更新底边相差差个数
updateCur(o , l , r);//更新底边长
return;
}
int mid = (l + r)>> , ls = o<< , rs = o<<|;
if(mid >= s) update(ls , l , mid , s , t , d);
if(mid+ <= t) update(rs , mid+ , r , s , t , d);
updateCur(o , l , r);
} int bin_search(double key , int n){
int l = , r = n-;
while(l<=r){
int mid = (l+r) >>;
if(x[mid] == key) return mid;
if(x[mid] > key) r=mid-;
else l=mid+;
}
return -;
} int main()
{
// freopen("a.in" , "r" , stdin);
int n,cas=;
double x1,x2,y1,y2;
while(scanf("%d" , &n) , n){
int k=;
for(int i=;i<n;++i){
cin>>x1>>y1>>x2>>y2;
x[k]=x1;
s[k++]=Seg(x1,x2,y1,);
x[k]=x2;
s[k++]=Seg(x1,x2,y2,-);
}
sort(x,x+k);
sort(s,s+k);
int t = unique(x , x+k) - x;
double ans=; for(int i=;i<k;++i){//扫描线段
int pos1 = bin_search(s[i].x1 , t);
/*
这里因为是把它理解成合法线段,所以最右端第 i 个点 , 对应第 i - 1条边
同样道理,在updateCur中,计算有效线段长度时 , 需要x[r+1]
*/
int pos2 = bin_search(s[i].x2 , t)-;
update( , , t- , pos1 , pos2 , s[i].d);//扫描线段时更新底边长度和底边相差个数
ans += sum[]*(s[i+].y-s[i].y);//新增加面积
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",++cas,ans);
}
return ;
}
HDU 1542 Atlantics 线段树+离散化扫描的更多相关文章
- POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...
- HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- hdu 1542 Atlantis(线段树,扫描线)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 4325-Flowers(线段树+离散化)
题意: 给出每个花开花的时间段,每询问一个时间点输出该时间点开花的数量 分析: 线段树的区间更新,单点查询,但发现时间很大,没法存区间,就想到了离散化. 离散化就是把要处理的数据统一起来重新标号. # ...
- HDU 1542 Atlantis(线段树面积并)
描述 There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. S ...
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
- hdu 1542 Atlantis (线段树扫描线)
大意: 求矩形面积并. 枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上. #include <iostream> #inclu ...
- Atlantis HDU - 1542 (线段树扫描线)
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
随机推荐
- C头文件中尖括号与双引号的区别及编译搜索顺序
这两天被问到一个很有意思的问题:C头文件中尖括号与双引号有什么区别,以前只大约知道 <> 常用在系统库文件,"" 常用在自定义的借口文件中,那具体在gcc编译搜索过程中 ...
- CentOS 7 配置 Nginx 正向代理 http、https 最详解
手头项目中有使用到 nginx,因为使用的三方云服务器,想上外网需要购买外网IP的,可是有些需要用到外网却不常用的主机也挂个外网IP有点浪费了,便想使用nginx的反向代理来实现多台内网服务器使用一台 ...
- 自动生成 html5 小页面
StringBuilder htmltext = new StringBuilder(); try { //var readP ...
- 300 Longest Increasing Subsequence 最长上升子序列
给出一个无序的整形数组,找到最长上升子序列的长度.例如,给出 [10, 9, 2, 5, 3, 7, 101, 18],最长的上升子序列是 [2, 3, 7, 101],因此它的长度是4.因为可能会有 ...
- [转]position:fixed; 在IE9下无效果的问题
本文转自:http://www.cnblogs.com/xinwang/archive/2013/04/06/3002384.html 平常DIV+CSS布局时,position属性一般用absoul ...
- js易混API汇总
一:slice()方法 ————————————http://www.w3school.com.cn/jsref/jsref_slice_string.asp ———————————————————— ...
- JOptionPane.showMessageDialog出现在浏览器下面的解决方法
将JOptionPane.showMessageDialog(null, result, "发布公告:", JOptionPane.INFORMATION_MESSAGE);中的参 ...
- python实现qq机器人qqbot
title: python实现qq机器人qqbot tags: python date: 2018-6-1 10:19:00 --- 以下内容为转载 一.介绍 qqbot 是一个用 python 实现 ...
- Reuse a SSL socket
It's possible to reuse a SSL socket after proper cleanup. See SSL Socket free and shutdown on stacko ...
- Hive DDL&DML
1.删除分区 ALTER TABLE table_name DROP IF EXISTS PARTITION(dt=') 如果是外部表,记得rm对应文件 2.添加分区 ALTER TABLE tabl ...