Picture

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5516    Accepted Submission(s): 2636

Problem Description
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.

 
Input
Your 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.

 
Output
Your 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
 
Source
题目大意:求矩形的周长.
分析:还是线段树+扫描线法.从下往上扫竖线,记录一个类似前缀和一样的东西,表示已经扫过的总长度,扫完当前层后的总长度-以前扫过的总长度的绝对值就是横线的答案.那纵线怎么求呢?采用与求面积差不多的方法,往上跳.跳到最近的上面的横线,那么当前有num条横线就能向上跳num*2*h长度的竖线.因为一条竖线两个端点.竖线答案+横线答案就是问题的解.
          比较容易出错的是最后竖线还要往上跳一次,所以要加一个哨兵元素.
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = , inf = 0x7ffffff;
int n, ans, cnt, minn = inf, maxx = -inf, tot;
int L[maxn << ], R[maxn << ], c[maxn << ], lc[maxn << ], rc[maxn << ], num[maxn << ], tag[maxn << ]; struct node
{
int l, r, h, id;
}e[maxn * ]; void build(int o, int l, int r)
{
L[o] = l;
R[o] = r;
num[o] = ;
lc[o] = rc[o] = ;
c[o] = ;
if (l == r)
return;
int mid = (l + r) >> ;
build(o * , l, mid);
build(o * + , mid + , r);
} bool cmp(node a, node b)
{
return a.h < b.h;
} void pushup(int o)
{
int l = L[o], r = R[o];
if (tag[o])
{
c[o] = r - l + ;
lc[o] = rc[o] = ;
num[o] = ;
}
else
if (l == r)
{
c[o] = ;
num[o] = ;
lc[o] = rc[o] = ;
}
else
{
c[o] = c[o * ] + c[o * + ];
lc[o] = lc[o * ];
rc[o] = rc[o * + ];
num[o] = num[o * ] + num[o * + ] - (rc[o * ] & lc[o * + ]);
}
} void update(int o, int x, int y, int v)
{
int l = L[o], r = R[o];
if (x <= l && r <= y)
{
tag[o] += v;
pushup(o);
return;
}
int mid = (l + r) >> ;
if (x <= mid)
update(o * , x, y, v);
if (y > mid)
update(o * + , x, y, v);
pushup(o);
} int main()
{
while (scanf("%d", &n) != EOF)
{
if (n == )
{
printf("%d\n", );
continue;
}
tot = ans = cnt = ;
minn = inf;
maxx = -inf;
memset(c, , sizeof(c));
memset(num, , sizeof(num));
memset(tag, , sizeof(tag));
memset(lc, , sizeof(lc));
memset(rc, , sizeof(rc));
memset(L, , sizeof(L));
memset(R, , sizeof(R));
for (int i = ; i <= n; i++)
{
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
minn = min(a, minn);
maxx = max(c, maxx);
e[++tot].l = a;
e[tot].r = c;
e[tot].h = b;
e[tot].id = ;
e[++tot].l = a;
e[tot].r = c;
e[tot].h = d;
e[tot].id = -;
}
sort(e + , e + + tot, cmp);
build(, minn, maxx - );
int last = ;
e[tot + ].h = e[tot].h;
for (int i = ; i <= tot; i++)
{
update(, e[i].l, e[i].r - , e[i].id);
ans += abs(c[] - last);
ans += (e[i + ].h - e[i].h) * num[] * ;
last = c[];
}
printf("%d\n", ans);
} return ;
}

Hdu1828 Picture的更多相关文章

  1. HDU1828 Picture 线段树+扫描线模板题

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

  2. HDU-1828 Picture(扫描线)

    题目大意:给若干个矩形,求轮廓边长. 题目分析:与求面积类似.按从下往上扫描,仍然是底边添加,上边删除.但要同时维护竖边的数目,每次扫描对答案的贡献为扫描线上总覆盖长度的变化量加上竖边的增量.总覆盖长 ...

  3. HDU-1828 Picture(扫描线 求矩形并的周长)

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

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

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

  5. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)

    转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...

  6. [转载]完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...

  7. 【转】线段树完全版~by NotOnlySuccess

    线段树完全版  ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...

  8. 《完全版线段树》——notonlysuccess

    转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...

  9. 【转】 线段树完全版 ~by NotOnlySuccess

    载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...

随机推荐

  1. 如何使用Win+R快捷键打开自定义程序

    鉴于大家对于提高效率这块有争议,更改了下标题. 大家平时一定都使用过Win+R运行快捷键, 在运行里可以快捷的打开一些系统软件,比如说输入mstsc是打开远程连接,输入explorer是打开文件管理器 ...

  2. smartgit 过期

    进入到安装目录把Setting.xml 文件删除然后,再次打开就可以正常使用了.

  3. 修改Linux系统下的最大文件描述符限制

    通常我们通过终端连接到linux系统后执行ulimit -n 命令可以看到本次登录的session其文件描述符的限制,如下: $ulimit -n1024 当然可以通过ulimit -SHn 1024 ...

  4. Python学习小目录汇总

    python其他知识目录 python基础知识-1 1.typora软件使用 2.python解释器安装 3.Python解释器环境变量添加 4.计算机编码知识: 5.输出print(): 6.变量 ...

  5. ES6的新特性(10)——Class 的基本语法

    Class 的基本语法 简介 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子. function Point(x, y) { this.x = x; this.y ...

  6. mysql---时间类型详解

    mysql 日期类型 mysql 日期类型    ·  DATE (适用于"出生日期"等只需要年月日数据的日期字段) 日期.支持的范围为'1000-01-01'到'9999-12- ...

  7. Spring学习(七)——增强类

    Spring 切点 什么是切点?切点(Pointcut),每个程序类都拥有多个连接点,如一个拥有两个方法的类,这两个方法都是连接点,即连接点是程序类中客观存在的事物.但在这为数从多的连接点中,如何定位 ...

  8. SpringBoot与Swagger2整合

    一.Swagger简介与优势 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还为了以后交接方便,都有要求写API文档. Swa ...

  9. Tomcat指定JDK路径

    一.应用实例 一般情况下一台服务器只跑一个业务,那么就直接配置一套环境,设置好Java环境变量即可.某些时候一台服务器上会安装多个业务,而且各个业务需要的JDK版本各不相同,或者为了使业务独立开来,需 ...

  10. 剖析Vue原理&实现双向绑定MVVM-2

    vue.js 最核心的功能有两个,一是响应式的数据绑定系统,二是组件系统.本文仅探究双向绑定是怎样实现的.先讲涉及的知识点,再用简化得不能再简化的代码实现一个简单的 hello world 示例. 一 ...