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 ...
随机推荐
- 07.SUSE Linux 系统本地yum源配置
SUSE Linux 系统 1.新建本地源存储目录root@suse:mkdir /mnt/SUSE_LOCAL_SOURCE 2.创建zypper本地源root@suse:zypper ar fil ...
- Angular Viewchild undefined
Angular的viewchild在使用的时候报错 undefined 1 检查是否在元素上打上标识 #xxx 2 查看引用元素时的时机 是否在AfterViewInit之后 3 检查元素是否在*ng ...
- idea中ehcahe配置中 Cannot find the declaration of element 'ehcache'.
ehcahe.xml 中报错: Cannot find the declaration of element 'ehcache'. 打开settings->languages&frame ...
- Promise.race 的原理
// race的原理 Promise.race = function(values){ return new Promise((resolve,reject)=>{ for(let i = 0 ...
- 【串线篇】spring boot自动配置精髓
一.SpringBoot启动会加载大量的自动配置类即绿色部分 二.我们看我们需要的功能有没有SpringBoot默认写好的自动配置类: 比如HttpEncodingAutoConfiguration ...
- Flutter-ListTile
ListTile 通常用于在 Flutter 中填充 ListView.在这篇文章中,我将用可视化的例子来说明所有的参数. title title 参数可以接受任何小部件,但通常是文本小部件 List ...
- 线程中sleep和wait方法的区别
sleep() 方法: 线程主动放弃CPU,使得线程在指定的时间内进入阻塞状态,不能得到CPU 时间,指定的时间一过,线程重新进入可执行状态.典型地,sleep()被用在等待某个资源就绪的情形:测试发 ...
- [BZOJ2600] ricehub
问题描述 乡间有一条笔直而长的路称为"米道".沿着这条米道上 R 块稻田,每块稻田的坐标均为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺序给出,即对于 ...
- laravel后台账户登录验证(5.5.48版本)
首先我是菜鸟,对laravel框架也不是很熟悉,突然有一天心血来潮就想研究一下laravel的后台登录用户登录的流程, 虽然公司项目中有这样的一套流程,也看了好几遍,越看越简单,越看我就越会了,当自己 ...
- linux运维、架构之路-Nginx提高
一.虚拟主机搭建 1.基于域名的虚拟主机 [root@web01 html]# cat nginx.conf worker_processes ; events { worker_connection ...