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 ...
随机推荐
- 玩转大数据:深入浅出大数据挖掘技术(Apriori算法、Tanagra工具、决策树)
一.本课程是怎么样的一门课程(全面介绍) 1.1.课程的背景 “大数据”作为时下最火热的IT行业的词汇,随之而来的数据仓库.数据分析.数据挖掘等等围绕大数据的商业价值的利用逐渐成为 ...
- iOS学习——ViewController(六)
ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁,ViewController管理应用中的众多视图. iOS的SDK中提供很多原生ViewControlle ...
- Keepalived安装工具
装keepalived前,要先检查主机上是否已经安装, ps -ef | grep keepalive 不检查的话.easy把前人装的东西覆盖掉,那么曾经弄的配置文件都没了比較麻烦. 下面都为root ...
- 简化ui文件转换写法
在命令行敲一串长的命令.枯燥麻烦. #coding:utf-8 import sys import os import subprocess if len(sys.argv) == 2: #节省输入, ...
- Android开发系列(二十八):使用SubMenu创建选项菜单
大部分手机上边都会有一个"MENU"键,在一个应用安装到手机上之后,能够通过"MENU"显示该应用关联的菜单. 可是,从Android 3.0開始,Androi ...
- Servlet的学习(二)
本篇接上一篇<Servlet的学习(一)>,讲述如何利用MyEclipse来创建web工程, 同时讲述如何在MyEclipse中配置Tomcat服务器. 在MyEclipse中,新建“We ...
- FZU 1894 (双端队列)
Problem 1894 志愿者选拔 Accept: 1166 Submit: 3683 Time Limit: 1500 mSec Memory Limit : 32768 KB Pr ...
- Java 的垃圾回收机制(转)
先看一段转载,原文出自 http://jefferent.iteye.com/blog/1123677 虚拟机中的共划分为三个代:年轻代(Young Generation).年老点(Old Gener ...
- 一起来开发Android的天气软件(三)——使用Volley实现网络通信
距离上一篇一起来开发Android天气软件二的时间又将近半个月了,之间一直由于有事而没有更新实在抱歉,近期会加快更新的步伐.争取在2015年到来前写完这系列的博文,上一章我们已经使用LitePal框架 ...
- Swift - 时间控制器NSTimer(每隔一定时间执行某个函数)
时间控制器NSTimer可以实现定时器功能,即每隔一定时间执行具体函数,可以重复也可以只执行一次. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 cl ...