欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ 1177 Picture 【线段树】【矩形周长并(横纵扫描线、离散化)】

程序员文章站 2022-07-13 11:10:13
...

Picture

Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 13668   Accepted: 7191

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. 

POJ 1177 Picture 【线段树】【矩形周长并(横纵扫描线、离散化)】


The corresponding boundary is the whole set of line segments drawn in Figure 2. 

POJ 1177 Picture 【线段树】【矩形周长并(横纵扫描线、离散化)】


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<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define db double
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
const int MAX = 1e5 + 7;
struct node{
    db x, y, h;
    int flag;
    node() {}
    node(db x, db y, db h, int flag) : x(x), y(y), h(h), flag(flag) {}
    bool operator < (const node &a) const{
        return h < a.h;
    }
} a[2][MAX];
db X[2][MAX], lz[MAX], len[MAX];
void pushup(int rt, int l, int r, int flag){
    if(lz[rt]) len[rt] = X[flag][r + 1] - X[flag][l];
    else if(l == r) len[rt] = 0;
    else len[rt] = len[rt << 1] + len[rt << 1 | 1];
}
void update(int rt, int l, int r, int x, int y, int v, int flag){
    if(x <= l && r <= y){
        lz[rt] += v;
        pushup(rt, l, r, flag);
        return;
    }
    int mid = (l + r) >> 1;
    if(x <= mid) update(lson, x, y, v, flag);
    if(mid < y) update(rson, x, y, v, flag);
    pushup(rt, l, r, flag);
}
int hz[2];
int main(){
    int n;
    while(scanf("%d", &n) != EOF){
        int cnt = 0;
        memset(X, 0, sizeof X);
        db ans = 0.0;
        for(int i = 0; i < n; i++){
            db A, b, c, d;
            scanf("%lf%lf%lf%lf", &A, &b, &c, &d);
            a[0][cnt] = node(A, c, b, 1);
            a[1][cnt] = node(b, d, A, 1);
            X[0][cnt] = A, X[1][cnt++] = b;
            a[0][cnt] = node(A, c, d, -1);
            a[1][cnt] = node(b, d, c, -1);
            X[0][cnt] = c, X[1][cnt++] = d;
        }
        sort(X[0], X[0] + cnt);
        hz[0] = unique(X[0], X[0] + cnt) - X[0];
        sort(X[1], X[1] + cnt);
        hz[1] = unique(X[1], X[1] + cnt) - X[1];
        sort(a[0], a[0] + cnt);
        sort(a[1], a[1] + cnt);
        for(int i = 0; i < 2; i++){
            db res = 0, flag = 0.0;
            memset(lz, 0, sizeof lz);
            memset(len, 0, sizeof len);
            for(int j = 0; j < cnt; j++){
                int l = lower_bound(X[i], X[i] + hz[i], a[i][j].x) - X[i];
                int r = lower_bound(X[i], X[i] + hz[i], a[i][j].y) - X[i] - 1;
                update(1, 0, hz[i] - 1, l, r, a[i][j].flag, i);
                res += fabs(len[1] - flag);
                flag = len[1];
            }
            ans += res;
        }
        printf("%d\n", (int)ans);
    }
    return 0;
}