首页 技术 正文
技术 2022年11月18日
0 收藏 842 点赞 4,338 浏览 3105 个字

题目链接:https://vjudge.net/problem/POJ-1177

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. 
POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化
The corresponding boundary is the whole set of line segments drawn in Figure 2. 
POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化
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.

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

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; struct line
{
int le, ri, h;
int id;
bool operator<(const line &a)const{
return h<a.h;
}
}Line[MAXN]; //X用于离散化横坐标,times为此区间被覆盖的次数,block为有多少块子区间, len为被覆盖的长度
int X[MAXN<<], times[MAXN<<], block[MAXN<<], len[MAXN];
bool usedl[MAXN<<], usedr[MAXN<<];
//usedl用于表示区间的左端是否被覆盖, usedr亦如此 void push_up(int u, int l, int r)
{
if(times[u]>) //该区间有被覆盖
{
len[u] = X[r] - X[l];
block[u] = ;
usedl[u] = usedr[u] = true;
}
else //该区间没有被覆盖
{
if(l+==r) //该区间为单位区间
{
len[u] = ;
block[u] = ;
usedl[u] = usedr[u] = false;
}
else //该区间至少包含两个单位区间
{
len[u] = len[u*] + len[u*+];
block[u] = block[u*] + block[u*+];
if(usedr[u*] && usedl[u*+]) //如果左半区间的右端与右半区间的左端均被覆盖,则他们合成一个子区间
block[u]--;
usedl[u] = usedl[u*];
usedr[u] = usedr[u*+];
}
}
} //此种线段树的操作对象为连续型,即最小的元素为长度为1的区间[l,r],其中l和r只代表端点(r-l>=1),用于确定
//区间的位置和长度,l和r本身没有特别的含义。而以往做的什么单点更新之类的,都属于离散型,在l处和r处是有含义的
void add(int u, int l, int r, int x, int y, int v)
{
if(x<=l && r<=y)
{
times[u] += v;
push_up(u, l, r);
return;
} int mid = (l+r)>>;
if(x<=mid-) add(u*, l, mid, x, y, v);
if(y>=mid+) add(u*+, mid, r, x, y, v);
push_up(u, l, r);
} int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
for(int i = ; i<=n; i++)
{
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
Line[i].le = Line[i+n].le = x1;
Line[i].ri = Line[i+n].ri = x2;
Line[i].h = y1; Line[i+n].h = y2;
Line[i].id = ; Line[i+n].id = -;
X[i] = x1; X[i+n] = x2;
} sort(Line+, Line++*n);
sort(X+, X++*n);
int m = unique(X+, X++*n) - (X+); memset(times, , sizeof(times));
memset(len, , sizeof(len));
memset(block, , sizeof(block));
memset(usedl, false, sizeof(usedl));
memset(usedr, false, sizeof(usedr)); int ans = , pre_len = ;
Line[*n+].h = Line[*n].h; //边界条件
for(int i = ; i<=*n; i++)
{
int l = upper_bound(X+, X++m, Line[i].le) - (X+);
int r = upper_bound(X+, X++m, Line[i].ri) - (X+);
add(, , m, l, r, Line[i].id);
ans += abs(len[] - pre_len); //变化的长度即为显露出来的横向边
ans += *block[]*(Line[i+].h-Line[i].h); //如果有cnt个连续的区间,那么就有2*cnt条显露出来的纵向边
pre_len = len[];
} printf("%d\n", ans);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,816
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899