線段樹 離散化

線段樹是一個二叉查找樹

線段樹一般解決的問題是有關區間的動態修改和查詢的問題。
首先要明確的是二叉查找樹的性質,有點類似於二分查找的過程,可以仔細理解一下,下面說一下建立一棵樹可以直接用數組來保存每個點,因爲二叉樹的性質,對於每個點來說,他的左孩子的編號是當前節點編號的二倍,表示爲:rt<<2rt<<2,右孩子編號爲其左孩子編號+1,表示爲:rt<<1|1rt<<1|1
那麼在更新操作的時候有兩種寫法,兩種理解,第二種不是很好理解,但是寫法簡單,且不容易出錯。下面介紹一下這兩種寫法:
但是注意無論是哪種寫法,建圖的時候都是將中間節點歸到左孩子。

1.需要截斷區間

1.第一種理解是每次都將區間截取,考慮,如果所查詢的區間剛好覆蓋當前節點的話就將這個點的域(col[rt])直接更新;
2.取當前節點的中間點m=(l+r)>>1m=(l+r)>>1
3.如果要查詢的區間的右端點RR滿足R<=mR<=m 則只用更新左孩子,如果要查詢的區間的左端點LL滿足L>mL>m 則只用更新右孩子
4.如果上面都不滿足,需要截取區間,更新左孩子,此時查找區間爲LLmm,然後再跟新右孩子,此時的查找區間爲m+1m+1R
代碼:

Update(int L, int R, int c, int l, int r, int rt)//L,R是要查詢的區間,l,r是當前點的區間
{
    if(L==l&&R==r){
        col[rt] = c;
        return;
    }
    int m = (l+r)>>1;
    if(R<=m) Update(L,R,c,l,m,rt<<1);
    else if(L>m) Update(L,R,c,m+1,r,rt<<1|1);
    else {
        Update(L,m,c,l,m,rt<<1);
        Update(m+1,R,c,m+1,r,rt<<1|1);
    }
}

2.不用截斷區間

1.如果要詢問的區間覆蓋這個區間,即L<=l&&R>=rL<=l&&R>=r則對於這個點是一定要整個點更新的,所以直接更新並返回就可以了
2.取當前節點的中間點m=(l+r)>>1m=(l+r)>>1
3.如果當前要詢問的點的右端點超過了中間節點的話即R>mR>m,說明右邊的節點有需要更新的點,所以要將右孩子更新。
4.如果當前要詢問的點的左端點超過了中間節點的話即L<=mL<=m,說明左邊的節點有需要更新的點,所以要將左孩子更新。

這樣代碼比較簡單,如下:

void Update(int L, int R, int l, int r, int c, int rt)
{
    if( L <= l && R >= r )
    {
        col[rt] = c;
        return;
    }
    if(col[rt]!=-1) PushDown(rt);
    int m = (l+r)>>1;
    if(L <= m ) Update(L,R,l,m,c,rt<<1);
    if(R > m) Update(L,R,m+1,r,c,rt<<1|1);
}

離散化:

離散化就是把原有的大二叉樹壓縮成小二叉樹,但是壓縮前後子區間關係不變。

例題:Mayor’s posters
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 l i 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 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+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.
在這裏插入圖片描述

Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4

代碼①:
對於數字的離散化離散化可以想到,是將這些數字排序,對應的下標就是離散化結果,所以在找對應的下標的時候都要用到的是二分查找。但是這個題要注意一個問題,就是考慮110,14,6~10這三個大字報來說如果按照常規的離散化的思路來說的話,會出現問題,就是中間的空隙值沒有離散化,爲了避免這樣的情況,我們每次講這個點的右端點加1的值也加入這個數組,一起裏散話,既可以避免這個問題了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N = 10005;
#define dd puts("haha");
int li[N],ri[N];
int x[N<<3];
int col[N<<4];
bool Hash[N];
void PushDown(int rt){
    col[rt<<1] = col[rt];
    col[rt<<1|1] = col[rt];
    col[rt] = -1;
    return;
}
void Update(int L, int R, int l, int r, int c, int rt)
{
    if( L <= l && R >= r ) {
        col[rt] = c;
        return;
    }
    if(col[rt]!=-1) PushDown(rt);
    int m = (l+r)>>1;
    if(L <= m ) Update(L,R,l,m,c,rt<<1);
    if(R > m) Update(L,R,m+1,r,c,rt<<1|1);
}
int ans;
void query(int l, int r, int rt)
{
    if(l==r||~col[rt]){//~col[rt]相當於col[rt]!=-1;因爲~是按位取反的意思
        if(!Hash[col[rt]]&&(col[rt]!=-1)){
            ans++;
            Hash[col[rt]] = 1;
        }
        return;
    }
    if(~col[rt])PushDown(rt);
    int m = (l+r)>>1;
    query(l,m,rt<<1);
    query(m+1,r,rt<<1|1);
}
int BSearch(int l, int r, int c)
{
    int m;
    while(l<=r){
        m =(l+r)>>1;
        if(x[m]==c) return m;
        else if(x[m]<c) l = m+1;
        else if(x[m]>c) r = m-1;
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(col,-1,sizeof(col));
        memset(Hash,0,sizeof(Hash));
        int n;
        scanf("%d",&n);
        int cnt = 1;
        for(int i = 1; i <=n; i++)
        {
            scanf("%d %d",&li[i],&ri[i]);
            x[cnt++] = li[i];
            x[cnt++] = ri[i];
            x[cnt++] = ri[i]+1;
        }
        sort(x+1,x+cnt+1);
        int m = unique(x+1,x+cnt+1)-x-1;
        for(int i = 1; i <= n; i++){
            int ll = BSearch(1,m,li[i]);
            int rr = BSearch(1,m,ri[i]);
            //printf("(%d %d)\n",ll,rr);
            Update(ll,rr,1,m,i,1);
        }
        //dd;
        ans = 0;
        query(1,m,1);
        printf("%d\n",ans);
    }
    return 0;
}

代碼②:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n,l[10005],r[10005];
int x[20005],vis[10005],a[80005];

void update(int k,int l,int r,int ll,int rr,int val)
{
    if(l>=ll&&r<=rr)
    {
        a[k]=val;
        return ;
    }
    if(a[k])
    {
        a[k<<1]=a[k<<1|1]=a[k];
        a[k]=0;
    }
    int mid=(l+r)>>1;
    if(ll<=mid) update(k<<1,l,mid,ll,rr,val);
    if(rr>mid) update(k<<1|1,mid+1,r,ll,rr,val);
}

void query(int k,int l,int r)
{
    if(a[k]) {vis[a[k]]=1;return ;}
    if(l==r) return ;
    int mid=(l+r)>>1;
    query(k<<1,l,mid);
    query(k<<1|1,mid+1,r);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int cnt=1,ans=0;
        memset(vis,0,sizeof vis);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&l[i],&r[i]);
            x[cnt++]=l[i];
            x[cnt++]=r[i];
        }
        sort(x+1,x+cnt);
        int m=unique(x+1,x+cnt)-x-1;
        memset(a,0,sizeof a);
        for(int i=1;i<=n;i++)
        {
            int ll=lower_bound(x+1,x+m,l[i])-x;
            int rr=lower_bound(x+1,x+m,r[i])-x;
            update(1,1,m,ll,rr,i);
        }
        query(1,1,m);
        for(int i=1;i<=n;i++)
            if(vis[i]) ans++;
        printf("%d\n",ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章