求矩形的周长(线段树+扫描线) 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 ...
随机推荐
- [2017BUAA软工助教]结对项目小结
2017BUAA结对项目小结 一.作业链接 http://www.cnblogs.com/jiel/p/7604111.html 二.评分细则 1.注意事项 按时间完成并提交--正常评分 晚交一周以内 ...
- nodeJs上传附件
两种方案: 这两种方案传参还是有区别额 在nodeJs中上传附件调用了 multer 的中间件,采用这个中间件来上传 首先是表单(前端部分): <!DOCTYPE html> <ht ...
- 11th 最后的致意
“终于我们不再是师生”,无论日后我们是否是师生,但这段经历是不可否认的,可以说软件工程这一门课程恐怕是我学生生涯中终生难忘的一段体验.即便不是从知识上,从另一个方面来讲,也教给了我一种做人做事的态度. ...
- spring 事务-support 有事务得开启就参加 没有就不参加
spring 事务-support 有事务得开启就参加 没有就不参加
- Luogu4770 NOI2018你的名字(后缀数组+线段树)
即求b串有多少个本质不同的非空子串,在a串的给定区间内未出现.即使已经8102年并且马上就9102年了,还是要高举SA伟大旗帜不动摇. 考虑离线,将所有询问串及一开始给的串加分隔符连起来,求出SA.对 ...
- hihoCoder 1631 Cats and Fish(ACM-ICPC北京赛区2017网络同步赛)
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are many homeless cats in PKU campus. They are all happy ...
- 【题解】 bzoj1875: [SDOI2009]HH去散步 (动态规划+矩阵乘法)
bzoj1875,懒得复制,戳我戳我 Solution: 看到这道题,看的出是个dp,每个点\(t\)时刻到达的方案数等于\(t-1\)到连过来的点方案数之和 但又因为题目有要求不能走一样的边回去不是 ...
- 【题解】 bzoj1190: [HNOI2007]梦幻岛宝珠 (动态规划)
bzoj1190,懒得复制,戳我戳我 Solution: 这道题其实是一个背包(分组背包),但是由于数字比较大,就要重新构造dp式子.啃了三天才懂. \(dp[i][j]\)表示背包容积为\(j*2^ ...
- luogu1082 [NOIp2012]同余方程 (扩展欧几里得)
由于保证有解,所以1%gcd(x,y)=0,所以gcd(x,y)=1,直接做就行了 #include<bits/stdc++.h> #define pa pair<int,int&g ...
- 【bzoj3160】 万径人踪灭
http://www.lydsy.com/JudgeOnline/problem.php?id=3160 (题目链接) 题意 给定一个由'a'和'b'构成的字符串,求不连续回文子序列的个数. Solu ...