hdu 1828 Picture 线段树+离散化(其区间的个数)

题意:

给定n个矩形,求这些矩形所构成图形的周长。

题解:

跟hdu1542很像,那道题是求构成图形的面积,我一开始也以为是面积,最后求了之后才发现时周长尴尬。不过那题的代码还是有用的,这题相当于将那道题代码的二倍化。

我们先离散化矩形各条竖直线的y坐标,然后根据x坐标排序,我们可以发现相邻的x坐标间的图形是一个或者多个矩形,那么图形的上下方向上的周长怎么求呢?我们很容易想到ans=∑(xi-xj)*w*2(其中j=i-1,而w表示被两个横坐标分割区域的矩形数目),那么我们就能转换下,每次碰到左边的时候维护线段树[yi,yj](这条边所覆盖的离散化区间)区域加一,碰到右边的时候减一。那么矩形个数就相当于线段树上非0段的个数了。同理,我们将x坐标离散化,排序y坐标可以求得左右方向上的周长。

线段树结点介绍:

p表示该段区间左边是否有非0数,q表示该段区间右边是否有非0数,w表示该段区间的非0段个数,cov表示覆盖情况。




代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
using namespace std;

const int maxn=1e4+10;
struct node{
    int l,r,cov;
    int p,q,w;
}e[maxn*4];
struct segment{
    int x,up,down;
    int cov;
}f[maxn],g[maxn];
int y[maxn],x[maxn];
int cmp(segment a,segment b)
{
    return a.x<b.x;
}
void build(int a,int b,int c)
{
    e[c].l=a;e[c].r=b;
    e[c].w=e[c].p=e[c].q=e[c].cov=0;
    if(a==b)return ;
    int mid=(a+b)/2;
    build(a,mid,2*c);
    build(mid+1,b,2*c+1);
}
void push_up(int c)
{
    if(e[2*c].q==1&&e[2*c+1].p==1)e[c].w=e[2*c].w+e[2*c+1].w-1;
    else e[c].w=e[2*c].w+e[2*c+1].w;
    e[c].p=e[2*c].p;e[c].q=e[2*c+1].q;
}
void update(int a,int b,int c,int val)
{
    //printf("%d\n",c);
    if(e[c].l==a&&e[c].r==b)
    {
        e[c].cov+=val;
        if(e[c].cov!=0)e[c].w=e[c].p=e[c].q=1;
        else
        {
            if(e[c].l==e[c].r)e[c].w=e[c].p=e[c].q=0;
            else push_up(c);
        }
        return ;
    }
    int mid=(e[c].l+e[c].r)/2;
    if(b<=mid)update(a,b,2*c,val);
    else if(a>mid)update(a,b,2*c+1,val);
    else
    {
        update(a,mid,2*c,val);
        update(mid+1,b,2*c+1,val);
    }
    if(e[c].cov==0)
    push_up(c);
}
int main()
{
    int n,tt=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)break;
        int i,j,k,t=0,tt=0,l,r;
        int x1,y1,x2,y2;
        for(i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            y[++t]=y1;f[t].x=x1;f[t].up=y2;f[t].down=y1,f[t].cov=1;
            y[++t]=y2;f[t].x=x2;f[t].up=y2;f[t].down=y1,f[t].cov=-1;
            x[++tt]=x1;g[tt].x=y1;g[tt].up=x2;g[tt].down=x1;g[tt].cov=1;
            x[++tt]=x2;g[tt].x=y2;g[tt].up=x2;g[tt].down=x1;g[tt].cov=-1;
        }
        int ans=0;
        sort(y+1,y+t+1);
        sort(f+1,f+t+1,cmp);
        build(1,t,1);
        for(i=1;i<=t;i++)
        {
            //printf("%d:%d\n",i,e[1].w);
            ans+=(f[i].x-f[i-1].x)*e[1].w*2;
            l=lower_bound(y+1,y+t+1,f[i].down)-y;
            r=lower_bound(y+1,y+t+1,f[i].up)-y-1;
            update(l,r,1,f[i].cov);
        }
        //printf("%d\n",ans);
        sort(x+1,x+tt+1);
        sort(g+1,g+tt+1,cmp);
        build(1,tt,1);
        for(i=1;i<=tt;i++)
        {
            //printf("%d:%d\n",i,e[1].w);
            ans+=(g[i].x-g[i-1].x)*e[1].w*2;
            l=lower_bound(x+1,x+tt+1,g[i].down)-x;
            r=lower_bound(x+1,x+tt+1,g[i].up)-x-1;
            update(l,r,1,g[i].cov);
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值