求矩形的周长(线段树+扫描线) 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 ...
随机推荐
- Appium+Python3+ Android入门
前言: Appium 是一个自动化测试开源工具,支持 iOS 平台和 Android 平台上的原生应用,web 应用和混合应用. 一.环境配置 1.安装Node.js https://nodejs.o ...
- 《Linux内核分析》课程第四周学习总结
姓名:何伟钦 学号:20135223 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/U ...
- Linux内核分析作业四
扒开系统调用的三层皮 一.用户态.内核态和中断 一般现代CPU都有几种不同的指令级别 在高级别执行级别下,代码可以执行特权指令,访问任意的物理地址,称之为内核态 在相应的低指令执行级别下,代码的掌控范 ...
- 第二次spring冲刺第2天
今天我们开了个小会,关于讨论开始页面的设计及数据输入的格式限制.运算功能等改善
- 阅读c#程序——回答问题
c#“小”程序: using System; using System.Collections.Generic; using System.Text; namespace FindTheNumber ...
- [转帖] CentOS 添加新的CA证书到认证地方
Install the ca-certificates package: yum install ca-certificates Enable the dynamic CA configuration ...
- python进阶-虚拟环境
virtualenv # 安装 pip install virtualenv # 查看版本,确认安装完成 virtualenv --version # 创建虚拟环境 virtualenv my_env ...
- jQuery源码之 empty与html('')的区别
empty: function() { var elem, i = 0; for ( ; (elem = this[i]) != null; i++ ) { // Remove element nod ...
- 【模板】MST(Prim)
代码如下 #include <bits/stdc++.h> using namespace std; const int maxv=2e5+10; const int maxe=5e5+1 ...
- Hadoop生态圈-Flume的主流source源配置
Hadoop生态圈-Flume的主流source源配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客只是配置的是Flume主流的Source,想要了解更详细的配置信息请参 ...