poj 1177 Picture

本文介绍了一个用于计算在墙面上多个矩形拼接形成的边界周长的算法实现过程,详细阐述了输入数据格式、算法流程以及输出结果。通过实例演示了如何使用C++编程语言来解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://poj.org/problem?id=1177

Picture
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 10280 Accepted: 5448

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<vector>
#include<bitset>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<cstdlib>
using namespace std;
#define CLR(A) memset(A,0,sizeof(A))
struct Node{
    int st,ed,m,lbd,rbd;
    int sequence_line,cnt;
}ST[40005];
void build(int st,int ed,int v){
    ST[v].st=st;ST[v].ed=ed;
    ST[v].m=ST[v].lbd=ST[v].rbd=0;
    ST[v].sequence_line=ST[v].cnt=0;
    if(ed-st>1){
        int mid=(st+ed)>>1;
        build(st,mid,2*v+1);
        build(mid,ed,2*v+2);
    }
}
inline void UpDate(int v){
    if(ST[v].cnt>0){
        ST[v].m=ST[v].ed-ST[v].st;
        ST[v].lbd=ST[v].rbd=1;
        ST[v].sequence_line=1;
        return;
    }
    if(ST[v].ed-ST[v].st==1){
        ST[v].m=0;
        ST[v].lbd=ST[v].rbd=0;
        ST[v].sequence_line=0;
    }
    else{
        int left=2*v+1,right=2*v+2;
        ST[v].m=ST[left].m+ST[right].m;
        ST[v].sequence_line=ST[left].sequence_line+ST[right].sequence_line-(ST[left].rbd&ST[right].lbd);
        ST[v].lbd=ST[left].lbd;
        ST[v].rbd=ST[right].rbd;
    }
}
void Insert(int s,int e,int v){
    if( s<=ST[v].st && e>=ST[v].ed){
        ST[v].cnt++;
        UpDate(v);
        return;
    }
    int mid=(ST[v].st+ST[v].ed)>>1;
    if(s<mid) Insert(s,e,2*v+1);
    if(e>mid) Insert(s,e,2*v+2);
    UpDate(v);
}
void Delete(int st,int ed,int v){
    if(st<=ST[v].st&&ed>=ST[v].ed){
        ST[v].cnt--;
        UpDate(v);
        return;
    }
    int mid=(ST[v].st+ST[v].ed)>>1;
    if(st<mid) Delete(st,ed,2*v+1);
    if(ed>mid) Delete(st,ed,2*v+2);
    UpDate(v);
}
struct line{
    int x,y1,y2;bool d;
}a[10003];
bool cmp(line t1,line t2){return t1.x<t2.x;}
void cal_c(int n){
    int i,j,k,t2=0,sum=0;
    a[n]=a[n-1];
    for(int i=0;i<n;i++){
        if(a[i].d==1) Insert(a[i].y1,a[i].y2,0);
        else Delete(a[i].y1,a[i].y2,0);
        sum+=ST[0].sequence_line*(a[i+1].x-a[i].x)*2;
        sum+=abs(ST[0].m-t2);
        t2=ST[0].m;
    }
    printf("%d\n",sum);
}
int main(){
    int n,x1,x2,y1,y2,i,j,suby,upy;
    while(~scanf("%d",&n)){
        j=0;
        suby=10000;upy=-10000;
        for(i=0;i<n;i++){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            a[j].x=x1;a[j].y1=y1;a[j].y2=y2;a[j].d=1;
            j++;
            a[j].x=x2;a[j].y1=y1;a[j].y2=y2;a[j].d=0;
            j++;
            if(suby>y1) suby=y1;
            if(upy<y2) upy=y2;
        }
        sort(a,a+j,cmp);
        build(suby,upy,0);
        cal_c(j);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值