POJ1151+线段树+扫描线
/*
线段树+扫描线+离散化
求多个矩形的面积
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
using namespace std;
const int maxn = ;
const int maxm = ;
struct SegTree{
int l,r;
int cover;
double L_val,R_val;
double sum;
}ST[ maxm<< ];
struct Line{
double x,y1,y2;
bool InOut;
}yLine[ maxm ];
int cmp( Line a,Line b ){
return a.x<b.x;
}
double yIndex[ maxm ];
int GetIndex( double val,int cnt ){
return lower_bound( yIndex,yIndex+cnt,val )-yIndex;
} void build( int L,int R,int n ){
ST[ n ].l = L;
ST[ n ].r = R;
ST[ n ].cover = ;
ST[ n ].sum = ;
ST[ n ].L_val = yIndex[ L ];
ST[ n ].R_val = yIndex[ R ];
if( R-L> ){
int mid = (L+R)/;
build( L,mid,*n );
build( mid,R,*n+ );
}
}
void PushUp( int n ){
if( ST[ n ].cover> ){
ST[ n ].sum = ST[ n ].R_val-ST[ n ].L_val;
}
else if( ST[ n ].r-ST[ n ].l> ){
ST[ n ].sum = ST[ *n ].sum+ST[ *n+ ].sum;
}
else
ST[ n ].sum = ;
}
void update( int left,int right,bool InOut,int n ){
if( left==ST[ n ].l&&right==ST[ n ].r ){
if( InOut==true ){
ST[ n ].cover++;
}
else{
ST[ n ].cover--;
}
}
else {
int mid = (ST[ n ].l+ST[ n ].r)/;
if( mid>=right ) update( left,right,InOut,*n );
else if( mid<=left ) update( left,right,InOut,*n+ );
else {
update( left,mid,InOut,*n );
update( mid,right,InOut,*n+ );
}
}
PushUp( n );
} int main(){
int n;
int T = ;
while( scanf("%d",&n)==,n ){
printf("Test case #%d\n",T++);
double x1,y1,x2,y2;
int cnt = ;
for( int i=;i<n;i++ ){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
yLine[ *i ].x = x1;
yLine[ *i+ ].x = x2;
yLine[ *i ].y1 = yLine[ *i+ ].y1 = y1;
yLine[ *i ].y2 = yLine[ *i+ ].y2 = y2;
yLine[ *i ].InOut = true;
yLine[ *i+ ].InOut = false;
yIndex[ *i ] = y1;
yIndex[ *i+ ] = y2;
}
sort( yLine,yLine+*n,cmp );
sort( yIndex,yIndex+*n );
for( int i=;i<*n;i++ ){
if( yIndex[i]!=yIndex[i-] )
yIndex[ cnt++ ] = yIndex[ i- ];
}
yIndex[ cnt++ ] = yIndex[ *n- ];
build( ,cnt-, );
double res = ;
update( GetIndex( yLine[].y1,cnt ),GetIndex( yLine[].y2,cnt ),yLine[].InOut, );
for( int i=;i<*n;i++ ){
res += ST[ ].sum*( yLine[i].x-yLine[i-].x );
update( GetIndex( yLine[i].y1,cnt ),GetIndex( yLine[i].y2,cnt ),yLine[i].InOut, );
}
printf("Total explored area: %.2lf\n\n",res);
}
return ;
}
POJ1151+线段树+扫描线的更多相关文章
- Atlantis poj1151 线段树扫描线
Atlantis poj1151 线段树扫描线 题意 题目给了n个矩形,每个矩形给了左下角和右上角的坐标,矩形可能会重叠,求的是矩形最后的面积. 题解思路 这个是我线段树扫描线的第一题,听了学长的讲解 ...
- Atlantis(POJ1151+线段树+扫描线)
题目链接:http://poj.org/problem?id=1151 题目: 题意:求所有矩形的面积,重合部分只算一次. 思路:扫描线入门题,推荐几篇学扫描线的博客: 1.http://www.cn ...
- 【学习笔记】线段树—扫描线补充 (IC_QQQ)
[学习笔记]线段树-扫描线补充 (IC_QQQ) (感谢 \(IC\)_\(QQQ\) 大佬授以本内容的著作权.此人超然于世外,仅有 \(Luogu\) 账号 尚可膜拜) [学习笔记]线段树详解(全) ...
- 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- HDU 4419 Colourful Rectangle --离散化+线段树扫描线
题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...
- BZOJ-3228 棋盘控制 线段树+扫描线+鬼畜毒瘤
3228: [Sdoi2008]棋盘控制 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 23 Solved: 9 [Submit][Status][D ...
- BZOJ-3225 立方体覆盖 线段树+扫描线+乱搞
看数据范围像是个暴力,而且理论复杂度似乎可行,然后被卡了两个点...然后来了个乱搞的线段树+扫描线.. 3225: [Sdoi2008]立方体覆盖 Time Limit: 2 Sec Memory L ...
随机推荐
- 在swift中使用oc 的代码
就是需要一个桥文件, 方法一:在swift项目中,新建一个oc的类,这时候,会弹出一个对话框,你点默认的那个选项就行了.然后在新生成的桥文件中导入你所需要的oc代码的头文件就行了. 方法二:但是有时候 ...
- WaitForSingleObject用法
对应函数 编辑 VC声明 DWORD WaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds ); 参数 编辑 hHandle[in]对 ...
- 鼠标按键自定义软件-X-Mouse Button Control
转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/mouse-button-x-mouse-button-custom-software-control/ 说明 ...
- [EAP]将hostapd作为radius服务器搭建EAP认证环境
文章主要由以下几部分组成: 0.概念理解: WPA/WPA2,EAP,IEEE, 802.11i, WiFi联盟, 802.1x 1.编译hostapd 2.配置hostapd的conf文件 3.外接 ...
- qml实现窗口拖动
在去掉窗口标题栏后窗口会失去鼠标拖动效果,所以需要自己添加拖动效果. 实现代码: ApplicationWindow { id: mainWindow visible: true ...
- DTcms 导航选中样式以及简化方法
(建议使用方法2,执行效率略高) 一般用于导航不能循环输出的情况. 可以循环输出导航的情况直接用if判断即可. 首页模版中顶部,自定义c#代码. <%set string channel=&qu ...
- MySQL基础学习之函数
数学函数 绝对值 abs() 圆周率 PI() 平方根 sqrt() 模除取余 mod(被除数,除数) 随机数 rand() 四舍五入 round(数字) 次方 ...
- PHPCMS 错误日志 Only variables should be passed by ...
有几个网站是PHPCMS V9做的,但这两天发现一个问题,PHPCMS 的错误日志超过了20M ,后台报警,然后我看了下错误日志,其中两万多行都是一个错误,错误信息如下: 1 <?php exi ...
- WPF中的WebBrowser
MainWindow.xaml.cs //新窗口事件 { Uri uri = new Uri(textBox_uri.Text); System.Windows.Forms.Integration.W ...
- 代码规范-IAR设置
1.在IAR内定义 char 2. 去掉相关的告警 3.LANGUAGE设置