Picture HDU 1828

题目链接:点这里


    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−10000,10000 and any existing rectangle has a positive area. 

    Please process to the end of file.

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

题意:

给你个矩形的坐标,要你求这n个矩形所围成的大矩形的周长.

思路:

线段树之扫面线求周长.将x轴坐标离散化,并且对x轴和有轴坐标进行排序,每次维护所扫面到的区间的x轴坐标的长度和线的个数.

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#define lson cur<<1,l,mid
#define rson cur<<1|1,mid+1,r
using namespace std;

typedef long long LL;
const int maxn = 10000+10;
int X[maxn];

struct ss{
    int lx,rx,y,t;
    bool operator < (const ss q){
    return y < q.y;
    }ss (){};
    ss(int l,int r,int y, int t)
    :lx(l),rx(r),y(y),t(t){};
}x[maxn];

struct ssr{
    int len,lp,rp,num,cnt;//cnt标记线是入边还是出边.
    ssr(){};
    ssr(int len,int lp,int rp,int num,int cnt)
    :len(len),lp(lp),rp(rp),num(num),cnt(cnt){};
}a[maxn<<2];

void built(int cur, int l, int r){
    a[cur] = ssr(0,0,0,0,0);
    if(l == r) return;
    int mid = (l + r) >> 1;
    built(lson);
    built(rson);
}

void pushup(int cur,int l,int r){
    if(a[cur].cnt){
        a[cur].len = X[r+1] - X[l] ;//x轴上的线段长度
        a[cur].lp = a[cur].rp = 1;
        a[cur].num = 1;
    } else if (l == r){
        a[cur].len = 0;
        a[cur].lp = a[cur].rp = 0;
        a[cur].num = 0;
    } else {
        a[cur].len = a[cur<<1].len + a[cur<<1|1].len;
        a[cur].lp = a[cur<<1].lp;
        a[cur].rp = a[cur<<1|1].rp;
        a[cur].num = a[cur<<1].num + a[cur<<1|1].num - (a[cur<<1].rp & a[cur<<1|1].lp);//线段数量
    }
}

void update(int cur,int l,int r,int ll, int rr,int v){
    if(l >= ll && r <= rr){
        a[cur].cnt += v;
        pushup(cur,l,r);
        return ;
    } int  mid = (l +r) >> 1;
    if(mid < rr) update(rson, ll , rr, v);
    if(ll <= mid) update(lson, ll ,rr, v);
    pushup(cur,l,r);
}

int main(){
    int n;
    while(scanf("%d", &n) != EOF){
        if (n == 0){
            printf("0\n");
            continue;
        }int k = 0;
        for(int i = 0; i < n; ++i){
            int x1,y1,x2,y2;
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            x[++k] = ss(x1,x2,y1,1);X[k] = x1;
            x[++k] = ss(x1,x2,y2,-1);X[k] = x2;
        }sort(x+1,x+1+k);//排序,将x轴坐标进行离散化
        sort(X+1,X+1+k);
        int num = 1;
        for(int i = 2; i <= k; ++i)
            if(X[i] != X[i-1])
                X[++num] = X[i];
        built(1,1,num);
        int ans = 0,res = 0;
        for(int i = 1; i <= k; ++i){
            int l = lower_bound(X + 1, X + 1 + num, x[i].lx)-X;
            int r = lower_bound(X + 1, X + 1 + num, x[i].rx) - X - 1;
            update(1,1,k, l, r, x[i].t);
            ans += abs(a[1].len - res);
            if (i < k) ans += (x[i+1].y - x[i].y) * (2 * a[1].num);
            res = a[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、付费专栏及课程。

余额充值