天天看點

POJ - 2528 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.

POJ - 2528 Mayor's posters 線段樹+特殊離散化+區間更新

Sample Input

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

Sample Output

4
      

思路:

因為區間最大為10000000,如果不預處理直接存的話一方面會爆記憶體,另一方面肯定也會逾時,是以我們需要預處理下,用離散化将區間範圍縮小。但是直接離散化跑線段樹的話會不對。因為一個點表示的是一個塊。

這裡引用别人部落格的反例:

如三張海報為:1~10 1~4 6~10

離散化時 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10

第一張海報時:牆的1~4被染為1;

第二張海報時:牆的1~2被染為2,3~4仍為1;

第三張海報時:牆的3~4被染為3,1~2仍為2。

最終,第一張海報就顯示被完全覆寫了,于是輸出2,但實際上明顯不是這樣,正确輸出為3。

新的離散方法為:在相差大于1的數間加一個數,例如在上面1 4 6 10中間加5(算法中實際上1,4之間,6,10之間都新增了數的)

X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 5, X[ 4 ] = 6, X[ 5 ] = 10

這樣之後,第一次是1~5被染成1;第二次1~2被染成2;第三次4~5被染成3

最終,1~2為2,3為1,4~5為3,于是輸出正确結果3。

引用部落格:https://blog.csdn.net/zezzezzez/article/details/80230026

然後就是寫線段樹區間更新了。

代碼如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=1e5+5;
int n,t;
struct qu
{
    int l,r;
};
qu a[maxn];
int b[maxn<<2];
int tree[maxn<<4];
int vis[maxn<<4];
int ans,cnt;
void push_down (int rt)
{
    if(tree[rt]!=-1)
    {
        tree[rt<<1]=tree[rt<<1|1]=tree[rt];
        tree[rt]=-1;
    }
}
void update (int l,int r,int x,int nl,int nr,int rt)
{
    if(nl>=l&&nr<=r)
    {
        tree[rt]=x;
        return;
    }
    int m=(nl+nr)>>1;
    push_down(rt);
    if(m>=l) update (l,r,x,nl,m,rt<<1);
    if(r>m) update (l,r,x,m+1,nr,rt<<1|1);
}
void query(int l,int r,int rt)
{
    if(tree[rt]!=-1)
    {
        if(vis[tree[rt]]==0)
        {
            ans++;
            vis[tree[rt]]=1;
        }
        return;
    }
    if(l==r) return ;
    int m=(l+r)>>1;
    push_down(rt);
    query(l,m,rt<<1);
    query(m+1,r,rt<<1|1);
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        cnt=ans=0;
        memset (tree,-1,sizeof(tree));
        memset (vis,0,sizeof(vis));
        for (int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].l,&a[i].r);
            b[cnt++]=a[i].l,b[cnt++]=a[i].r;
        }
        sort(b,b+cnt);
        int cnt1=unique(b,b+cnt)-b;
        int tcnt=cnt1;
        for (int i=1;i<tcnt;i++)
        {
            if(b[i]-b[i-1]>1) b[cnt1++]=b[i-1]+1;
        }
        sort(b,b+cnt1);
        for (int i=0;i<n;i++)
        {
            int l=lower_bound(b,b+cnt1,a[i].l)-b;
            int r=lower_bound(b,b+cnt1,a[i].r)-b;
            update (l,r,i,0,cnt1-1,1);
        }
        query(0,cnt1-1,1);
        printf("%d\n",ans);
    }
    return 0;
}