poj1177 Picture 扫描线+线段树+离散化

Language:
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 10203 Accepted: 5406

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.

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

Source


题意:求矩阵并的周长

思路:参考kuangbin大大的博客的,上面的分析很详细(传送门),详见代码:

ps: poj数据很水,hdu1828数据较强。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
const int MAXN=10000+100;
int n,edge_cnt;
int y[MAXN];
struct node
{
    bool lc,rc; //记录边界是否被覆盖
    int l,r,len;
    int lf,rf,cnt,lazy; //cnt记录当前区域沿x轴有多少对需要计算的边
}segtree[MAXN<<2];
struct Line
{
    int flag;//flag=-1,表示是正方形的左边;flag=1,表示正方形的右边
    int x,y1,y2;
}line[MAXN];
bool cmp(Line a,Line b)
{
    return a.x<b.x;
}
void build(int rt,int l,int r)
{
    segtree[rt].l=l; segtree[rt].r=r;
    segtree[rt].lf=y[l]; segtree[rt].rf=y[r];
    segtree[rt].lc=segtree[rt].rc=0;
    segtree[rt].lazy=segtree[rt].len=segtree[rt].cnt=0;
    if(l+1==r)
        return ;
    int mid=(l+r)>>1;
    build(L(rt),l,mid); build(R(rt),mid,r);
}
void cal(int rt)
{
    if(segtree[rt].lazy<0){
        segtree[rt].len=segtree[rt].rf-segtree[rt].lf;
        segtree[rt].cnt=1; // 当区域被覆盖,那么只能有一对,即区间左右端
        segtree[rt].lc=segtree[rt].rc=1;
        return ;
    }
    if(segtree[rt].l+1 == segtree[rt].r ){
        segtree[rt].len=0; segtree[rt].cnt=0;
        segtree[rt].lc=segtree[rt].rc=0;
    }
    else {
        segtree[rt].len=segtree[L(rt)].len+segtree[R(rt)].len;
        segtree[rt].cnt=segtree[L(rt)].cnt+segtree[R(rt)].cnt;
        segtree[rt].lc=segtree[L(rt)].lc; segtree[rt].rc=segtree[R(rt)].rc;
        if(segtree[L(rt)].rc && segtree[R(rt)].lc) segtree[rt].cnt--; //当两个子区间边界处重合,那么对数-1,即只有左子区间的左端和右子区间的右端形成的一对
    }
}
void update(int rt,Line e)
{
    if(segtree[rt].lf == e.y1 && segtree[rt].rf == e.y2){
        segtree[rt].lazy+=e.flag; cal(rt);
        return ;
    }
    if(e.y2<=segtree[L(rt)].rf) update(L(rt),e);
    else if(e.y1>=segtree[R(rt)].lf) update(R(rt),e);
    else {
        Line temp=e; temp.y2=segtree[L(rt)].rf;
        update(L(rt),temp);
        temp=e; temp.y1=segtree[R(rt)].lf;
        update(R(rt),temp);
    }
    cal(rt);
}
int main()
{
    //freopen("text.txt","r",stdin);
    int x1,y1,x2,y2;
    while(~scanf("%d",&n)){
        edge_cnt=0;
        for(int i=1;i<=n;i++){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            line[++edge_cnt].x=x1; line[edge_cnt].flag=-1;
            line[edge_cnt].y1=y1; line[edge_cnt].y2=y2; y[edge_cnt]=y1;
            line[++edge_cnt].x=x2; line[edge_cnt].flag=1;
            line[edge_cnt].y1=y1; line[edge_cnt].y2=y2; y[edge_cnt]=y2;
        }
        sort(line+1,line+edge_cnt+1,cmp);
        sort(y+1,y+edge_cnt+1);
        n=unique(y+1,y+edge_cnt+1)-(y+1);
        build(1,1,n);
        int ans=0,last=0;//用last记录上次的长度
        for(int i=1;i<edge_cnt;i++){
            update(1,line[i]);
            ans+=segtree[1].cnt*2*(line[i+1].x-line[i].x);
            ans+=abs(segtree[1].len-last);
            last=segtree[1].len;
        }
        ans+=segtree[1].len;
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值