HDU1828 Picture(线段树+扫描线求周长并)

Picture

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


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
 

Source
题意:给出n块矩形(有覆盖),求他们的周长并。
思路:用线段树+扫描线
类似求覆盖面积的,每一步需要的是什么?是当前底边长度,还有当前线段数量,这样求可以求出该部分的周长。
所以需要用线段树处理一段区间是否是连续的,还有区间内的线段数量。
一段区间如果他的左儿子连续右儿子连续,左儿子和右儿子之间也连续,那么这段区间便是连续的。
一段区间的线段数量由它右儿子的线段数量加上左儿子的线段数量,还有左右儿子之间是否连续,如果是还要加上一。
这样看好像很模糊,可以试试在线段树的图上比划比划,区间如果连续,那么它的左右儿子连续,那么左右儿子的左右儿子也连续,那么直到最底下的点与点就容易看出了,反之由点和点之间的关系,不断地更新出区间的连续性(或线段数量),是一种递归思路。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 20010
using namespace std;
int numseg[maxn<<2], sum[maxn<<2], mark[maxn<<2];
bool lbd[maxn<<2], rbd[maxn<<2];
struct seg{
    int l, r, h, c;
    seg(){}
    seg(int x1, int x2, int y, int d):l(x1),r(x2),h(y),c(d){}
    bool operator<(const seg &a)const{
        return h<a.h;
    }
}s[maxn<<1];
void pushUp(int n, int left, int right){
    if(mark[n]){
        sum[n] = right-left+1;
        numseg[n] = lbd[n] = rbd[n] = 1;
    }else if(left == right){
        numseg[n] = lbd[n] = rbd[n] = sum[n] = 0;
    }else{
        lbd[n] = lbd[n<<1];
        rbd[n] = rbd[n<<1|1];
        sum[n] = sum[n<<1] + sum[n<<1|1];
        numseg[n] = numseg[n<<1] + numseg[n<<1|1] - lbd[n<<1|1]*rbd[n<<1];
    }
}
void update(int L, int R, int rt, int d, int left, int right){
    if(L<=left&&right<=R){
        mark[rt] += d;
        pushUp(rt, left, right);
        return;
    }
    int mid = (left+right)>>1;
    if(L<=mid) update(L, R, rt<<1, d, left, mid);
    if(mid<R) update(L, R, rt<<1|1, d, mid+1, right);
    pushUp(rt, left, right);
}
int main()
{
    int n, i, j, k, x1, x2, y1, y2, last, res;
    while(~scanf("%d", &n))
    {
        int l = 10001, r = -10001;
        k = 0;
        for(i = 0;i < n;i++){
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            l = min(l, x1), r = max(r, x2);
            s[k++] = seg(x1, x2, y1, 1);
            s[k++] = seg(x1, x2, y2, -1);
        }
        last = res = 0;
        sort(s, s+k);
        for(i = 0;i < k;i++){
            update(s[i].l, s[i].r-1, 1, s[i].c, l, r);
            res += 2*numseg[1]*(s[i+1].h-s[i].h) + abs(sum[1]-last);
            last = sum[1];
        }
        printf("%d\n", res);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值