求矩形的周长(线段树+扫描线) Picture POJ - 1177
题目链接:https://cn.vjudge.net/problem/POJ-1177
题目大意:求矩形外部的周长
具体思路:借用一下bin巨的一张图片。

我们按照y周从下往上的扫描线进行扫描,第一下,求出红色的部分的面积(x轴的长度+当前的y高度和上一个点的高度之差*2)。第二次,我们计算x轴的长度是
上一次被覆盖的x轴的长度-当前的被x轴覆盖的面积,这样的话,就可以避免重复的计算了。
AC代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <ctime>
#define ll long long
# define lson l,m,rt<<
# define rson m+,r,rt<<|
using namespace std;
const int maxn = +;
# define inf 0x3f3f3f3f
struct node
{
int l,r,h;
int d;
node() {}
node(int xx,int yy,int zz,int tt)
{
l=xx;
r=yy;
h=zz;
d=tt;
}
bool friend operator < (node t1,node t2)
{
return t1.h<t2.h;
}
} q[maxn<<];
int Hash[maxn];
int tree[maxn],lazy[maxn],cnt[maxn];
int lx[maxn],ly[maxn];
void up(int l,int r,int rt)
{
if(lazy[rt])
{
cnt[rt]=;
lx[rt]=;
ly[rt]=;
tree[rt]=Hash[r+]-Hash[l];
}
else if(l==r)
{
cnt[rt]=;
tree[rt]=;
lx[rt]=ly[rt]=;
}
else
{
cnt[rt]=cnt[rt<<]+cnt[rt<<|]-(lx[rt<<|]&ly[rt<<]);//这个地方注意是左边端点的右定点和右边端点的左定点,因为是需要判断区间是否有相交
tree[rt]=tree[rt<<]+tree[rt<<|];
lx[rt]=lx[rt<<],ly[rt]=ly[rt<<|];
}
}
void update(int l,int r,int rt,int L,int R,int p)
{
if(L<=l&&R>=r)
{
lazy[rt]+=p;
up(l,r,rt);
return ;
}
int m=(l+r)>>;
if(L<=m)
update(lson,L,R,p);
if(R>m)
update(rson,L,R,p);
up(l,r,rt);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int num=;
int x1,y1,x2,y2;
for(int i=; i<n; i++)
{
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
q[num]= {x1,x2,y1,};
Hash[num++]=x1;
q[num]= {x1,x2,y2,-};
Hash[num++]=x2;
}
sort(Hash,Hash+num);
sort(q,q+num);
int k=;
for(int i=; i<num; i++)
{
if(Hash[i]==Hash[i-])
continue;
Hash[k++]=Hash[i];
}
memset(cnt,,sizeof(cnt));
memset(lx,,sizeof(lx));
memset(ly,,sizeof(ly));
memset(tree,,sizeof(tree));
memset(lazy,,sizeof(lazy));
int ans=;
int len=;
for(int i=; i<num; i++)
{
int l=lower_bound(Hash,Hash+k,q[i].l)-Hash;
int r=lower_bound(Hash,Hash+k,q[i].r)-Hash-;
update(,k-,,l,r,q[i].d);
ans+=abs(len-tree[]);
len=tree[];
if(i<num-)
ans+=*(q[i+].h-q[i].h)*cnt[];
}
printf("%d\n",ans);
}
return ;
}
求矩形的周长(线段树+扫描线) Picture POJ - 1177的更多相关文章
- HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1255 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<= ...
- HDU3642 Get The Treasury —— 求矩形交体积 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-3642 Jack knows that there is a great underground treasury in a ...
- HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...
- POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/POJ-1177 A number of rectangular posters, photographs and other pict ...
- hdu1542 Atlantis 线段树--扫描线求面积并
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
- hdu1255 覆盖的面积 线段树-扫描线
矩形面积并 线段树-扫描线裸题 #include<stdio.h> #include<string.h> #include<algorithm> #include& ...
- 51nod 1206 Picture 矩形周长求并 | 线段树 扫描线
51nod 1206 Picture 矩形周长求并 | 线段树 扫描线 #include <cstdio> #include <cmath> #include <cstr ...
- HDU 1828“Picture”(线段树+扫描线求矩形周长并)
传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
- hdu 1828 Picture(线段树扫描线矩形周长并)
线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
随机推荐
- 12.15daily_scrum
新的阶段工作已经开始,本阶段我们小组的工作重心在于界面的优化和资源配置的整合,让用户产生更好的体验效果,有一些更加直观和便捷的应用功能展示,加以相应的辅助功能让新版本的笔记本软件更具竞争力和可用性. ...
- This is me
This is me 爱琴棋 爱书画 也爱格物 爱跋山 爱涉水 也爱深林 This is me. 刘伯承的诗词有曰“高耸入云”,于是“李入云”便成为了我一生的标记,也造就了一个时而安静,时而疯狂的我 ...
- Linux内核分析作业 NO.4
扒开系统调用的三层皮(上) 于佳心 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-100002900 ...
- linux内核分析--计算机是如何工作的
实验部分 使用gcc -S -o main.s main.c -m32命令将源代码编译成汇编代码. 源代码如下: int g(int x) { return x + 9; } int f(int x) ...
- 2017-2018-2 1723《程序设计与数据结构》第三周作业 & 实验一 总结
作业地址 第三周作业:https://edu.cnblogs.com/campus/besti/CS-IMIS-1723/homework/1667 提交情况如图: 实验一:https://edu.c ...
- spring-boot随笔
配置了spring-boot-starter-web的依赖后,会自动添加tomcat和spring mvc的依赖,那么spring boot 会对tomcat和spring mvc进行自动配置 < ...
- python scipy stats学习笔记
from scipy.stats import chi2 # 卡方分布from scipy.stats import norm # 正态分布from scipy.stats import t # t分 ...
- ElasticSearch 2 (2) - Setup
ElasticSearch 2.1.1 (2) - Setup Installation Elasticsearch can be started using: $ bin/elasticsearc ...
- SqlServer测试SQL语句执行效率
方法一: SET STATISTICS io ON SET STATISTICS time ON go ---需要测试的sql语句 go SET STATISTICS profile OFF SET ...
- LVS(Linus Virtual Server):三种负载均衡方式比较+另三种负载均衡方式
还有个姊妹篇也可以参考这个文章:六大Web负载均衡原理与实现 什么是LVS (Linux Virtual Server)? 首先简单介绍一下LVS (Linux Virtual Server)到底 ...