POJ 1151 Atlantis 矩形面积求交/线段树扫描线
Atlantis
题目连接
http://poj.org/problem?id=1151
Description
Input
The input file is terminated by a line containing a single 0. Don't process it.
1000000000.
Output
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
10 10 20 20
15 15 25 25.5
0
Sample Output
Total explored area: 180.00
HINT
题意
给你N个矩形,求矩形相交的面积
题解:
线段树,扫描线模板题
代码:
//#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 矩形面积求交/线段树扫描线的更多相关文章
- 51nod 1206 Picture 矩形周长求并 | 线段树 扫描线
51nod 1206 Picture 矩形周长求并 | 线段树 扫描线 #include <cstdio> #include <cmath> #include <cstr ...
- 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)
[题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...
- poj 1151(离散化+矩形面积并)
题目链接:http://poj.org/problem?id=1151 关于离散化,这篇博客讲的很好:http://www.cppblog.com/MiYu/archive/2010/10/15/12 ...
- hdu1542 矩形面积并(线段树+离散化+扫描线)
题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...
- POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/POJ-1177 A number of rectangular posters, photographs and other pict ...
- POJ 2482 Stars in Your Window (线段树+扫描线+区间最值,思路太妙了)
该题和 黑书 P102 采矿 类似 参考链接:http://blog.csdn.net/shiqi_614/article/details/7819232http://blog.csdn.net/ts ...
- 求矩形的周长(线段树+扫描线) Picture POJ - 1177
题目链接:https://cn.vjudge.net/problem/POJ-1177 题目大意:求矩形外部的周长 具体思路:借用一下bin巨的一张图片. 我们按照y周从下往上的扫描线进行扫描,第一下 ...
- 覆盖的面积 HDU - 1255 线段树+扫描线+离散化 求特定交叉面积
#include<cstdio> #include<map> #include<algorithm> using namespace std; ; struct N ...
- poj 2482 Stars in Your Window (线段树扫描线)
题目大意: 求一个窗体覆盖最多的星星的权值. 思路分析: 每个星星看成 左下点为x y 右上点为x+w-1 y+h-1 的矩形. 然后求出最大覆盖的和. #include <cstdio> ...
随机推荐
- html History API
History api 兼容性支持一下浏览器 为什么要使用History API 在AJAX给我们带来提高用户体验.减少HTTP连接数等好处的同时,也渐渐显露出一些不足之处,比如: 1.页面全是用aj ...
- mac os idea的快捷键
全局搜索:shift+command+f 搜索类:command+o 光标向前向后移动:command+option+(左/右) 删除一行: command+delete
- Android属性动画简单剖析
运行效果图: 先看布局文件吧,activity_main.xml: <?xml version="1.0" encoding="utf-8"?> & ...
- fuzz实战之honggfuzz
Honggfuzz实战 前言 本文介绍 libfuzzer 和 afl 联合增强版 honggfuzz .同时介绍利用 honggfuzz 来 fuzz 网络应用服务. 介绍 honggfuzz 也是 ...
- ReactNative应用<DCL每日查看>开发总结
App效果: 功能和交互简单描述: 针对微信使用用户每天的零碎时间来进行天气,新闻要点等查看,免去了打开其他App来查看; 针对每一天可以设置一项重要任务计划,可开启通知提醒,让每一天任务简化,免去太 ...
- zookeeper应用 - 监控
服务器端:监听zk上父节点的子节点变化 package monitor; import java.util.List; import java.util.concurrent.CountDownLat ...
- CSS 实例之文字的凸起与凹陷
一些页面会有一些凹凸文字效果,这个主要是设置背景+文字阴影来达成这个效果的.文字阴影使用方法如下: text-shadow: 水平位置 垂直位置 模糊距离 阴影颜色 具体代码如下: body { ba ...
- 选择 Java 编写 iOS 与 安卓 App的八大理由
[编者按]本文作者为 RoboVM 的 CEO 兼联合创始人 Henric Müller,主要介绍选用 Java 编写移动应用的八大理由.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 在过 ...
- ARM CPU 架构
1978年12月5日,物理学家赫尔曼·豪泽(Hermann Hauser)和工程师Chris Curry,在英国剑桥创办了CPU公司(Cambridge Processing Unit),主要业务是为 ...
- Mysql使用binlog恢复数据解决误操作问题的两种方法
为保证没有其他参数配置影响,重新安装配置了一台最小化安装的CentOS7虚拟机 1. 基础知识 安装mysql5.6数据库Mysql binlog初步理解 2. 配置mysql 开启binlog.修 ...