Picture

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

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
 
 
 
给出 n 个矩阵 求包围这些矩阵的边界长度。
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
using namespace std;
typedef pair<int,int> pii ;
typedef long long LL;
#define X first
#define Y second
#define root 1,n,1
#define lr rt<<1
#define rr rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int N = ;
const int M = ;
const int mod = ;
int n , m ;
struct Point { int x , y ;Point(){} };
struct Line{ int tag ; Point a, b ; }e[N];
inline bool cmp1 ( const Line &A , const Line &B ) { return A.a.x < B.a.x ; }
inline bool cmp2 ( const Line &A , const Line &B ) { return A.a.y < B.a.y ; } int lazy[N<<] , cnt[N<<] , sum[N<<] ; void build( int l , int r , int rt ) {
sum[rt] = lazy[rt] = cnt[rt] = ;
if( l == r ) return ;
int mid = (l+r)>>;
build(lson),build(rson);
} void Up( int l , int r , int rt ) {
if( cnt[rt] > ) sum[rt] = r - l + ;
else sum[rt] = sum[lr] + sum[rr] ;
} void update( int l , int r , int rt , int L , int R , int tag ) {
if( l == L && r == R ) {
if( tag ) {
cnt[rt]++ , lazy[rt] ++ ;
sum[rt] = r - l + ;
}
else {
cnt[rt]-- , lazy[rt] -- ;
if( cnt[rt] > ) sum[rt] = r - l + ;
else {
if( l == r ) sum[rt] = ;
else sum[rt] = sum[lr] + sum[rr] ;
}
}
return ;
}
int mid = (l+r)>>;
if( L > mid ) update(rson,L,R,tag);
else if( R <= mid ) update(lson,L,R,tag);
else update(lson,L,mid,tag) , update(rson,mid+,R,tag);
Up(l,r,rt);
} int x1[N] , x2[N] , y1[N] , y2[N]; int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif // LOCAL
int _ , cas = ;
int mx , Mx , my , My ;
while( scanf("%d",&n) != EOF ) {
Mx = My = -N , mx = my = N ;
for( int i = ; i < n ; ++i ) {
scanf("%d%d%d%d",&x1[i],&y1[i],&x2[i],&y2[i]);
mx = min( mx , x1[i] ); Mx = max( Mx , x2[i] );
my = min( my , y1[i] ); My = max( My , y2[i] );
e[i].a.x = x1[i] , e[i].a.y = y1[i] ;
e[i].b.x = x1[i] , e[i].b.y = y2[i] ;
e[i].tag = ;
e[i+n].a.x = x2[i] , e[i+n].a.y = y2[i];
e[i+n].b.x = x2[i] , e[i+n].b.y = y1[i];
e[i+n].tag = ;
}
int tot = n * ;
sort( e , e + tot ,cmp1 ) ;
LL ans = , last = ;
build( my , My - , );
for( int i = ; i < tot ; ++i ) {
int x = e[i].a.y , y = e[i].b.y ;
if( x > y ) swap(x,y);
update( my , My - , , x , y - , e[i].tag );
LL tmp = sum[] ;
ans += abs( tmp - last );
last = tmp ;
}
for( int i = ; i < n ; ++i ){
e[i].a.x = x1[i] , e[i].a.y = y1[i] ;
e[i].b.x = x2[i] , e[i].b.y = y1[i] ;
e[i].tag = ;
e[i+n].a.x = x2[i] , e[i+n].a.y = y2[i] ;
e[i+n].b.x = x1[i] , e[i+n].b.y = y2[i] ;
e[i+n].tag = ;
}
last = ;
sort( e , e + tot , cmp2 ) ;
build(mx,Mx-,);
for( int i = ; i < tot ; ++i ) {
int x = e[i].a.x , y = e[i].b.x ;
if( x > y ) swap(x,y);
update( mx , Mx - , , x , y - , e[i].tag );
LL tmp = sum[] ;
ans += abs( tmp - last );
last = tmp ;
}
printf("%I64d\n",ans);
}
}

hdu 1828 Picture(线段树轮廓线)的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 1828 Picture(长方形的周长和)

    HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4 ...

  6. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  7. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  8. hdu 3016 dp+线段树

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

  9. HDU 1828 Picture(线段树扫描线求周长)

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

随机推荐

  1. Javascript 原型链之原型对象、实例和构造函数三者之间的关系

    前言:用了这么久js,对于它的原型链一直有种模糊的不确切感,很不爽,隧解析之. 本文主要解决的问题有以下三个: (1)constructor 和 prototype 以及实例之间啥关系? (2)pro ...

  2. select 项目<选课系统>

    """1. 创建北京.上海 2 所学校 学校类对象 属性:名字,地址 行为: 2. 创建linux , python , go 3个课程 , linux\py 在北京开, ...

  3. webpack webpack.config.js配置

    安装指定版本的webpack npm install webpack@3.6 -g 安装live-server    运行项目插件   输入live-server  运行后自动打开网页 npm ins ...

  4. Java的基本数据类型,以及他们的封装类

    基本类型  大小  默认值  封装类   boolean 1 false  Boolean  byte  1 0  Byte  char  2  \u0000(null)  Character  sh ...

  5. mvn 打包排除test

    mvn clean package compile -Dmaven.test.skip=true

  6. 前端面试题(4)JavaScript

    前端面试题JavaScript(一) JavaScript的组成 JavaScript 由以下三部分组成: ECMAScript(核心):JavaScript 语言基础 DOM(文档对象模型):规定了 ...

  7. 百度编辑器UEditor使用总结

    官网下载地址:http://ueditor.baidu.com/website/download.html 我下载的是jsp版本,下载后将整个目录复制到项目的js包下,然后将jsp包下的lib下的ja ...

  8. ht-5 treemap特性

    (1)TreeMap类通过使用红黑树实现Map接口 (2)TreeMap提供按排序顺序存储键值对的有效手段,同时允许快速检索 (3)不同于散列映射,树映射保证它的元素按键的自然顺序升序排列 (4)Tr ...

  9. .Net Core 学习目录(搁置)

    简介 .NET Core 是.NET Framework的新一代版本,是微软开发的第一个官方版本,具有跨平台 (Windows.Mac OSX.Linux) 能力的应用程序开发框架 (Applicat ...

  10. Linux Crontab命令定时任务基本语法

    一.Crontab查看编辑重启 1.查看crontab定时执行任务列表 crontab -l 2.编辑crontab定时执行任务 crontab -e 3.删除crontab定时任务 crontab  ...