数据结构的半夜——线段树学习笔记2:离散化

今晚完成了kuanbin带你飞的第四道例题,一道运用离散化思想的题目

这里就来总结归纳这道例题

D-Mayor’s posters POJ 2528

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 80590Accepted: 23189

Description

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:

  • Every candidate can place exactly one poster on the wall.
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
  • The wall is divided into segments and the width of each segment is one byte.
  • Each poster must completely cover a contiguous number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.

Your task is to find the number of visible posters when all the posters are placed given the information about posters’ size, their place and order of placement on the electoral wall.

Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,… , ri.

Output

For each input data set print the number of visible posters after all the posters are placed.

The picture below illustrates the case of the sample input.
img

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

精简题意:在10000000长的线段上,依次使用长度不等的线段去覆盖,问可以从最外层看见多少条线段。

这里抽象题意,可以看作在长度为10000000的区间,依次染色区间[l,r],最后剩下多少种颜色

这么长的长度,直接使用nlogn数据结构无法维护,但是我们发现只有10000条线段,所以大部分空间是浪费的,可以通过离散化来压缩空间。

这里的线段树只是一个工具,核心是离散化的过程

我找到一组小数据但是十分典型,可以来模拟我们的离散化过程

依次染色的区间为

[1,10]

[1,4]

[6,10]

未离散化前应该是这么染的

ps: === 代表一格

=== === === === === === === === === === 

=== === === === === === === === === ===

=== === === === ===

					    === === === ===

正确答案为3

如果按普通的离散化方式进行染色

离散化:

1 2 3 4

1 4 6 10

=== === === ===

=== === === ===

=== ===
   
        === ===

而这样却答案变成了2

经过观察发现,由于4与6经离散化后两个区间连在了一起,于是我们将两个若相差一格及以上的点,

加入一个点来表示他们中间的点:

1 2 3 4 5 6 7

1 2 4 5 6 7 10

=== === === === === === ===

=== === === === === === ===

=== === === 

                === === ===

答案就便是正确的了

将离散化的核心代码进行解析

        int tot=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&lx[i],&rx[i]);
            tot++;
            num[tot]=lx[i];//将l,r端点加入数组中
            tot++;
            num[tot]=rx[i];
        }
        sort(num+1,num+1+tot);//进行排序
        int q=unique(num+1,num+1+tot)-num-1;//离散化
        int m=q;//记录元素个数
        for(int i=1;i<=m;i++)
        {
            if(num[i+1]-num[i] > 1) q++,num[q]=num[i]+1;
            //若两个相邻元素相差大于等于1格,加入中间元素
        }
        sort(num+1,num+1+q);//再次排序
        for(int i=1;i<=n;i++)
        {
            L=lower_bound(num+1,num+1+q,lx[i])-num;//找到下标
            R=lower_bound(num+1,num+1+q,rx[i])-num;
            C=i;
            update(1,q,1);//插入线段树
        }

这时统计元素个数时只要看区间的lazytag为多少即可

最后上标程

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define maxn 10005
#define lson th<<1
#define rson th<<1|1
using namespace std;
int vis[maxn<<4];
int tag[maxn<<4];
int lx[maxn<<4];
int rx[maxn<<4];
int num[maxn<<4];
int n;
int L,R,C;
int T;
int ans=0;
void pushdown(int th)
{
    if(tag[th]==0)  return;
    tag[lson]=tag[rson]=tag[th];
    tag[th]=0;
}
void update(int l,int r,int th)
{
    if(L<=l && r<=R)
    {
        tag[th]=C;
        return;
    }
    pushdown(th);
    int mid=l+r >>1;
    if(L<=mid) update(l,mid,lson);
    if(mid<R) update(mid+1,r,rson);
}
void query(int l,int r,int th)
{
    if(tag[th]!=0)
    {
        if(vis[tag[th]]==0) ans++;
        vis[tag[th]]=1;
        return;
    }
    int mid=l+r >>1;
    if(l==r) return;
    query(l,mid,lson);
    query(mid+1,r,rson);
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        memset(vis,0,sizeof(vis));
        memset(tag,0,sizeof(tag));
        memset(num,0,sizeof(num));
        scanf("%d",&n);
        int tot=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&lx[i],&rx[i]);
            tot++;
            num[tot]=lx[i];
            tot++;
            num[tot]=rx[i];
        }
        sort(num+1,num+1+tot);
        int q=unique(num+1,num+1+tot)-num-1;
        int m=q;
        for(int i=1;i<=m;i++)
        {
            if(num[i+1]-num[i] > 1) q++,num[q]=num[i]+1;
        }
        sort(num+1,num+1+q);
        for(int i=1;i<=n;i++)
        {
            L=lower_bound(num+1,num+1+q,lx[i])-num;
            R=lower_bound(num+1,num+1+q,rx[i])-num;
            C=i;
            update(1,q,1);
        }
        ans=0;
        query(1,q,1);
        printf("%d\n",ans);
    }
}

做完这道题后,我对离散化的理解更加的深入与彻底了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值