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. phpcms单页顶级栏目默认打开第一个子栏目方法

    首先phpcms单页如过下面有子栏目,那么当前栏目是不能被编辑内容的,且访问后是没有内容的,首先不知道这是不是产品设计的一个缺陷,但是在使用过程中确实在后台也没有找到其他的对应解决办法,刚好在某QQ群 ...

  2. 做程序开发的你如果经常用Redis,这些问题肯定会遇到

    分布式缓存Redis是一种支持Key-Value等多种数据结构的存储系统.可用于缓存.事件发布或订阅.高速队列等多种场景.Redis使用ANSI C语言编写,提供字符串(String).哈希(Hash ...

  3. Linux内核学习笔记(1)-- 进程管理概述

    一.进程与线程 进程是处于执行期的程序,但是并不仅仅局限于一段可执行程序代码.通常,进程还要包含其他资源,像打开的文件,挂起的信号,内核内部数据,处理器状态,一个或多个具有内存映射的内存地址空间及一个 ...

  4. JAVA学习笔记--简介几个常见关键字static、final、this、super

    一.static static(静态的),可以放在类.方法.字段之前. 通常,当创建类时,就是在描述那个类的外观与行为.除非用 new 创建那个类的对象,否则,实际上并未获得任何对象.执行 new 来 ...

  5. Spark入门(Python)

    Hadoop是对大数据集进行分布式计算的标准工具,这也是为什么当你穿过机场时能看到”大数据(Big Data)”广告的原因.它已经成为大数据的操作系统,提供了包括工具和技巧在内的丰富生态系统,允许使用 ...

  6. 关于jsonp跨域的 实现

    1.实现原理    1.把接口写在 script标签的src 中 这个接口就可以访问(不会存在跨域问题  因为接口在浏览器地址栏是可以访问的  会返回json字符串); 2.直接写不可以  因为正常情 ...

  7. Java GUI 点击按钮退出

    import java.awt.*; import java.awt.event.*; public class TestFrameTwo implements ActionListener { Fr ...

  8. centos 6 编译emacs-24.5

    yum install `yum deplist emacs | grep provider | awk -F: '{print $2}' | awk '{print $1}' | xargs` yu ...

  9. IBM存储降级告警等一些服务器问题/dd/ethtool

    1.IBM存储降级告警 一般两种情况 a.端口降级 例如模块16G->8G(IBM储存端口自适应) b.系统在作raid后,有硬盘损坏,降级 黄灯告警 2. dimm error dimm内存插 ...

  10. phaser2 微信小游戏入手

    phaser2小游戏基本没什么什么问题,可以下常开发游戏.如果遇到什么问题, 可以提出来共同讨论. 下面来个例子 import './lib/weapp-adapter'; import Phaser ...