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. 那些年vue踩过的坑

    1.前言 学习Vue前端框架已经一个月了,作为一个web刚入门的菜鸟,在学习的过程中,网上有些技术博客往往没有什么可以借鉴的地方,在这里 我特意将我从开始一直到登录的过程记录下来.希望看到我的文章的朋 ...

  2. ExpandableListView控件实现二级列表

    效果图如下: 二级列表附有点击事件. 1.布局文件: 此处加了一个自定义的导航RelativeLayout,记得注activity的时候添加 android:theme="@style/Th ...

  3. Transformation functionality for the String class

    String类的转换功能: package com.itheima_05; /* * String类的转换功能: * char[] toCharArray():把字符串转换为字符数组 * String ...

  4. Python 3从入门到精通02-python的简单使用

    Python 3中的打印语句和字符串使用: Python中的常见数学运算: 这样的简单基础知识,你需要花5分钟就可以了,很基础的东西.

  5. 本地sql大文件导入数据库

    mysql中配置my.ini interactive_timeout = 120 wait_timeout = 120 max_allowed_packet = 32M 导入sql运行命令 sourc ...

  6. H5 高德地图获取当前位置信息

    返回结果:jsonp_393330_({"status":"1","info":"OK","infocode& ...

  7. MsSQL使用加密连接SSL/TLS

    说明 应用程序通过未加密的通道与数据库服务器通信, 这可能会造成重大的安全风险.在这种情况下, 攻击者可以修改用户输入的数据, 甚至对数据库服务器执行任意 SQL 命令. 例如,当您使用以下连接字符串 ...

  8. python离线安装外部依赖包

     1.制作requirement.txt pip freeze > requirement.txt 内网安装外部依赖包办法: 例如:安装pytest包得时候会顺带安装pytest依赖包 离线下载 ...

  9. 铁乐学Python_day08作业

    1. 文件a.txt内容:每一行内容分别为商品名字,价钱,个数. apple 10 3 tesla 100000 1 mac 3000 2 lenovo 30000 3 chicken 10 3 通过 ...

  10. Linux--安全加固02

    目录:1.BIOS2.SSH安全3.禁用telnet4.禁用代码编译5.ProFTP6.TCPwrappers7.创建一个SU组8.root通知9.history安全10.欢迎信息11.禁用所有特殊账 ...