1. 程式人生 > >資料結構的半夜——線段樹學習筆記2:離散化

資料結構的半夜——線段樹學習筆記2:離散化

資料結構的半夜——線段樹學習筆記2:離散化

今晚完成了kuanbin帶你飛的第四道例題,一道運用離散化思想的題目

這裡就來總結歸納這道例題

D-Mayor's posters

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 80590 Accepted: 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);
    }
}

做完這道題後,我對離散化的理解更加的深入與徹底了