POJ - 1177 Picture —— 扫描线求周长

Picture

Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 13565 Accepted: 7145

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

题意:

求给定多个矩形并的周长

思路:

扫描线

横向的长度是上一次查询时的总区间长度减掉当前查询时的长度

用ml,mr表示当前区间的左端点处和右端点处是否有竖向的边,用mm求和,表示区间内需要计算的竖向边的条数

竖向的长度是两个边的高度差乘根节点里的mm

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <bitset>
#define max_ 5010
#define inf 0x3f3f3f3f
#define ll long long
#define les 1e-8
#define mod 364875103
using namespace std;
struct node
{
    int l,r;
    int s;
    int len;
    int ml,mr,mm;
};
struct node tree[max_*8];
struct edge
{
    int l,r;
    int h;
    int f;
    bool operator < (const edge &a)const
    {
        return h<a.h;
    }
};
struct edge e[max_*2];
int n;
int x[max_*2];
void built(int i,int l,int r)
{
    tree[i].l=l;
    tree[i].r=r;
    tree[i].s=tree[i].len=tree[i].mm=tree[i].ml=tree[i].mr=0;
    if(l+1==r)
    return;
    int mid=(l+r)>>1;
    built(i<<1,l,mid);
    built(i<<1|1,mid,r);
}
void up(int i)
{
    if(tree[i].s>=1)
    {
        tree[i].len=x[tree[i].r]-x[tree[i].l];
        tree[i].mm=tree[i].ml=tree[i].mr=1;
    }
    else if(tree[i].l+1==tree[i].r)
    {
        tree[i].len=0;
        tree[i].mm=tree[i].ml=tree[i].mr=0;
    }
    else
    {
        tree[i].len=tree[i<<1].len+tree[i<<1|1].len;
        tree[i].ml=tree[i<<1].ml;
        tree[i].mr=tree[i<<1|1].mr;
        tree[i].mm=tree[i<<1].mm+tree[i<<1|1].mm-tree[i<<1].mr*tree[i<<1|1].ml;
    }
}
void updata(int i,int l,int r,int v)
{
    if(tree[i].l==l&&tree[i].r==r)
    {
        tree[i].s+=v;
        up(i);
        return;
    }
    int mid=(tree[i].l+tree[i].r)>>1;
    if(r<=mid)
    updata(i<<1,l,r,v);
    else if(l>=mid)
    updata(i<<1|1,l,r,v);
    else
    {
        updata(i<<1,l,mid,v);
        updata(i<<1|1,mid,r,v);
    }
    up(i);
}
int main(int argc, char const *argv[]) {
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            int x1,x2,y1,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            int p=i*2-1;
            int q=i*2;
            e[p].l=e[q].l=x1;
            e[p].r=e[q].r=x2;
            e[p].h=y1;
            e[p].f=1;
            e[q].h=y2;
            e[q].f=-1;
            x[p]=x1;
            x[q]=x2;
        }
        sort(x+1,x+1+n*2);
        sort(e+1,e+1+n*2);
        built(1,1,2*n);
        int pre=0,ans=0;
        e[0].h=e[1].h;
        for(int i=1;i<=2*n;i++)
        {
            int l=lower_bound(x+1,x+1+2*n,e[i].l)-x;
            int r=lower_bound(x+1,x+1+2*n,e[i].r)-x;
            ans+=2*tree[1].mm*(e[i].h-e[i-1].h);
            updata(1,l,r,e[i].f);
            ans+=e[i].f*(tree[1].len-pre);
            pre=tree[1].len;
        }
        printf("%d\n",ans );
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值