Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12059    Accepted Submission(s): 5083

Problem Description
There
are several ancient Greek texts that contain descriptions of the fabled
island Atlantis. Some of these texts even include maps of parts of the
island. But unfortunately, these maps describe different regions of
Atlantis. Your friend Bill has to know the total area for which maps
exist. You (unwisely) volunteered to write a program that calculates
this quantity.
Input
The
input file consists of several test cases. Each test case starts with a
line containing a single integer n (1<=n<=100) of available maps.
The n following lines describe one map each. Each of these lines
contains four numbers x1;y1;x2;y2
(0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily
integers. The values (x1; y1) and (x2;y2) are the coordinates of the
top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don’t process it.
Output
For
each test case, your program should output one section. The first line
of each section must be “Test case #k”, where k is the number of the
test case (starting with 1). The second one must be “Total explored
area: a”, where a is the total explored area (i.e. the area of the union
of all rectangles in this test case), printed exact to two digits to
the right of the decimal point.
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
看了大神的解释 懂了一点 http://m.blog.csdn.net/article/details?id=52048323
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<queue>
#include<cstring>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
double x[*N];
struct Edge{
double l,r;
double h;
int flag;
}edge[*N];
struct node{
int l,r;
int s;
double len;
}tree[N*];
bool cmp(Edge x,Edge y){
return x.h<y.h;
}
void pushup(int pos){
if(tree[pos].s)tree[pos].len=x[tree[pos].r+]-x[tree[pos].l];
else if(tree[pos].l==tree[pos].r)tree[pos].len=;
else{
tree[pos].len=tree[pos<<].len+tree[pos<<|].len;
}
}
void build(int l,int r,int pos){
tree[pos].l=l;tree[pos].r=r;
tree[pos].s=;tree[pos].len=;
if(tree[pos].l==tree[pos].r)return;
int mid=(tree[pos].l+tree[pos].r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
}
void update(int l,int r,int pos,int xx){
if(tree[pos].l==l&&tree[pos].r==r){
tree[pos].s=tree[pos].s+xx;
pushup(pos);
return;
}
int mid=(tree[pos].l+tree[pos].r)>>;
if(r<=mid)update(l,r,pos<<,xx);
else if(l>mid)update(l,r,pos<<|,xx);
else{
update(l,mid,pos<<,xx);
update(mid+,r,pos<<|,xx);
}
pushup(pos);
}
int main(){
int n;
int tt=;
while(scanf("%d",&n)!=EOF){
tt++;
if(n==)break;
int t=;
for(int i=;i<n;i++){
double x1,x2,y1,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
Edge &t1=edge[t];Edge &t2=edge[t+];
t1.l=t2.l=x1;t1.r=t2.r=x2;
t1.h=y1;t2.h=y2;
t1.flag=;t2.flag=-;
x[t]=x1;x[t+]=x2;
t=t+;
}
sort(edge,t+edge,cmp);
sort(x,x+t);
int k=;
for(int i=;i<t;i++){
if(x[i]!=x[i-]){
x[k++]=x[i];
}
}
//cout<<1<<endl;
build(,k-,);
//cout<<2<<endl;
double ans=0.0;
//cout<<t<<endl;
for(int i=;i<t;i++){
int l=lower_bound(x,x+k,edge[i].l)-x;
int r=lower_bound(x,x+k,edge[i].r)-x-;
update(l,r,,edge[i].flag);
ans=ans+(edge[i+].h-edge[i].h)*tree[].len;
//cout<<ans<<endl;
}
printf("Test case #%d\n",tt);
printf("Total explored area: %.2f\n\n",ans);
//cout<<ans<<endl;
}
}

hdu 1542(线段树+扫描线 求矩形相交面积)的更多相关文章

  1. Atlantis HDU - 1542 线段树+扫描线 求交叉图形面积

    //永远只考虑根节点的信息,说明在query时不会调用pushdown //所有操作均是成对出现,且先加后减 // #include <cstdio> #include <cstri ...

  2. HDU 1828“Picture”(线段树+扫描线求矩形周长并)

    传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...

  3. hdu1542 线段树扫描线求矩形面积的并

    题意:       给你n个正方形,求出他们的所占面积有多大,重叠的部分只能算一次. 思路:       自己的第一道线段树扫描线题目,至于扫描线,最近会写一个总结,现在就不直接在这里写了,说下我的方 ...

  4. HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)

    传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...

  5. hdu 1542 线段树+扫描线 学习

    学习扫描线ing... 玄学的东西... 扫描线其实就是用一条假想的线去扫描一堆矩形,借以求出他们的面积或周长(这一篇是面积,下一篇是周长) 扫描线求面积的主要思想就是对一个二维的矩形的某一维上建立一 ...

  6. hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积

    题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...

  7. hdu1828 线段树扫描线求矩形面积的周长

    题意:       给你n个矩形,问你这n个矩形所围成的图形的周长是多少. 思路:       线段树的扫描线简单应用,这个题目我用的方法比较笨,就是扫描两次,上下扫描,求出多边形的上下边长和,然后同 ...

  8. UVA-11983-Weird Advertisement(线段树+扫描线)[求矩形覆盖K次以上的面积]

    题意: 求矩形覆盖K次以上的面积 分析: k很小,可以开K颗线段树,用sum[rt][i]来保存覆盖i次的区间和,K次以上全算K次 // File Name: 11983.cpp // Author: ...

  9. 2015 UESTC 数据结构专题E题 秋实大哥与家 线段树扫描线求矩形面积交

    E - 秋实大哥与家 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...

随机推荐

  1. 关于 WebView 的一些笔记

    什么是 WebView WebView 是手机中内置了一款高性能 webkit 内核浏览器,在 SDK 中封装的一个组件.没有提供地址栏和导航栏, WebView 只是单纯的展示一个网页界面.在开发中 ...

  2. Resources.getResourceAsReader 报空指针

    1.maven项目中 可能未编译 导致找不到, 解决方法:mvn clean install -DskipTests -X   编译一下项目 2.可能在 Resources.getResourceAs ...

  3. SQL查询性能优化

    使用高效的查询 使用 EXISTS 代替 IN -- 查询A表中同时存在B表的数据 -- 慢 SELECT * FROM Class_A WHERE id IN (SELECT id FROM Cla ...

  4. 如何使用 Python 创建一名可操控的角色玩家

    在 这个系列的第一篇文章 中,我解释了如何使用 Python 创建一个简单的基于文本的骰子游戏.在第二部分中,我向你们展示了如何从头开始构建游戏,即从 创建游戏的环境 开始.但是每个游戏都需要一名玩家 ...

  5. 微信小程序animation

    wxml <view class="background" animation="{{rotateData}}"> </view>< ...

  6. web前端学习总结--CSS

    CSS 什么是CSS? CSS 指层叠样式表(Cascading Style Sheets) 样式定义如何显示HTML元素 样式通常存储在样式表中 如何使用CSS 内联方式 样式定义在单个的HTML元 ...

  7. 使用IDM下载软件下载百度云网盘里的资源,以Chrome浏览器为例

    1.下载安装最新版的Chrome浏览器,我是在腾讯软件下载中心下载的,使用普通下载,下载地址:https://dl.softmgr.qq.com/original/Browser/72.0.3626. ...

  8. Eclipse集成Maven的Web工程demo(独立及Maven集成tomcat)

    用到的工具JDK1.8Eclipse Luna j2eeEclipse 集成的Mavetomcat7 (集成在xampp中的独立web服务器,或者通过Maven plugin集成web服务器) 步骤如 ...

  9. 【codeforces 514E】Darth Vader and Tree

    [题目链接]:http://codeforces.com/problemset/problem/514/E [题意] 无限节点的树; 每个节点都有n个儿子节点; 且每个节点与其第i个节点的距离都是ai ...

  10. noip模拟赛 逃避

    题目描述 给定一篇只含有大小写字母,空格以及 ′.′(不含引号)的长度为 L 的文章.文章被若干个 ′.′ 划分 成若干个句子,句子被若干个空格划分成单词.你需要将文章中每个句子第一个单词的首字母改成 ...