poj 1177 Picture

本文介绍了一种计算多个矩形粘贴在墙上形成图形的周长的方法,通过水平和垂直扫描线算法来确定边界长度,提供了两种实现方式:暴力算法和线段树算法。
Picture
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 12522 Accepted: 6605

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

题意:求几个矩形并的周长

对横着的和竖着的分别求一次
暴力算法:
对于每一条横边从左到右扫一遍,如果是矩形的下边,先判断当前坐标覆盖数为0,ans+,然后当前坐标覆盖数+1(覆盖数不为0也要加)
如果是矩形的上边,先判断当前坐标覆盖数为1,ans++,然后当前坐标覆盖数-1(覆盖数不为1也要减)
对于每一条竖边,同理(下边改成左边,上边改成右边)
注:1、边[l,r]的扫描范围为[l,r-1]
2、排序时,若几条横边的高度一样,上边优先。同理,竖边左边优先。
原因:这种情况下是一个矩形的下边和另一个矩形的上边重合,两条边的长度都不能算。
如果先扫下边,扫下边之前一定扫了上边,坐标覆盖可能为1,导致答案错误累加

线段树做法:
类似于求矩形并的面积:http://www.cnblogs.com/TheRoadToTheGold/p/6828162.html
不同点:累加答案累加与上一次的差,不需要再乘高度
注:1、边[l,r]的扫描范围是[l,r-1]
2、排序时,若几条边高度一样,谁在前无所谓。因为累加与上一次的差

暴力法:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define delta 10001
using namespace std;
int n,x1,x2,y1,y2;
int g[20005],ans;
struct ways
{
    int s,t,h,f;
}x[20005],y[20005];
bool cmp(ways p,ways q)
{
   if(p.h!=q.h)    return p.h<q.h;
   return p.f>q.f;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        x1+=delta; y1+=delta; x2+=delta; y2+=delta;
        x[i*2-1].s=x1; x[i*2-1].t=x2; x[i*2-1].h=y1; x[i*2-1].f=1;
        x[i*2].s  =x1; x[i*2].t  =x2; x[i*2].h  =y2; x[i*2].f=-1;
        y[i*2-1].s=y1; y[i*2-1].t=y2; y[i*2-1].h=x1; y[i*2-1].f=1;
        y[i*2].s  =y1; y[i*2].t  =y2; y[i*2].h  =x2; y[i*2].f=-1;
    }
    sort(x+1,x+2*n+1,cmp);
    sort(y+1,y+2*n+1,cmp);
    int l,r;
    for(int i=1;i<=2*n;i++)
    {
        l=x[i].s; r=x[i].t;
        for(int j=l;j<r;j++)
         if(x[i].f==1)
         {
             if(!g[j]) ans++;
             g[j]++;
         }
         else
         {
             if(g[j]==1) ans++;
             g[j]--;
         }
    }
    memset(g,0,sizeof(g));
    for(int i=1;i<=2*n;i++)
    {
        l=y[i].s; r=y[i].t;
        for(int j=l;j<r;j++)
         if(y[i].f==1)
         {
             if(!g[j]) ans++;
             g[j]++;
         }
         else
         {
             if(g[j]==1) ans++;
             g[j]--;
         }
    }
    printf("%d",ans);
}

 

线段树法:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define delta 10001
using namespace std;
int n,x1,x2,y3,y2;
int ans;
int opl,opr,maxnx,maxny;
struct ways
{
    int s,t,h,f;
}x[20005],y[20005];
struct node
{
    int sum,f;
}tr[20005*4]; 
bool cmp(ways p,ways q)
{
   return p.h<q.h;
}
void up(int k,int l,int r)
{
    if(tr[k].f) tr[k].sum=r-l+1;
    else if(l==r) tr[k].sum=0;
    else tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
}
void solve(int k,int l,int r,int w)
{
    if(l>=opl&&r<=opr)
    {
        tr[k].f+=w;
        up(k,l,r);
        return ;
    }
    int mid=l+r>>1;
    if(opl<=mid) solve(k<<1,l,mid,w);
    if(opr>mid) solve(k<<1|1,mid+1,r,w);
    up(k,l,r);
}
int main()
{
    scanf("%d",&n);
    ans=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d%d",&x1,&y3,&x2,&y2);
        x1+=delta; y3+=delta; x2+=delta; y2+=delta;
        x[i*2-1].s=x1; x[i*2-1].t=x2; x[i*2-1].h=y3; x[i*2-1].f=1; 
        x[i*2].s  =x1; x[i*2].t  =x2; x[i*2].h  =y2; x[i*2].f=-1;  
        y[i*2-1].s=y3; y[i*2-1].t=y2; y[i*2-1].h=x1; y[i*2-1].f=1;
        y[i*2].s  =y3; y[i*2].t  =y2; y[i*2].h  =x2; y[i*2].f=-1;
        maxnx=max(maxnx,x2); maxny=max(y2,maxny);
    }
    sort(x+1,x+2*n+1,cmp);
    sort(y+1,y+2*n+1,cmp);
    int last=0;
    for(int i=1;i<=2*n;i++)
    {
        opl=x[i].s; opr=x[i].t-1;
        if(x[i].f==1) solve(1,1,maxnx,1);
        else solve(1,1,maxnx,-1);
        ans+=abs(last-tr[1].sum);
        last=tr[1].sum;;
    }
    memset(tr,0,sizeof(tr));
    last=0;
    for(int i=1;i<=2*n;i++)
    {
        opl=y[i].s; opr=y[i].t-1;
        if(y[i].f==1) solve(1,1,maxny,1);
        else solve(1,1,maxny,-1);
        ans+=abs(last-tr[1].sum);
        last=tr[1].sum;
    }
    printf("%d",ans);
}

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/6841804.html

(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值