POJ 1177 Picture(扫描线求周长)

17 篇文章 0 订阅
15 篇文章 0 订阅

与求面积并的差不多,但是这个与扫描的方向相同的情况不太好处理,如果扫描线离散化两次扫两遍其实也可以解决这个问题,但是这样无论在时间还是空间上稍微就有点浪费了啊。这里因为我是离散x坐标的所以对于平行于y轴的方向上的统计比较难统计。处理的方法是:标记区间左边的断点,和右边的断点,求出这个区间一共有多少个断点。就可以统计出平行于y轴的长度了。这里合并的时候需要判断右边的左区间和左边的右区间是否相同,如果相同的话,说明他们连接到了一起,要减去多加的。

Picture
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 10300 Accepted: 5462

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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-12
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

using namespace std;

const int maxn = 50100;

struct node
{
    int l, r;
    int h;
    int x;
} f[maxn];

int dc[maxn];
int cnt[maxn<<2];
int sum[maxn<<2];
int ans[maxn<<2];
int Left[maxn<<2];
int Right[maxn<<2];

bool cmp(node a, node b)
{
    return a.h < b.h;
}

int Find(int x, int a[], int n)
{
    int l = 0;
    int r = n-1;
    while(l <= r)
    {
        int mid = (l+r)>>1;
        if(a[mid] == x) return mid;
        else if(a[mid] < x) l = mid+1;
        else r = mid-1;
    }
    return -1;
}

void Up(int l, int r, int site)
{
    if(cnt[site])
    {
        sum[site] = dc[r+1]-dc[l];
        Left[site] = Right[site] = 1;
        ans[site] = 2;
    }
    else if(l == r)
    {
        sum[site] = 0;
        ans[site] = 0;
        Left[site] = Right[site] = 0;
    }
    else
    {
        sum[site] = sum[site<<1]+sum[site<<1|1];
        ans[site] = ans[site<<1]+ans[site<<1|1];
        Left[site] = Left[site<<1];
        Right[site] = Right[site<<1|1];
        if(Left[site<<1|1] && Right[site<<1]) ans[site] -= 2;
    }
}


void Update(int l, int r, int L, int R, int d, int site)
{
    if(L <= l && r <= R)
    {
        cnt[site] += d;
        Up(l, r, site);
        return;
    }
    int mid = (l+r)>>1;
    if(L <= mid) Update(l, mid, L, R, d, site<<1);
    if(R > mid) Update(mid+1, r, L, R, d, site<<1|1);
    Up(l, r, site);
}
int main()
{
    int n;
    while(cin >>n)
    {
        if(!n) break;
        int x1, y1, x2, y2;
        int m = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%d %d %d %d",&x1, &y1, &x2, &y2);
            dc[m] = x1;
            f[m].l = x1;
            f[m].r = x2;
            f[m].h = y1;
            f[m++].x = 1;

            dc[m] = x2;
            f[m].l = x1;
            f[m].r = x2;
            f[m].h = y2;
            f[m++].x = -1;
        }
        sort(dc, dc+m);
        sort(f, f+m, cmp);
        int k = unique(dc, dc+m)-dc;
        memset(cnt, 0, sizeof(cnt));
        memset(sum, 0, sizeof(sum));
        memset(ans, 0, sizeof(ans));
        memset(Left, 0, sizeof(Left));
        memset(Right, 0, sizeof(Right));
        int res = 0;
        int x = 0;
        for(int i = 0; i < m; i++)
        {
            int l = Find(f[i].l, dc, k);
            int r = Find(f[i].r, dc, k)-1;
            if(l <= r) Update(0, k-1, l, r, f[i].x, 1);
            if(i+1 < m) res += ans[1]*(f[i+1].h-f[i].h);
            res += abs(sum[1]-x);
            x = sum[1];
        }
        cout<<res<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值