hdu1828 Picture 扫描线

本文介绍了HDU1828 Picture问题的解决方案,重点讲解了利用扫描线算法来处理图形的填充。通过实例输入和输出,详细阐述了算法的实现过程,帮助读者理解如何应用扫描线技术解决此类问题。

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

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

做了好几天竟然发现是一个变量写错了。。。
让你秋的是图形的周长,你只需要上下扫描一遍左右扫描一遍就行了。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int y[100000];
int x[100000];
struct node
{
    int l,r,flag;
    int  yl,yr,s;
} tree[44000];
struct w
{
    int x,yl,yr;
    int flag;
} p[44000],tem,q[44000];
int cmp(struct w a,struct w b)
{
    return a.x<b.x;
}
void build(int left,int right,int root)
{
    tree[root].l=left;
    tree[root].r=right;
    tree[root].s=tree[root].flag=0;
    tree[root].yl=y[left];
    tree[root].yr=y[right];
    if(left+1==right)
        return;
    int mid=(left+right)>>1;
    build(left,mid,root<<1);
    build(mid,right,root<<1|1);
}
void build2(int left,int right,int root)
{
    tree[root].l=left;
    tree[root].r=right;
    tree[root].s=tree[root].flag=0;
    tree[root].yl=x[left];
    tree[root].yr=x[right];
    if(left+1==right)
        return;
    int mid=(left+right)>>1;
    build2(left,mid,root<<1);
    build2(mid,right,root<<1|1);
}
void len(int t)
{
    if(tree[t].flag>0)
    {
        tree[t].s=tree[t].yr-tree[t].yl;
        return ;
    }
    if(tree[t].l+1==tree[t].r)
        tree[t].s=0;
    else
        tree[t].s=tree[t<<1].s+tree[t<<1|1].s;
}
void update(int t,w e)
{
    if(e.yl==tree[t].yl&&e.yr==tree[t].yr)
    {
        tree[t].flag+=e.flag;
        len(t);
        return;
    }
    if(e.yr<=tree[t<<1].yr)
        update(t<<1,e);
    else if(e.yl>=tree[t<<1|1].yl)
        update(t<<1|1,e);
    else
    {
        w tmp=e;
        tmp.yr=tree[t<<1].yr;
        update(t<<1,tmp);
        tmp=e;
        tmp.yl=tree[t<<1|1].yl;
        update(t<<1|1,tmp);
    }
     len(t);
}
int main()
{
    int t,cnt,cnt2;
    int x1,x2,y1,y2;
    while(~scanf("%d",&t)&&t)
    {
        cnt=1;
        cnt2=1;
        while(t--)
        {
            scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
            q[cnt2].x=x1;
            q[cnt2].yl=y1;
            q[cnt2].yr=y2;
            q[cnt2].flag=1;
            x[cnt2++]=y1;
            q[cnt2].x=x2;
            q[cnt2].yl=y1;
            q[cnt2].yr=y2;
            q[cnt2].flag=-1;
            x[cnt2++]=y2;

            p[cnt].x=y1;
            p[cnt].yl=x1;
            p[cnt].yr=x2;
            p[cnt].flag=1;
            y[cnt++]=x1;
            p[cnt].x=y2;
            p[cnt].yl=x1;
            p[cnt].yr=x2;
            p[cnt].flag=-1;
            y[cnt++]=x2;
        }
        sort(p+1,p+cnt,cmp);
        sort(y+1,y+cnt);
        build(1,cnt-1,1);
        update(1,p[1]);
        int  sum=tree[1].s;
        int low=tree[1].s;
        for(int i=2; i<cnt; i++)
        {
            update(1,p[i]);
            sum+=abs(low-tree[1].s);
            low=tree[1].s;
        }
        sort(q+1,q+cnt2,cmp);
        sort(x+1,x+cnt2);
        build2(1,cnt2-1,1);
        update(1,q[1]);
          sum+=tree[1].s;
          low=tree[1].s;
        for(int i=2; i<cnt2; i++)
        {
            update(1,q[i]);
            sum+=abs(low-tree[1].s);
             low=tree[1].s;
        }
        printf("%d\n",sum);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值