hdu1542(线段树——矩形面积并)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1542
分析:离散化+扫描线+线段树
#pragma comment(linker,"/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 10007
#define inf 0x3f3f3f3f
#define N 2015
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
struct line
{
double l,r,h;
int d;
line(){}
line(double l,double r,double h,int d):l(l),r(r),h(h),d(d){}
bool operator<(const line &a)const
{
return h<a.h;
}
}s[N];
double sum[N<<],has[N];
int col[N<<];
void Pushup(int l,int r,int rt)
{
if(col[rt])sum[rt]=has[r+]-has[l];//每个点表示的是has[i]~has[i+1]的长度,故has[r+1]-has[l]
else if(l==r)sum[rt]=;//节点没有子节点加上来,故清零
else sum[rt]=sum[rt<<]+sum[rt<<|];//加上子孙节点被覆盖着的线段长度
}
void update(int L,int R,int d,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[rt]+=d;
Pushup(l,r,rt);
return;
}
int m=(l+r)>>;
if(L<=m)update(L,R,d,lson);
if(m<R)update(L,R,d,rson);
Pushup(l,r,rt);
}
int bin(double key,double a[],int n)
{
int l=,r=n-;
while(l<=r)
{
int m=(l+r)>>;
if(a[m]==key)return m;
if(a[m]>key)r=m-;
else l=m+;
}
return -;
}
int main()
{
int n,cas=;
double x1,y1,x2,y2;
while(scanf("%d",&n)&&n)
{
int k=;
for(int i=;i<n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
has[k]=x1;
s[k++]=line(x1,x2,y1,);
has[k]=x2;
s[k++]=line(x1,x2,y2,-);
}
sort(s,s+k);
sort(has,has+k);
int m=;
for(int i=;i<k;i++)//去重
if(has[i]!=has[i-])has[m++]=has[i];
double ans=;
for(int i=;i<k;i++)
{
int L=bin(s[i].l,has,m);
int R=bin(s[i].r,has,m)-;
update(L,R,s[i].d,,m-,);//区间更新
ans+=sum[]*(s[i+].h-s[i].h);//sum[1]表示被覆盖着的总长度
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",cas++,ans);
}
}
hdu1542(线段树——矩形面积并)的更多相关文章
- HDU 1542:Atlantis(扫描线+线段树 矩形面积并)***
题目链接 题意 给出n个矩形,求面积并. 思路 使用扫描线,我这里离散化y轴,按照x坐标从左往右扫过去.离散化后的y轴可以用线段树维护整个y上面的线段总长度,当碰到扫描线的时候,就可以统计面积.这里要 ...
- poj 1151 Atlantis (离散化 + 扫描线 + 线段树 矩形面积并)
题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 映射到y轴,并且记录下每个的y坐标,并对y坐标进行离散. 然后按照x从左向右扫描. #include <io ...
- hdu1255(线段树——矩形面积交)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题意:求N个矩形中,求被覆盖至少俩次的面积和 分析:覆盖两次即col[rt]>=2就好.一 ...
- hdu 1542 线段树扫描(面积)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...
- hdu1542 线段树扫描线求矩形面积的并
题意: 给你n个正方形,求出他们的所占面积有多大,重叠的部分只能算一次. 思路: 自己的第一道线段树扫描线题目,至于扫描线,最近会写一个总结,现在就不直接在这里写了,说下我的方 ...
- HDU - 1542 Atlantis(线段树求面积并)
https://cn.vjudge.net/problem/HDU-1542 题意 求矩形的面积并 分析 点为浮点数,需要离散化处理. 给定一个矩形的左下角坐标和右上角坐标分别为:(x1,y1).(x ...
- hdu 1255 覆盖的面积 (线段树处理面积覆盖问题(模板))
http://acm.hdu.edu.cn/showproblem.php?pid=1255 覆盖的面积 Time Limit: 10000/5000 MS (Java/Others) Memo ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并
D. Vika and Segments Vika has an infinite sheet of squared paper. Initially all squares are whit ...
随机推荐
- Nginx 负载均衡配置和策略
Nginx 的 HttpUpstreamModule 提供对后端(backend)server的简单负载均衡.一个最简单的 upstream 写法例如以下: upstream backend { se ...
- 百度地图SDK for Android v2.1.2全新发布
2013年6月20日 Android SDK:V2.1.2产品上线 新增: 自定义指南针位置(类:MapController,方法:setCompassMargin) 自定义当前位置图标(类:MyLo ...
- 出现Data Tools 与VS 不兼容问题
转载自:http://www.yishimei.cn/network/73.html 相信很多人都遇到了“此版本的SQL Server Data Tools与此计算机中安装的数据库运行时组件不兼容”这 ...
- CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别
http://blog.csdn.net/swimmer2000/archive/2007/10/30/1856213.aspx MFC(VC6.0)的CWnd及其子类中,有如下三个函数: / ...
- java大牛list
1 Java的未来 Java能干什么.不能干什么,一开始就要搞清楚.这对于成为一个纯种的Java程序猿至关重要. 2 构建Java运行环境 Java运行在服务器,服务器都是Linux系统,对于真正程序 ...
- 以&运行在后台的程序,关闭terminal后,相应进进程自动关闭
以&运行在后台的程序,关闭terminal后,相应进进程自动关闭
- 《Java从入门到精通》src9-25
find . -name *.java |xargs -i sh -c "echo {};cat {}" > ../all.java[op@TIM src]$ cat al ...
- android在其他线程中访问UI线程的方法
1.Activity.runOnUiThread( Runnable ) 2.View.post( Runnable ) 3.View.postDelayed( Runnable, long ) 4. ...
- Python IDLE 快捷键
Python IDLE 快捷键 编辑状态时: Ctrl + [ .Ctrl + ] 缩进代码 Alt+3 Alt+4 注释.取消注释代码行 Alt+5 Alt+6 切换缩进方式 空格<=> ...
- mysql dump 参数
mysql dump 参数: -R, --routines Dump stored routines (functions and procedures). 备份 函数和存储过程: -E, --eve ...