【HDU 1828】Picture 线段树扫描线

Picture

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6289    Accepted Submission(s): 2935


 

Problem 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.

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 25 15 -4 24 14 0 -6 16 4 2 15 10 22 30 10 36 20 34 0 40 16

 

 

Sample Output

 

228

 

题目大意:给出若干个矩形,求这些矩形交叠成的图形的周长。

(如果之前没有了解过扫描线,请移步:https://blog.youkuaiyun.com/hxtscjk/article/details/81051412,是一道扫描线求覆盖面积的题,我个人觉得要比求周长容易理解一些。)

首先我们可以把周长分成两类,一类是横线,一类是竖线。

横线的求法比较简单,从下向上扫描,一边扫描一边将图形向下投影,然后计算加入某条线之前和之后的投影长度差,得出的就是加入了这条线后整个图形增加的周长长度。因为既然投影长度改变了,那么肯定是扫描到了边缘位置,边缘位置就是周长要计算的地方了。

至于竖线的求法,由于它也涉及到了图形相互覆盖,插入又移除的问题,也需要利用线段树来计算。要计算的竖线所在的位置在投影上也有体现,即:投影上的长度是由N条线段组成,就有2*N条竖线需要计算。所以就可以把问题转化成投影中有几条线段了。这样在树中再定义一个计数的变量,和线段在区间内覆盖的左右端点两个变量,在pushup操作中将它们的计算涵盖进入,就能得出最后的答案了。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
    int l,r,pos,s;
}k[10010];
struct node1
{
    int left,right,s;//对应节点的左右端点以及上下边的标记
    int sumd;//图中横线的总长度
    int sumv;//图中被覆盖的部分的块数
    int lmax,rmax;//图中被覆盖部分的最左和最右端点
}tree[80010];
int x[10010];
int qwe(node x,node y)
{
    return x.pos<y.pos;
}
void build_tree(int root,int left,int right)
{
    tree[root].left=left;
    tree[root].right=right;
    tree[root].s=0;
    tree[root].sumd=0;
    tree[root].sumv=0;
    tree[root].lmax=0;
    tree[root].rmax=0;
    if(left==right)return ;
    int mid=(left+right)/2;
    build_tree(root*2,left,mid);
    build_tree(root*2+1,mid+1,right);
}
void push_up(int root)
{
    if(tree[root].s)
    {
        tree[root].lmax=tree[root].left;
        tree[root].rmax=tree[root].right;
        tree[root].sumv=1;
        tree[root].sumd=x[tree[root].right+1]-x[tree[root].left];
    }
    else if(tree[root].left==tree[root].right)
    {
        tree[root].lmax=0;
        tree[root].rmax=0;
        tree[root].sumv=0;
        tree[root].sumd=0;
    }
    else
    {
        tree[root].lmax=tree[root*2].lmax;
        tree[root].rmax=tree[root*2+1].rmax;
        tree[root].sumd=tree[root*2].sumd+tree[root*2+1].sumd;
        if(tree[root*2].rmax==tree[root*2+1].lmax-1)//如果左右端点区间相连
            tree[root].sumv=tree[root*2].sumv+tree[root*2+1].sumv-1;
        else//不相连
            tree[root].sumv=tree[root*2].sumv+tree[root*2+1].sumv;
    }
}
void update_tree(int root,int left,int right,int s)
{
    if(tree[root].left==left&&tree[root].right==right)
    {
        tree[root].s+=s;
        push_up(root);
        return ;
    }
    int mid=(tree[root].left+tree[root].right)/2;
    if(right<=mid)update_tree(root*2,left,right,s);
    else if(left>=mid+1)update_tree(root*2+1,left,right,s);
    else
    {
        update_tree(root*2,left,mid,s);
        update_tree(root*2+1,mid+1,right,s);
    }
    push_up(root);
}
int main()
{
    int n,x1,y1,x2,y2;
    while(~scanf("%d",&n))
    {
        int cntk=1;int cntx=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            k[cntk].l=x1;
            k[cntk].r=x2;
            k[cntk].s=1;
            k[cntk++].pos=y1;
            x[cntx++]=x1;
            k[cntk].l=x1;
            k[cntk].r=x2;
            k[cntk].s=-1;
            k[cntk++].pos=y2;
            x[cntx++]=x2;
        }
        sort(k+1,k+cntk,qwe);
        sort(x+1,x+cntx);
        int num=2;//去重
        for(int i=2;i<cntx;i++)
        {
            if(x[i]!=x[i-1])x[num++]=x[i];
        }
        cntx=num;
        build_tree(1,1,cntk-2);
        int sum=0;int xx=0;
        for(int i=1;i<cntk;i++)
        {
            int l=lower_bound(x+1,x+cntx,k[i].l)-x;
            int r=lower_bound(x+1,x+cntx,k[i].r)-x-1;
            update_tree(1,l,r,k[i].s);
            sum+=abs(tree[1].sumd-xx);//这次和上次投影的长度差
            xx=tree[1].sumd;
            sum+=(tree[1].sumv*(k[i+1].pos-k[i].pos)*2);
        }//再加上覆盖的段数*高度*左右2条线
        printf("%d\n",sum);
    }
}

 

内容概要:《2024年中国城市低空经济发展指数报告》由36氪研究院发布,指出低空经济作为新质生产力的代表,已成为中国经济新的增长点。报告从发展环境、资金投入、创新能力、基础支撑和发展成效五个维度构建了综合指数评价体系,评估了全国重点城市的低空经济发展状况。北京和深圳在总指数中名列前茅,分别以91.26和84.53的得分领先,展现出强大的资金投入、创新能力和基础支撑。低空经济主要涉及无人机、eVTOL(电动垂直起降飞行器)和直升机等产品,广泛应用于农业、物流、交通、应急救援等领域。政策支持、市场需求和技术进步共同推动了低空经济的快速发展,预计到2026年市场规模将突破万亿元。 适用人群:对低空经济发展感兴趣的政策制定者、投资者、企业和研究人员。 使用场景及目标:①了解低空经济的定义、分类和发展驱动力;②掌握低空经济的主要应用场景和市场规模预测;③评估各城市在低空经济发展中的表现和潜力;④为政策制定、投资决策和企业发展提供参考依据。 其他说明:报告强调了政策监管、产业生态建设和区域融合错位的重要性,提出了加强法律法规建设、人才储备和基础设施建设等建议。低空经济正加速向网络化、智能化、规模化和集聚化方向发展,各地应找准自身比较优势,实现差异化发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值