Atlantis

题目连接

http://poj.org/problem?id=1151

Description

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

1000000000.

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

HINT

题意

给你N个矩形,求矩形相交的面积

题解:

线段树,扫描线模板题

具体请看这一篇博文:http://blog.csdn.net/shiqi_614/article/details/6821814
有一个很坑的地方是 printf("Total explored area: %.2f\n\n", ans);
我一开始写的printf("Total explored area: %.2lf\n\n", ans);wa了好久。。。我也不知道为什么,如果有大佬知道欢迎指教。
 
有一天 OYJY大佬告诉我这是因为POJ评测机有小脾气。

代码:

 //#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 10050
int n,cnt,tot,cas;
double kth[N];
struct Query
{
double l,r,h; int id;
bool operator <(const Query&b)const
{return h<b.h;}
}que[N<<];
struct Tree{int l,r,lazy;double sum;}tr[N<<];
template<typename T>void read(T&x)
{
int k=;char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if(c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void push_up(int x)
{
double len=(kth[tr[x].r]-kth[tr[x].l]);
tr[x].sum=;
if (tr[x].lazy>)tr[x].sum=len;
else if(tr[x].r-tr[x].l>)tr[x].sum=tr[x<<].sum+tr[x<<|].sum;
}
void bt(int x,int l,int r)
{
tr[x]=Tree{l,r,,};
if(r-l==)return;
int mid=(l+r)>>;
bt(x<<,l,mid);
bt(x<<|,mid,r);
}
void update(int x,int l,int r,int tt)
{
if (l<=tr[x].l&&tr[x].r<=r)
{
tr[x].lazy+=tt;
push_up(x);
return;
}
int mid=(tr[x].l+tr[x].r)>>;
if(l<mid)update(x<<,l,r,tt);
if(mid<r)update(x<<|,l,r,tt);
push_up(x);
}
double query(int x,int l,int r)
{
if (l<=tr[x].l&&tr[x].r<=r) return tr[x].sum;
int mid=(tr[x].l+tr[x].r)>>;
double ans=;
if(l<mid)ans+=query(x<<,l,r);
if(mid<r)ans+=query(x<<|,l,r);
return ans;
}
void input()
{
read(n);
if (n==)exit();
double x1,y1,x2,y2;
for(int i=;i<=n;i++)
{
//read(x1);read(y1); read(x2);read(y2);
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
que[++tot]=Query{x1,x2,y1,};
que[++tot]=Query{x1,x2,y2,-};
kth[++cnt]=x1;kth[++cnt]=y1;
kth[++cnt]=x2;kth[++cnt]=y2;
}
}
void work()
{
double ans=;
sort(que+,que+tot+);
sort(kth+,kth+cnt+);
cnt=unique(kth+,kth+cnt+)-kth-;
bt(,,cnt);
for(int i=;i<=tot-;i++)
{
int l=lower_bound(kth+,kth+cnt+,que[i].l)-kth;
int r=lower_bound(kth+,kth+cnt+,que[i].r)-kth;
update(,l,r,que[i].id);
ans+=tr[].sum*(que[i+].h-que[i].h);
}
//printf("Test case #%d\nTotal explored area: %.2lf\n\n",++cas,ans);
printf("Test case #%d\n", ++cas);
printf("Total explored area: %.2f\n\n", ans);
}
void clear(){cnt=;tot=;}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
while()
{
clear();
input();
work();
}
}

POJ 1151 Atlantis 矩形面积求交/线段树扫描线的更多相关文章

  1. 51nod 1206 Picture 矩形周长求并 | 线段树 扫描线

    51nod 1206 Picture 矩形周长求并 | 线段树 扫描线 #include <cstdio> #include <cmath> #include <cstr ...

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

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

  3. poj 1151(离散化+矩形面积并)

    题目链接:http://poj.org/problem?id=1151 关于离散化,这篇博客讲的很好:http://www.cppblog.com/MiYu/archive/2010/10/15/12 ...

  4. hdu1542 矩形面积并(线段树+离散化+扫描线)

    题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...

  5. POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/POJ-1177 A number of rectangular posters, photographs and other pict ...

  6. POJ 2482 Stars in Your Window (线段树+扫描线+区间最值,思路太妙了)

    该题和 黑书 P102 采矿 类似 参考链接:http://blog.csdn.net/shiqi_614/article/details/7819232http://blog.csdn.net/ts ...

  7. 求矩形的周长(线段树+扫描线) Picture POJ - 1177

    题目链接:https://cn.vjudge.net/problem/POJ-1177 题目大意:求矩形外部的周长 具体思路:借用一下bin巨的一张图片. 我们按照y周从下往上的扫描线进行扫描,第一下 ...

  8. 覆盖的面积 HDU - 1255 线段树+扫描线+离散化 求特定交叉面积

    #include<cstdio> #include<map> #include<algorithm> using namespace std; ; struct N ...

  9. poj 2482 Stars in Your Window (线段树扫描线)

    题目大意: 求一个窗体覆盖最多的星星的权值. 思路分析: 每个星星看成 左下点为x y 右上点为x+w-1 y+h-1 的矩形. 然后求出最大覆盖的和. #include <cstdio> ...

随机推荐

  1. cocos2d-x学习笔记--第一天记录

    1.环境安装 http://www.cocos2d-x.org/ ---下载2.2.3--解压 https://www.python.org/ ---2.7.6 系统环境变量 设置安装目录 2创建一个 ...

  2. 网络I/O模型--07Netty基础

    Netty 是由 JBOSS 提供的一个 Java 开源框架. Netty 提供异步的.事件驱动的网络应用程序框架和工具 ,用以快速开发高性能 . 高可靠性的网络服务器和客户端程序.      Net ...

  3. L3(SP+OO+UT)能力评估

  4. linux系统下安装ssl证书(tomcat)

    1.申请ssl证书 2.下载ssl证书 打开此网址  https://myssl.com/cert_convert.html 将证书文件(xxx.com.crt)和密钥文件上传(xxx.com.key ...

  5. LeetCode 题解之Plus One

    1.题目描述 2.题目分析 从后向前做加法,等于10则进位,否则直接加1 ,返回 digits; 3.代码 vector<int> plusOne(vector<int>&am ...

  6. [翻译] REMenu

    REMenu https://github.com/romaonthego/REMenu Dropdown menu inspired by Vine. 一款下拉菜单效果控件,灵感来自于Vine. R ...

  7. 深入浅出TFS——创建WorkItem

    使用场景:在项目交付流程当中,我们在准备部署项目的时候,比如需要依次部署到DEV.QA和PROD共3个不同的环境.我们需要由专门负责部署的部门Integration Team的人员来操作.这个时候我们 ...

  8. September 03rd 2017 Week 36th Sunday

    What does it profit a man if he gains the whole world and loses his own soul? 失去灵魂,赢得世界又如何? It matte ...

  9. 安装64位php开发环境

    最近听说PHP5.4速度很快,所以想建立一个本地环境测试下.我打算用本地windows xp sp3下安装PHP5.4.8.Apache2.4.3和Mysql5.5.28. 首先去下载PHP.Apac ...

  10. [2018HN省队集训D1T3] Or

    [2018HN省队集训D1T3] Or 题意 给定 \(n\) 和 \(k\), 求长度为 \(n\) 的满足下列条件的数列的数量模 \(998244353\) 的值: 所有值在 \([1,2^k)\ ...