A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.

The corresponding boundary is the whole set of line segments drawn in Figure 2.

The vertices of all rectangles have integer coordinates.

InputYour program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

0 <= number of rectangles < 5000 
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

Please process to the end of file.OutputYour program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.Sample Input

7
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16

Sample Output

228

题解:
  扫描线,横着一次,竖着一次,就可以了,
  每次是局对值相减,维护一个l,r(这样不需要坐标转化),然后一个cnt表示这条线段被覆盖了多少次,
  以及该线段多少区域,被覆盖了多少次,向上更新即可。
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstdio> #define N 5007
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if (ch=='-') f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
} int n,top,ans,minn,maxx;
struct Node
{
int a,b,c,d;
}a[N],cl[N*2]; struct date
{
int l,r,sum,cnt;
void init(){sum=cnt=0;}
}tr[40007]; void build(int p,int l,int r)
{
tr[p].init();tr[p].l=l,tr[p].r=r;
if (l==r) return;
int mid=(l+r)>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r);
}
bool cmp(Node x,Node y){return x.a<y.a;}
void update(int p)
{
if (tr[p].cnt) tr[p].sum=tr[p].r-tr[p].l+1;
else tr[p].sum=tr[p<<1].sum+tr[p<<1|1].sum;
}
void change(int p,int l,int r,int x,int y,int z)
{
if (l==x&&y==r)
{
tr[p].cnt+=z;
update(p);
return ;
}
int mid=(l+r)>>1;
if (y<=mid) change(p<<1,l,mid,x,y,z);
else if (x>mid) change(p<<1|1,mid+1,r,x,y,z);
else change(p<<1,l,mid,x,mid,z),change(p<<1|1,mid+1,r,mid+1,y,z);
update(p);
}
void solve()
{
build(1,minn,maxx);
sort(cl+1,cl+top+1,cmp);
int last=0;
for (int i=1;i<=top;i++)
{
change(1,minn,maxx,cl[i].b,cl[i].c-1,cl[i].d);
ans+=abs(tr[1].sum-last);
last=tr[1].sum;
}
}
int main()
{
while(~scanf("%d",&n))
{
minn=10000,maxx=-10000;
for (int i=1;i<=n;i++)
{
a[i].a=read(),a[i].b=read(),a[i].c=read(),a[i].d=read();
minn=min(minn,a[i].a),minn=min(minn,a[i].b),minn=min(minn,a[i].c),minn=min(minn,a[i].d);
maxx=max(maxx,a[i].a),maxx=max(maxx,a[i].b),maxx=max(maxx,a[i].c),maxx=max(maxx,a[i].d);
}
for (int i=1;i<=n;i++)
{
cl[++top].a=a[i].b;
cl[top].b=a[i].a,cl[top].c=a[i].c;
cl[top].d=1;
cl[++top].a=a[i].d;
cl[top].b=a[i].a,cl[top].c=a[i].c;
cl[top].d=-1;
}
solve(),top=0;
for (int i=1;i<=n;i++)
{
cl[++top].a=a[i].a;
cl[top].b=a[i].b,cl[top].c=a[i].d;
cl[top].d=1;
cl[++top].a=a[i].c;
cl[top].b=a[i].b,cl[top].c=a[i].d;
cl[top].d=-1;
}
solve();
printf("%d\n",ans);
}
}

  

hdu 1828 Picture(线段树,扫描线)的更多相关文章

  1. HDU 1828 Picture (线段树+扫描线)(周长并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...

  2. HDU 1828 Picture (线段树:扫描线周长)

    依然是扫描线,只不过是求所有矩形覆盖之后形成的图形的周长. 容易发现,扫描线中的某一条横边对答案的贡献. 其实就是 加上/去掉这条边之前的答案 和 加上/去掉这条边之后的答案 之差的绝对值 然后横着竖 ...

  3. POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

    求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...

  4. hdu 1828 Picture(线段树 || 普通hash标记)

    http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others)    Mem ...

  5. hdu 1828 Picture(线段树轮廓线)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  6. HDU 1542 - Atlantis - [线段树+扫描线]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  7. hdu1828 Picture(线段树+扫描线+矩形周长)

    看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积))  解法一·:两次扫描线 如图我们可以 ...

  8. 覆盖的面积 HDU - 1255 (线段树-扫描线)模板提

    给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1& ...

  9. HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  10. hdu 1542 Atlantis (线段树扫描线)

    大意: 求矩形面积并. 枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上. #include <iostream> #inclu ...

随机推荐

  1. Normal equations 正规方程组

    前面我们通过Gradient Descent的方法进行了线性回归,但是梯度下降有如下特点: (1)需要预先选定Learning rate: (2)需要多次iteration: (3)需要Feature ...

  2. Netflix正式开源其API网关Zuul 2--转

    微信公众号:聊聊架构 5 月 21 日,Netflix 在其官方博客上宣布正式开源微服务网关组件 Zuul 2.Netflix 公司是微服务界的楷模,他们有大规模生产级微服务的成功应用案例,也开源了相 ...

  3. IIS中不让下级应用程序继承主域名的web.config配置

    <location path="." allowOverride="true" inheritInChildApplications="fals ...

  4. 外文翻译 《How we decide》赛场上的四分卫

    本书导言翻译 为了能看懂这一章,先做了如下的功课: 百度百科 四分卫 国家橄榄球联盟中文站 在2002年超级碗赛场上,比赛的时间仅剩80秒,两队比分持平.新英格兰爱国者队于17码的位置执球,他们的对手 ...

  5. HttpServletRequest对象,自己学习的心得。

    1. HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过Http超文本传输协议访问服务器时,Http请求头中的所有信息都封装在这个对象中, ...

  6. CentOS 7 下配置 firewalld(firewall-cmd)实现 NAT 转发 软路由

    如果配合 DHCP 服务或实现更多功能. ☼ NAT 转发软路由 开启 NAT 转发之后,只要本机可以上网,不论是单网卡还是多网卡,局域网内的其他机器可以将默认网关设置为已开启 NAT 转发的服务器 ...

  7. git --删除文件、重命名

    修改最后一次提交 git commit --amend -m “” 删除文件:. git rm <需要删除的文件> 只是删除当前工作目录和暂存区的文件,也就是取消跟踪.在下次提交时不纳入版 ...

  8. Bing图片下载器(Python实现)

    分享一个Python实现的Bing图片下载器.下载首页图片并保存到到当前目录.其中用到了正则库re以及Request库. 大致流程如下: 1.Request抓取首页数据 2.re正则匹配首页图片URL ...

  9. Eureka 整理

    服务治理:(该模块也可以使用集群) 该模块主要负责完成微服务架构中的服务治理功能. 1.构建服务注册中心. 每个服务单元需要向注册中心登记自己提供的服务. 2.服务注册与服务发现. 服务之间的调用不再 ...

  10. Laravel 的 API 认证系统 Passport 三部曲(二、passport的具体使用)

    GQ1994 关注 2018.04.20 09:31 字数 1152 阅读 1316评论 0喜欢 1 参考链接 Laravel 的 API 认证系统 Passport 三部曲(一.passport安装 ...