POJ 1151 线段树+扫描线
题意:求矩形面积的并
思路:
注意是[l,mid][mid,r] 这是真正的线段了
就当扫描线模板使吧~
//By SiriusRen
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define eps 1e-5
int N,tot,n,cases;
double y[666],ans=0;
struct Node{double x,y1,y2;int cover;}node[666];
struct Tree{double l,r,len;int cover;}tree[66666];
bool cmp(Node a,Node b){return a.x<b.x;}
void build(int l,int r,int pos){
tree[pos].l=y[l],tree[pos].r=y[r],tree[pos].len=tree[pos].cover=0;
if(l+1==r)return;
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
build(l,mid,lson),build(mid,r,rson);
}
void push_up(int l,int r,int pos){
if(tree[pos].cover>0)
tree[pos].len=tree[pos].r-tree[pos].l;
else if(l+1==r)tree[pos].len=0;
else tree[pos].len=tree[pos<<1].len+tree[pos<<1|1].len;
}
void update(int l,int r,int pos,Node jy){
if(tree[pos].l>=jy.y1&&tree[pos].r<=jy.y2){
tree[pos].cover+=jy.cover;push_up(l,r,pos);
return;
}
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
if(tree[lson].r>=jy.y2)update(l,mid,lson,jy);
else if(tree[rson].l<=jy.y1)update(mid,r,rson,jy);
else update(l,mid,lson,jy),update(mid,r,rson,jy);
push_up(l,r,pos);
}
int main(){
while(scanf("%d",&N)&&N){
ans=tot=0;
for(int i=1;i<=N;i++){
double X1,Y1,X2,Y2;
scanf("%lf%lf%lf%lf",&X1,&Y1,&X2,&Y2);
node[++tot].x=X1,node[tot].y1=Y1,node[tot].y2=Y2;node[tot].cover=1;y[tot]=Y1;
node[++tot].x=X2,node[tot].y1=Y1,node[tot].y2=Y2;node[tot].cover=-1;y[tot]=Y2;
}
sort(node+1,node+1+tot,cmp),sort(y+1,y+1+tot);
n=unique(y+1,y+1+tot)-y-1;
build(1,n,1),update(1,n,1,node[1]);
for(int i=2;i<=tot;i++){
ans+=tree[1].len*(node[i].x-node[i-1].x);
update(1,n,1,node[i]);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",++cases,ans);
}
}
POJ 1151 线段树+扫描线的更多相关文章
- POJ 1151 线段树+扫描线(计算矩形面积并)
前一篇博客有了讲解就不再叙述了 #include<cstdio> #include<cstring> #include<cmath> #include<ios ...
- POJ 3277 线段树+扫描线
题意: 思路: 线段树求矩形面积的并...同 POJ 1151 //By SiriusRen #include <cstdio> #include <algorithm> us ...
- 线段树 扫描线 L - Atlantis HDU - 1542 M - City Horizon POJ - 3277 N - Paint the Wall HDU - 1543
学习博客推荐——线段树+扫描线(有关扫描线的理解) 我觉得要注意的几点 1 我的模板线段树的叶子节点存的都是 x[L]~x[L+1] 2 如果没有必要这个lazy 标志是可以不下传的 也就省了一个pu ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]
题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...
- 【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],对于每一个查 ...
- 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 ...
随机推荐
- hdoj 1013Digital Roots
/*Digital Roots Problem Description The digital root of a positive integer is found by summing th ...
- 数组溢界地址的正确使用: 即 int a[6] 中的 a[-1] 和 a[6] 正确使用
正如大家所知道的那样: 数组 int a[6] , 编译器阅读到这句数组定义,会为分配6个int 类型的地址:a[0] a[1] a[2] a[3] a[4] a[5].我们 能够正 ...
- bzoj1212: [HNOI2004]L语言(字典树)
1212: [HNOI2004]L语言 题目:传送门 题解: 看完题目之后就觉得可以暴力在字典树上之间询问,一开始还傻了以为用文章来建,肯定用单词啊: 那么我们可以用一个v数组表示当前字符串1~i的区 ...
- 89.[NodeJS] Express 模板传值对象app.locals、res.locals
转自:https://blog.csdn.net/Elliott_Yoho/article/details/53537437 locals是Express应用中 Application(app)对象和 ...
- su和sudo的区别与使用,su命令,linux命令
su和sudo的区别与使用 一. 使用 su 命令临时切换用户身份 1. su 的适用条件和威力 su命令就是切换用户 的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要添加用户任务, ...
- xBIM 高级03 更改日志创建
系列目录 [已更新最新开发文章,点击查看详细] 模型中发生的每一个变化都是事务的一部分,这是我们设计的核心.所有事务都是由 IModel 的实现创建的,并且从中被弱引用,因此当使用 using ...
- COGS 2479 奇怪的姿势卡♂过去 (bitset+折半)
思路: 此题显然是CDQ套CDQ套树套树 (然而我懒) 想用一种奇怪的姿势卡过去 就出现了以下解法 5w*5w/8的bitset hiahiahia 但是空间会爆怎么办啊- 折半~ 变成5w*2.5w ...
- VC6.0 设置动态链接库工程生成dll以及lib文件的位置
在"Projet"->"Settings..."的"Link"选项卡中 "Output file name"中设置 ...
- Git 连接细节
Git 连接细节 首先创建Git 账号 https://github.com/ 下载Git https://git-scm.com/downloads 新建远程仓库 SSH keys : 打开 Git ...
- 003.JMS概述
1. 基本概念 JMS:Java Message Service, Java消息服务,是Java EE中的一个技术. JMS规范:JMS定义了Java中访问消息中间件的接口,并没有给予实现,实现JMS ...