求矩形的周长(线段树+扫描线) 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 ...
随机推荐
- PAT甲题题解-1070. Mooncake (25)-排序,大水题
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- 第二个spring冲刺第8天
今天我们团队分别安排了不同的任务,分别是1人程序编写,1人检查bug,1人负责客户体验,还有我们的总负责人王俊凯同学负责各个部分的协调.今天程序有了新的调整,但是功能还是没有完全做出来,不过还在开发途 ...
- 为什么java的main方法必须是静态的
今天看类型信息时发现一个问题,不能再main方法中打印this关键字的信息,这时想起了之前的知识,不能再静态方法中调用this.理由很简单,this表示“这个对象”,也就是声明一个类的对象,然而静态方 ...
- python自动化之爬虫原理及简单案例
[爬虫案例]动态地图里的数据如何抓取:以全国PPP综合信息平台网站为例 http://mp.weixin.qq.com/s/BXWTf5hmq8vp91ZvgaphEw [爬虫案例]动态页面的抓取! ...
- python自动化之鼠标移动
################################用GUI自动化控制键盘和鼠标############################### ''' http://pyautogui.r ...
- java学习之switch 等值判断
当匹配到相等的值时候 则进入case里面执行语句 当该语句有break时候 则退出匹配 当没有break时候 则继续往下匹配 直到遇到break才停止匹配
- 二分图最大权匹配模板(pascal)
用uoj80的题面了: 从前一个和谐的班级,有 nlnl 个是男生,有 nrnr 个是女生.编号分别为 1,…,nl1,…,nl 和 1,…,nr1,…,nr. 有若干个这样的条件:第 vv 个男生和 ...
- Grafana elasticsearch 应用
早期的时候,项目基于ES+echart写了一些仪表盘的展示页面,虽然ES配合这种char界面有着天然的优势,但实际写起代码来,还是很多重复的劳动,在一次偶然中发现Grafana,看到它提供了很多仪表盘 ...
- LCM Cardinality UVA - 10892(算术基本定理)
这题就是 LightOJ - 1236 解析去看这个把https://www.cnblogs.com/WTSRUVF/p/9185140.html 贴代码了: #include <iostrea ...
- NOI备战总结ing……
持续做题ing…… 已完成: 树套树 点分治 博弈论 凸包 杜教筛 反演 FFT 数位DP DP专栏 网络流 数学专栏 正在进行中: waiting: SAM Kd-tree 矩阵树 分治 FWT B ...