Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 23181   Accepted: 8644

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 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

 
题目大意:给出一些矩形,算他们的面积并
试题分析:乍一看并不是用线段树,知道扫描线后就很好做了。
     扫描线是什么呢?
    扫描线可以从左到右或者从上到下(方向无所谓),就是从上开始扫,扫到一个矩形的开头那么这个矩形就可以进入我们的面积计算,扫到末尾就退出。
    这一过程我们就可以用线段树来维护,由于坐标可能有浮点数,所以就需要操作时离散化一下。
    这次没有写差分+离散化+线段树,差分要标记下传,直接储存也挺好做的

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=100001;
const int INF=999999;
int N,M;
double a[100001],b[100001];
int A[100001];
struct data2{
double sum,len;
int ct;
}tr[1000001];
int tmp,tmp2;
struct data{
double x,y1,y2;
bool end;
}poi[1000001];
int T; bool cmp(data a,data b){
return a.x<b.x;
}
void build(int l,int r,int rt){
tr[rt].ct=0;tr[rt].sum=0;
tr[rt].len=a[r]-a[l];
if(l+1>=r) return ;
int mid=(l+r)>>1;
build(l,mid,rt*2);
build(mid,r,rt*2+1);
return ;
}
void Update(int rt,int l,int r){
if(tr[rt].ct) tr[rt].sum=tr[rt].len;
else if(r-l>1) tr[rt].sum=tr[rt*2].sum+tr[rt*2+1].sum;
else tr[rt].sum=0;
return ;
}
void add(int l,int r,int rt,int L,int R){
if(L==l&&R==r){
tr[rt].ct++;
Update(rt,l,r);
return ;
}
int mid=(l+r)>>1;
if(L>=mid) add(mid,r,rt*2+1,L,R);
else if(R<=mid) add(l,mid,rt*2,L,R);
else{
add(mid,r,rt*2+1,mid,R);
add(l,mid,rt*2,L,mid);
}
Update(rt,l,r);
return;
}
void del(int l,int r,int rt,int L,int R){
if(L==l&&R==r){
tr[rt].ct--;
Update(rt,l,r);
return ;
}
int mid=(l+r)>>1;
if(L>=mid) del(mid,r,rt*2+1,L,R);
else if(R<=mid) del(l,mid,rt*2,L,R);
else{
del(mid,r,rt*2+1,mid,R);
del(l,mid,rt*2,L,mid);
}
Update(rt,l,r);
return;
} double ans;
int main(){
while(1){
N=read();
if(!N) break;
tmp2=tmp=0;double x1,x2,y1,y2;
for(int i=1;i<=N;i++){
cin>>x1>>y1>>x2>>y2;
poi[++tmp].x=x1;
poi[tmp].y1=y1;
poi[tmp].y2=y2;
poi[tmp].end=false;
poi[++tmp].x=x2;
poi[tmp].y1=y1;
poi[tmp].y2=y2;
poi[tmp].end=true;
}
for(int i=1;i<=tmp;i+=2){
a[tmp2]=b[tmp2]=poi[i].y1;tmp2++;
a[tmp2]=b[tmp2]=poi[i].y2;tmp2++;
}
sort(poi+1,poi+tmp+1,cmp);
sort(a,a+tmp2);
int tmp3=0,tmp4=0;
tmp3=unique(a,a+tmp2)-a;
ans=0;
build(0,tmp3-1,1);
double lasth;
for(int i=1;i<tmp;i++){
if(!poi[i].end) add(0,tmp3-1,1,lower_bound(a,a+tmp3,poi[i].y1)-a,lower_bound(a,a+tmp3,poi[i].y2)-a);
else del(0,tmp3-1,1,lower_bound(a,a+tmp3,poi[i].y1)-a,lower_bound(a,a+tmp3,poi[i].y2)-a);
ans+=(double)((poi[i+1].x-poi[i].x)*tr[1].sum);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",++T,ans);
}
}

  

【线段树】Atlantis的更多相关文章

  1. 线段树---Atlantis

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/A Description There are se ...

  2. hdu 1542 Atlantis(线段树,扫描线)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  3. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  4. POJ 1542 Atlantis(线段树 面积 并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 参考网址:http://blog.csdn.net/sunmenggmail/article/d ...

  5. 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)

    [题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...

  6. 【POJ1151】Atlantis(线段树,扫描线)

    [POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...

  7. hdu1542 Atlantis 线段树--扫描线求面积并

    There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...

  8. HDU 1542 Atlantis(线段树面积并)

     描述 There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. S ...

  9. hdu1542 Atlantis (线段树+扫描线+离散化)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  10. HDU 1542 - Atlantis - [线段树+扫描线]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

随机推荐

  1. Linux系统网络基础知识及配置

    一:DNS(domain name system)简介 DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而 ...

  2. [bzoj4569][SCOI2016]萌萌哒-并查集+倍增

    Brief Description 一个长度为n的大数,用S1S2S3...Sn表示,其中Si表示数的第i位,S1是数的最高位,告诉你一些限制条件,每个条 件表示为四个数,l1,r1,l2,r2,即两 ...

  3. python面向对象进阶(下)

    一.item系列:就是把字典模拟成一个字典去操作(操作字典就用item的方式) obj[‘属性’]的方式去操作属性时触发的方法 __getitem__:obj['属性'] 时触发 __setitem_ ...

  4. 下载 LFS所需要的源码包的脚本程序及检验方法

    下载 LFS所需要的源码包的脚本程序及检验方法 http://blog.csdn.net/yygydjkthh/article/details/45315143

  5. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance(xsi:schemaLocation详解)

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"中xsi的意思是 :本xml文件中要用到某些来自xsi代表的“http:/ ...

  6. Python爬虫数据处理

    一.首先理解下面几个函数 设置变量 length()函数 char_length() replace() 函数 max() 函数1.1.设置变量 set @变量名=值 set @address='中国 ...

  7. [New learn]SDWebImage框架的基本使用

    代码:https://github.com/xufeng79x/SDWebImage 1.简介 SDWebImage是一个第三方框架,它能够帮助我们有效管理应用图片下载,沙盒保存和内存保存的任务.通过 ...

  8. 调用手机端硬件功能 汇总(android/ios) Native.js示例汇总

    Native.js示例汇总 NJS Native.JS 示例 Native.js虽然强大和开放,但很多web开发者因为不熟悉原生API而难以独立完成.这篇帖子的目的就是汇总各种写好的NJS代码,方便w ...

  9. 80端口被System进程占用问题

    更新: 有可能占用80端口的服务: 如果安装了IIS,关闭IIS: 如果未开启IIS功能,而安装了诸如Web Matrix的开发程序,则有可能被Web Development Agent Servic ...

  10. ES6 module语法加载 import export

    export:暴露,就是把接口暴露出去 import:引入,跟字面意思一样,引入接口 export {} export function demo(){} export var demo1; 这上面的 ...