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. 【转】: 塞尔达组在GDC2017演讲的文字翻译:技术的智慧

    大家好,我是堂田卓宏,在<荒野之息>的制作中我担任技术总监的职位.我在2003年加入任天堂,并且作为程序员参与了许多不同游戏的制作.在本次的制作的过程中,我们的程序员团队也需要打破许多游戏 ...

  2. mysql 无法启动,错误1067,进程意外终止

    在做项目启动mysql数据库时,经常出现 这个错误,今天总结一下 //查看了网上很多的方法,都不适用,但或许对你适用.ps:网上只提供了怎么解决这个问题,但是没有将怎么去发现问题,对症下药才是王道.而 ...

  3. phpldapadmin具体设置

    一.需求    1.属性隐藏 只显示用户名,部门(因为是单OU设计为了做区分),登录密码 2.属性顺序显示 部门>用户名>登录密码 3.使用UID可以登陆 用户可以使用账户(自己的名字)登 ...

  4. 简析Monte Carlo与TD算法的相关问题

    Monte Carlo算法是否能够做到一步更新,即在线学习? 答案显然是不能,如果可以的话,TD算法还有何存在的意义?MC算法必须要等到episode结束后才可以进行值估计的主要原因在于对Return ...

  5. python实现lower_bound和upper_bound

    由于对于二分法一直都不是很熟悉,这里就用C++中的lower_bound和upper_bound练练手.这里用python实现 lower_bound和upper_bound本质上用的就是二分法,lo ...

  6. 2.hive里的增删改查

    1.hive的增删改查 查询数据库 hive> show databases; OK default Time taken: 0.254 seconds, Fetched: 1 row(s) h ...

  7. Python 中的实用数据挖掘

    本文是 2014 年 12 月我在布拉格经济大学做的名为‘ Python 数据科学’讲座的笔记.欢迎通过 @RadimRehurek 进行提问和评论. 本次讲座的目的是展示一些关于机器学习的高级概念. ...

  8. python中取整的几种方法

    #encoding:utf-8import math #向上取整print "math.ceil---"print "math.ceil(2.3) => " ...

  9. 第9次Scrum会议(10/21)【欢迎来怼】

    一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华小组照片 二.开会信息 时间:2017/10/21 17:20~17:45,总计25min.地点:东北师范 ...

  10. 【Alpha】阶段第二次Scrum Meeting

    [Alpha]阶段第二次Scrum Meeting 工作情况 团队成员 今日已完成任务 明日待完成任务 刘峻辰 发表评论接口 更新评论接口 赵智源 部署实际项目 编写脚本实现持续集成 肖萌威 编写注册 ...