hdu1828-Picture 线段树+扫描线 求周长并

Picture

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5072    Accepted Submission(s): 2453


Problem 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.

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
题目大意:给你一些矩形,给出两个x坐标和两个y的坐标,然后需要求最外围的部分的周长并。
解题思路:
我们对图形进行两次扫描操作,分别是从下到上,从左到右,然后将得到的结果加起来就是这个形状的周长了。和求面积并的思想是差不多的。
ac代码:
#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 0,20004,1

int const MP = 10000 + 5;
int const MX = 2e4 + 5;

int cnt[MX << 2];
int S[MX << 2];

struct Que {
    int d;
    int top, L, R;
    Que() {}
    Que(int _top, int _L, int _R, int _d) {
        top = _top; L = _L; R = _R; d = _d;
    }
    bool operator<(const Que &b)const {
        return top < b.top;
    }
} A[MX], B[MX];

void push_up(int l, int r, int rt) {
    if(cnt[rt]) S[rt] = r + 1 - l;
    else if(l == r) S[rt] = 0;
    else S[rt] = S[rt << 1] + S[rt << 1 | 1];
}

void update(int L, int R, int d, int l, int r, int rt) {
    if(L <= l && r <= R) {
        cnt[rt] += d;
        push_up(l, r, rt);
        return;
    }

    int m = (l + r) >> 1;
    if(L <= m) update(L, R, d, lson);
    if(R > m) update(L, R, d, rson);
    push_up(l, r, rt);
}

int main() {
    int n, ansk = 0;
    while(~scanf("%d", &n)) {
        memset(cnt, 0, sizeof(cnt));
        memset(S, 0, sizeof(S));

        for(int i = 1; i <= n; i++) {
            int x1, y1, x2, y2;
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            x1 += MP; y1 += MP; x2 += MP; y2 += MP;
            A[i] = Que(y1, x1, x2, 1);//从左往右扫描 
            A[i + n] = Que(y2, x1, x2, -1);
            B[i] = Que(x1, y1, y2, 1);//由下往上扫描 
            B[i + n] = Que(x2, y1, y2, -1);
        }
        sort(A + 1, A + 1 + 2 * n);
        sort(B + 1, B + 1 + 2 * n);
        //从左往右扫描 
        int ans = 0, last = 0;
        for(int i = 1; i <= 2 * n; i++) {
            update(A[i].L, A[i].R - 1, A[i].d, root);
            ans += abs(last - S[1]);
            last = S[1];
        }

        last = 0;
        memset(cnt, 0, sizeof(cnt));
        memset(S, 0, sizeof(S));
        //由下往上扫描 
        for(int i = 1; i <= 2 * n; i++) {
            update(B[i].L, B[i].R - 1, B[i].d, root);
            ans += abs(last - S[1]);
            last = S[1];
        }
        printf("%d\n", ans);
    }
    return 0;
}

题目链接: 点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1828
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值