Picture(扫描线周长并)

传送门 HDU1828

描述

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.

输入

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.

输出

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.

样例

  • 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
  • Output
    Case 1: 2500
    Case 2: 16

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<bits/stdc++.h>
#define INIT(a,b) memset(a,b,sizeof(a))
#define LL long long
#define PB push_back
#define lson d<<1,l,mid
#define rson d<<1|1,mid+1,r
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=4e4+7;
const int mod=1e9+7;
struct SL{
int l,r,y,flag;
bool operator < (const SL& a){return y<a.y;}
};
int len[maxn],cnt[maxn],fl[maxn],fr[maxn],num[maxn];
//len横线长度,cnt标记,fl区间左端点是否有竖线,fr区间右端点是否有竖线,num区间竖线对数
vector<SL> sl;
vector<int>dx;
void init(){
sl.clear();dx.clear();
INIT(len,0);INIT(cnt,0);
INIT(fl,0);INIT(fr,0);
INIT(num,0);
}

void push_up(int d,int l,int r){
if(cnt[d]){
len[d]=dx[r+1]-dx[l];
num[d]=1;
fl[d]=fr[d]=1;
}
else if(l==r) len[d]=fl[d]=fr[d]=num[d]=0;
else{
len[d]=len[d<<1]+len[d<<1|1];
fl[d]=fl[d<<1];
fr[d]=fr[d<<1|1];
num[d]=num[d<<1]+num[d<<1|1]-(fr[d<<1]&fl[d<<1|1]);
//当左区间右线和右区间左线重合时-1
}
}
void update(int d,int l,int r,int ql,int qr,int val){
if(ql<=l&&r<=qr) cnt[d]+=val;
else{
int mid=(l+r)>>1;
if(ql<=mid) update(lson,ql,qr,val);
if(mid<qr) update(rson,ql,qr,val);
}
push_up(d,l,r);
}
int main(){
int n,x1,y1,x2,y2;
while(scanf("%d",&n)!=EOF){
init();
for(int i=0;i<n;i++){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
sl.PB((SL){x1,x2,y1,1});
sl.PB((SL){x1,x2,y2,-1});
dx.PB(x1);dx.PB(x2);
}
sort(sl.begin(),sl.end());
sort(dx.begin(),dx.end());
dx.erase(unique(dx.begin(),dx.end()),dx.end());
LL ans=0;int tem=0;
for(int i=0;i<sl.size();i++){
int ql=lower_bound(dx.begin(),dx.end(),sl[i].l)-dx.begin();
int qr=lower_bound(dx.begin(),dx.end(),sl[i].r)-dx.begin()-1;
update(1,0,dx.size()-1,ql,qr,sl[i].flag);
ans+=abs(len[1]-tem);
tem=len[1];
if(i < sl.size()-1) ans+=num[1]*2*(sl[i+1].y-sl[i].y);
}
printf("%lld\n",ans);
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值