USACO5.5.1 Picture(picture)

先处理所有的横线,横边记录一个pos值,每个矩形有两条横边,下面的pos记为1,上面的pos记为-1。

将所有横边按y坐标由小到大排序,枚举,横坐标上的每个点记录一个值layer。

每遇到一条边i,对于边i上的每一个点x,layer[ x ] += i.pos 。

如果layer[ j ]从0变为1,周长ans++,如果layer从1变为0,同样ans++。

竖线同理,但要注意:矩形的顶点只能算一次,在横边算过了竖边就不算了。


/*
ID:xsy97051
LANG:C++
TASK:picture
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

struct Line
{
    /*init   创建这个struct,直接调用struct line t(a,b,c,d),直接把4个值都弄好了
    bool operator <是定义了一个比较函数,可以方便的对line数组排序或者是用优先队列了*/
    int a,b,h,pos;
    void init(int aa,int bb,int hh,int ppos)
    {
        a=aa;   b=bb;   h=hh;   pos=ppos;
    }

    bool operator <(const Line &l) const{
        if(h!=l.h) return h<l.h;  
        return pos>l.pos;  
    }
};

int n,ans=0;  
Line hor[10010];  
Line ver[10010];  
int layer[20010];  

int main()
{
    freopen("picture.in","r",stdin);
    freopen("picture.out","w",stdout);
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        hor[i<<1].init(a+10000,c+10000,b,1);  
        hor[(i<<1)+1].init(a+10000,c+10000,d,-1);  
        ver[i<<1].init(b+10000,d+10000,a,1);  
        ver[(i<<1)+1].init(b+10000,d+10000,c,-1);  
    }
    n<<=1;  
    sort(hor,hor+n);  
    sort(ver,ver+n);  
 
    for(int i=0;i<n;i++)
    {
        Line l=hor[i];
        for(int j=l.a;j<l.b;j++)
        {
            if(!layer[j] && layer[j]+l.pos==1) ans++;  
            if(layer[j]==1 && layer[j]+l.pos==0) ans++;  
            layer[j]+=l.pos;  
        }
    }
 
    memset(layer,0,sizeof(layer));  
 
    for(int i=0;i<n;i++)
    {  
        Line l=ver[i];  
        for(int j=l.a;j<l.b;j++)
        {  
            if(!layer[j] && layer[j]+l.pos==1) ans++;  
            if(layer[j]==1 && layer[j]+l.pos==0) ans++;  
            layer[j]+=l.pos;  
        }  
    }  
   
    cout<<ans<<endl;
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值