poj 1177 Picture 【线段树 扫描线 求轮廓周长】

Picture
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 11444 Accepted: 6050

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


题意:给你n个矩形,让你求轮廓周长。


思路一、用线段树 扫描线 求两次周长,纵的一次,横的一次

思路二、只扫描一次,线段树维护与扫描线平行的平面 有多少段连续的线,一段线对应两个垂直分支。


偷懒用了第二种。。。


AC代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (10000+10)
#define MAXM (200000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#define first fi
#define second se
using namespace std;
struct Tree{
    int l, r, len;
    int sum, rownum;
    int lcover, rcover;
    int cover;
};
Tree tree[MAXN<<2];
struct Node{
    int x1, x2, y, cover;
};
bool cmp(Node a, Node b){
    return a.y < b.y;
}
Node num[MAXN];
int rec[MAXN];
void PushUp(int o)
{
    if(tree[o].cover > 0)
    {
        tree[o].sum = tree[o].len;
        tree[o].lcover = tree[o].rcover = 1;
        tree[o].rownum = 1;
    }
    else
    {
        if(tree[o].l == tree[o].r)
        {
            tree[o].sum = 0; tree[o].rownum = 0;
            tree[o].lcover = tree[o].rcover = 0;
        }
        else
        {
            tree[o].lcover = tree[ll].lcover; tree[o].rcover = tree[rr].rcover;
            tree[o].rownum = tree[ll].rownum + tree[rr].rownum;
            tree[o].sum = tree[ll].sum + tree[rr].sum;
            if(tree[ll].rcover && tree[rr].lcover)
                tree[o].rownum--;
        }
    }
}
void Build(int o, int l, int r)
{
    tree[o].l = l; tree[o].r = r;
    tree[o].sum = tree[o].rownum = 0;
    tree[o].cover = tree[o].lcover = tree[o].rcover = 0;
    tree[o].len = rec[tree[o].r+1] - rec[tree[o].l];
    if(l == r)
        return ;
    int mid = (l + r) >> 1;
    Build(lson); Build(rson);
}
void Update(int o, int L, int R, int v)
{
    if(tree[o].l == L &&  R == tree[o].r)
    {
        tree[o].cover += v;
        PushUp(o);
        return ;
    }
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(R <= mid)
        Update(ll, L, R, v);
    else if(L > mid)
        Update(rr, L, R, v);
    else
    {
        Update(ll, L, mid, v);
        Update(rr, mid+1, R, v);
    }
    PushUp(o);
}
int main()
{
    int n;
    while(Ri(n) != EOF)
    {
        int k = 0;
        for(int i = 0; i < n; i++)
        {
            int x1, y1, x2, y2;
            Ri(x1); Ri(y1); Ri(x2); Ri(y2);

            num[k].x1 = x1; num[k].x2 = x2;
            num[k].y = y1;
            num[k].cover = 1; rec[k++] = x1;

            num[k].x1 = x1; num[k].x2 = x2;
            num[k].y = y2;
            num[k].cover = -1; rec[k++] = x2;
        }
        sort(num, num+k, cmp); sort(rec, rec+k);
        int R = unique(rec, rec+k) - rec;
        Build(1, 0, R-1);
        int ans = 0; int last = 0; int x, y;
        for(int i = 0; i < k; i++)
        {
            x = lower_bound(rec, rec+R, num[i].x1) - rec;
            y = lower_bound(rec, rec+R, num[i].x2) - rec;
            if(x <= y-1) Update(1, x, y-1, num[i].cover);
            if(i < k-1) ans += tree[1].rownum * 2 * (num[i+1].y - num[i].y);
            ans += abs(tree[1].sum - last); last = tree[1].sum;
        }
        Pi(ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值