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. MyEclipse常用设置记录

    MyEclipse版本:MyEclipse 2014 Blue版本. 设置内容: 1.内存优化 <MyEclipse_ROOT>/myeclipse-blue.ini文件 主要修改-vma ...

  2. rhel7安装oracle 11gR2

    一.修改操作系统核心参数 在Root用户下执行以下步骤: 1)修改用户的SHELL的限制,修改/etc/security/limits.conf文件 输入命令:vi /etc/security/lim ...

  3. 12.1Java-构造方法

    一.构造方法 作用:在new的同时对成员变量赋值,给对象的属性初始化赋值格式:权限 方法名(参数列表){}方法的名字,必须和类的名字完全一致,大小写一致构造方法不许写返回值类型,如void,int 构 ...

  4. 虚拟机下安装 CentOS 7 的几个小问题

    ※ 网络问题(Destination Host Unreachable) 安装时网络选择的"桥接"模式, 安装完毕,并配置IP地址后,发现只能ping通自己,局域网内的其他IP无法 ...

  5. 微信小程序组件解读和分析:六、progress进度条

    progress进度条组件说明: 进度条,就是表示事情当前完成到什么地步了,可以让用户视觉上感知事情的执行.progress进度条是微信小程序的组件,和HTML5的进度条progress类似. pro ...

  6. nginx for windows 安装

    一.nginx for windows 的安装地址: http://nginx.org/en/download.html 二.nginx 安装地址: http://nginx.org/en/docs/ ...

  7. LOL喷子专用自动骂人工具,2018更新完整版!

    软件截图 软件说明: 先进入游戏 打开程序 Z开启/C关闭 下载地址:密码 yjnm

  8. 【4412开发板使用经验分享】迅为4412开发板I2C驱动问题

    本文转自迅为论坛:bbs.topeetboard.com 我想写DS3231 的驱动 但是读回的数据老是-6 硬件: 我I2C设备连接的这几个GPIO,看了2.5的手册,接口应该是链接正确的 软件 分 ...

  9. Swift Intermediate Language (SIL)

    Swift Intermediate Language (SIL) https://github.com/apple/swift/blob/master/docs/SIL.rst#witness-me ...

  10. LeetCode137只出现一次的数字——位运算

    题目 题目描述:给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现三次.找出那个只出现一次的元素. 说明:你的算法应该具有线性时间的复杂度.你可以不使用额外的空间来实现吗? 思路 题 ...