hdu 1828 Picture(线段树轮廓线)
Picture
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3075 Accepted Submission(s): 1616
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.
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.
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.
#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(线段树轮廓线)的更多相关文章
- hdu 1828 Picture(线段树 || 普通hash标记)
http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others) Mem ...
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
- HDU 1828 Picture (线段树+扫描线)(周长并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...
- HDU 1828 Picture (线段树:扫描线周长)
依然是扫描线,只不过是求所有矩形覆盖之后形成的图形的周长. 容易发现,扫描线中的某一条横边对答案的贡献. 其实就是 加上/去掉这条边之前的答案 和 加上/去掉这条边之后的答案 之差的绝对值 然后横着竖 ...
- HDU 1828 Picture(长方形的周长和)
HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4 ...
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 1828 Picture(线段树扫描线求周长)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
随机推荐
- const类型总结
const 表示常类型 作用: 1.具有不可变性. 2.可以很方便对参数进行调整和修改,和宏定义一样,不变则已,变都变. 3.保护被修饰的东西,防止被意外修改.(如:在修饰函数的形参时,加 ...
- 消灭 Java 代码的“坏味道”
消灭 Java 代码的“坏味道” 原创: 王超 阿里巴巴中间件 昨天 导读 明代王阳明先生在<传习录>谈为学之道时说: 私欲日生,如地上尘,一日不扫,便又有一层.着实用功,便见道无终穷,愈 ...
- 2018ICPC银川 L Continuous Intervals 单调栈 线段树
题意:给你一个序列,问你这个序列有多少个子区间,满足把区间里的数排序之后相邻两个数之间的差 <= 1 ? 思路:https://blog.csdn.net/u013534123/article/ ...
- mvn 打包排除test
mvn clean package compile -Dmaven.test.skip=true
- php中判断数组键值,array_key_exists和isset区别
$arr = array('key' => NULL); if(isset($arr['key'])){ echo 'isset'; } else { echo 'unset'; } echo ...
- windows 10 删除资源管理器导航栏 Creative Cloud Files
安装完Adobe的Photoshop.After Effects等软件后,资源管理器导航栏会出现让人讨厌的Creative Cloud Files目录,稍微修改注册表即可删除. 快捷键Windows ...
- Python---基础---list(列表)
2019-05-20 一. # append() 向列表末尾追加新元素 返回值Nonelist1 = [1,2,3,4,5]print(id(list1))list1.append(6)prin ...
- Apache HttpClient之fluent API的使用
该方法为Apache HttpClient 4.5以上的版本支持,在官网有明确的说明. 对比以前的方式,其优点是代码更简洁,同时为线程安全的.仅举一个最简单的post栗子 JAR包信息: <de ...
- JS自定义随机键盘
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Centos7防火墙和SELinux的开启和关闭
在虚拟机里面开启多个服务,对应多个端口,在防火墙开启的情况下,就要对外开放端口,这样客户端才能正常访问,但比较繁琐,关闭更直接点. 防火墙 临时关闭防火墙 systemctl stop firewal ...