线段树专题----N - Picture---- 扫描线求多矩形周长并

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

扫描线经典应用:
扫描线+线段树通常用来求矩形周长并,面积并,面积。
周长并思路:
我们先计算横向线段的长度,把所有坐标的y轴坐标按照升序排列,扫描线从y的最小值依次向上扫描,求出每次扫描所覆盖的长度,与上一次长度做差得到其绝对值(这个值不需要加到线段树中去),加到最终的答案中去。横向纵向都来一遍就得出最终的答案了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int MAXN = 10020;

int n;
int dis[2][MAXN],sum[MAXN],cover[MAXN];

struct seg{
    int l,r,h;
    int cnt;
    seg(){}
    seg(int l,int r,int h,int cnt):l(l),r(r),h(h),cnt(cnt){}
    friend bool operator < (const seg &p,const seg &q){
        return p.h < q.h;
    }
}s[2][MAXN];

void pushup(int rt,int l,int r,int flag)
{
    if(cover[rt]) sum[rt] = dis[flag][r + 1] - dis[flag][l];
    else if(l == r) sum[rt]= 0;
    else sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

void update(int rt,int l,int r,int ql,int qr,int val,int flag)
{
    if(l >= ql && r <= qr){
        cover[rt] += val;
        pushup(rt,l,r,flag);
        return ;
    }
    int mid = (l + r) >> 1;
    if(ql <= mid) update(rt<<1,l,mid,ql,qr,val,flag);
    if(qr > mid) update(rt<<1|1,mid + 1,r,ql,qr,val,flag);
    pushup(rt,l,r,flag);
}

int main()
{
    scanf("%d",&n);
    int num[3];
    int a,b,c,d;
    for(int i = 1;i <= n;++i){
        scanf("%d %d %d %d",&a,&b,&c,&d);
        dis[0][i] = a;dis[0][i + n] = c;
        dis[1][i] = b;dis[1][i + n] = d;
        s[0][i] = seg(a,c,b,1);
        s[0][i + n] = seg(a,c,d,-1);
        s[1][i] = seg(b,d,a,1);
        s[1][i + n] = seg(b,d,c,-1);
    }
    n *= 2;

    sort(dis[0] + 1,dis[0] + n + 1);
    num[0] = unique(dis[0] + 1,dis[0] + n + 1) - dis[0] - 1;
    sort(dis[1] + 1,dis[1] + n + 1);
    num[1] = unique(dis[1] + 1,dis[1] + n + 1) - dis[1] - 1;

    sort(s[0] + 1,s[0] + n + 1);
    sort(s[1] + 1,s[1] + n + 1);

    int ans = 0;
    for(int i = 0;i < 2;++i){
        int lst = 0;
        memset(sum,0,sizeof(sum));
        memset(cover,0,sizeof(cover));
        for(int j = 1;j <= n;++j){
            int l = lower_bound(dis[i] + 1,dis[i] + num[i] + 1,s[i][j].l) - dis[i];
            int r = lower_bound(dis[i] + 1,dis[i] + num[i] + 1,s[i][j].r) - dis[i];
            update(1,1,num[i],l,r - 1,s[i][j].cnt,i);
            ans += abs(sum[1] - lst);
            lst = sum[1];
        }
    }
    cout << ans << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值