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> ...
随机推荐
- Dialog向Activity传递数据
PopupDialog中声明一个内部接口PriorityListener,接口中声明一个回调函数refreshPriorityUI,用于在 Dialog的监听事件触发后刷新Activity的UI显示. ...
- 抓取远程master分支到本地,并与UI分支合并
1.pull (1)UI: git add . git commit -m git checkout master (2)master: git pull origin master git ...
- js获取元素显示隐藏的当前状态
js获取元素显示隐藏的当前状态 // CSS var display = $("."+cls).css("display"); if(display == &q ...
- JS弹出div简单样式
<div id="dialog" style="display:none;z-index:9999;position: absolute;border:1px so ...
- 解决eclipse 文件更新不自动刷新的问题
打开eclipse 1. Window ===> Preferences ===> General ===> Workspace 2. 勾选 1> Refresh using ...
- C++ 的继承与虚函数 读书笔记
一.类与类之间关系: 1.类与类之间可能会存在共性. 2.类与类之间必定会有差异. 3.为也节约开发时间和代码量,我们在设计类时可以把类的共享抽象出来形成一个基础类(基类). 4.使用基类+差异生成一 ...
- C# 获取两个时间段之间的所有时间与获取当前时间所在的季度开始和结束时间
一:C# 获取两个时间段之间的所有时间 public List<string> GetTimeList(string rq1, string rq2) { List<string&g ...
- 铁乐学python_Day42_锁和队列
铁乐学python_Day42_锁和队列 例:多个线程抢占资源的情况 from threading import Thread import time def work(): global n tem ...
- SQL——快速定位相关的外键表
- windows 2012R2 上必须要用sharepoint 2013 sp1.
已经确认. 虽然有人讲以下powershell可以帮助安装sharepoint 2013. 不过不是每次都可以的 Import-Module ServerManager Add-WindowsFeat ...