Picture HDU 1828

来源:互联网 发布:iphone7专业拍照软件 编辑:程序博客网 时间:2024/05/24 03:01

题目链接:点这里


    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 2515 -4 24 140 -6 16 42 15 10 2230 10 36 2034 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,rusing 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;}
原创粉丝点击