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. 数据结构应用实例#栈&单链表#简易计算器

    修改BUG的时候一不小心BUG越修越多,鉴于维护程序并不是学习数据结构的初衷,我已经果断的弃坑了!! 以下内容再不更新,Github上的代码直接无法正常编译运行.... 参考参考就好,学习到栈的作用就 ...

  2. dubbo之主机绑定

    主机绑定 查找顺序 缺省主机 IP 查找顺序: 通过 LocalHost.getLocalHost() 获取本机地址. 如果是 127.* 等 loopback 地址,则扫描各网卡,获取网卡 IP. ...

  3. Java中IO对象的输入输出流

    输入流: public void inputDemo () throws IOException { //文件名称 String fileName = "d:\\aaa.txt"; ...

  4. 什么是ACID

    ACID是衡量事务的四个特性: 原子性(Atomicity,或称不可分割性) 一致性(Consistency) 隔离性(Isolation) 持久性(Durability) 原子性:原子性是指一个事务 ...

  5. php第十五节课

    租房表 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  6. Heaters (codeforces 1066B)

    贪心题 策略 在最左边设置一个array 0,每一次从右往左,如果有heater的话就寻找heater左边界是不是小于等于目前的上一个heater的右边界,如果没有一个这样的,那么就直接输出-1 代码 ...

  7. Linux之网络文件共享服务(NFS)

    概念: NFS:Network File System 网络文件系统,基于内核的文件系统.Sun公司开发,通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件,基于RPC(Remote ...

  8. UTL

    在PL/SQL中,UTL_FILE包提供文本文件输入和输出功能. 可以访问的目录通过初始化参数UTL_FILE_DIR设置. 注意:UTL_FILE只能读取服务器端文本文件,不能读取二进制文件.这时候 ...

  9. hdu 2782 dfs(限定)

    The Worm Turns Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  10. [数据结构]C#顺序表的实现

    在数据结构的学习当中,想必C++可能是大家接触最多的入门语言了 但是C#的数据结构却很少看到,今天我写了一个C#顺序表的顺序存储结构 顺序表是在计算机内存中以数组的形式保存的线性表,线性表的顺序存储是 ...