D - 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

1.先把每条线段的单独存下来,同时把每个端点的值存在另一个数组里,在此把图放上

        for(int i = 1; i <= n; ++ i)
        {
            cin >> li[i] >> ri[i];
            x[cnt++] = li[i];
            x[cnt++] = ri[i];
        }
2.来一发排序,sort即可,然后处理一下相邻数据的差超过1的情况。先解释一下为什么,线段树是以点的形式存储了线段,所以难免会有局限,比如区间[1, 4]和[5, 6], 在线段数里面其实是相连接的,也就是说中间没有空隙(注意!!!),按照普通的离散化,两个不连续的数离散化之后会是连续的数,比如[1, 4], [2, 8], [7, 10]这三条线段,离散后:[1, 3], [2, 5], [4, 6], 然后算算离散前后的结果,一个是3, 一个是2,所以说就要把相差超过1的数据的特点体现出来,使得其之间有空隙。方法就是在这两个数中间再加一个数。就是x[i - 1]++这个操作了,最后再来发sort就ok了

        sort(x + 1, x + cnt);
        int m = cnt;
        for(int i = 2; i < cnt; ++ i)
        {
            if(x[i] - x[i - 1] > 1)
            {
                x[m++] = x[i - 1]++;
            }
        }
        sort(x + 1, x + m);
3.然后把数组中重复的元素去掉,因为接下来我们要用二分查找来确定每条线段的标号,去重才能保证标号唯一。

        int top = 2;
        y[1] = x[1];
        for(int i = 2; i < m; ++ i)
        {
            if(x[i] != x[i - 1])
            {
                y[top++] = x[i];
            }
        }
4.下来就是通过二分查找把每条线段离散后的范围和标号对应起来,进行区间更新。

 for(int i = 1; i <= n; ++ i)
 {
     int L = Bin(1, top - 1, li[i]);
     int R = Bin(1, top - 1, ri[i]);
     Update(L, R, i, 1, top - 1, 1);
 }


另外附上二分查找的函数

 inline int Bin(int l, int r, int x)
{
    int m;
    while(l < r)
    {
        m = (l + r) / 2;
        if(y[m] == x)
            return m;
        else if(y[m] > x)
            r = m - 1;
        else
            l = m + 1;
    }
    return l;
}
最后再查询一波就好了

下面上完整代码:

再解释一下我的线段树,我用-1代表此区间没有颜色或者不是纯色的情况(反正这两种情况都差不多,不能直接统计)

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=100001;
int tree[8*N],li[N*4],ri[N*4];
int x[N*4],y[N*4],sum;
int vis[N*4];
int bin(int l,int r,int x)
{

    int m;
    while(l<r)
    {
        m=(l+r)/2;
        if(y[m]==x)
            return m;
        else if(y[m]>x)
            r=m-1;
        else
            l=m+1;
    }
    return l;
}
void updata(int a,int b,int op,int l,int r,int rt)
{
    if(l>=a&&b>=r)
    {
        tree[rt]=op;
        return;
    }
    if(tree[rt]!=-1)
    {
        int ans=tree[rt];
        tree[rt*2]=ans;
        tree[rt*2+1]=ans;
        tree[rt]=-1;
    }
    int m=(l+r)>>1;
    if(m>=a)
        updata(a,b,op,l,m,rt<<1);
    if(m<b)
        updata(a,b,op,m+1,r,rt<<1|1);
}
void query(int l,int r,int rt)
{
    if(tree[rt]!=-1)
    {
        if(!vis[tree[rt]])
        {
            vis[tree[rt]]=1;
            sum++;
        }
        return;
    }
    if(l==r)
        return;
    int m=(l+r)/2;
    query(l,m,rt<<1);
    query(m+1,r,rt<<1|1);

}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {

        scanf("%d",&n);
        memset(vis,0,sizeof(vis));
        memset(tree,-1,sizeof(tree));
        int ans=1;
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&li[i],&ri[i]);
            x[ans++]=li[i];
            x[ans++]=ri[i];

        }
        sort(x+1,x+ans);
        int m=ans;
        for(int i=2; i<ans; i++)
        {
            if(x[i]-x[i-1]>1)
                x[m++]=x[i-1]++;

        }
        sort(x+1,x+m);
        int top=2;
        y[1]=x[1];
        for(int i=2; i<m; i++)
        {
            if(x[i]!=x[i-1])
                y[top++]=x[i];

        }
        for(int i=1; i<=n; i++)
        {
            int l=bin(1,top-1,li[i]);
            int r=bin(1,top-1,ri[i]);
            updata(l,r,i,1,top-1,1);

        }
        sum=0;
        query(1,top-1,1);
        printf("%d\n",sum);

    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/chen_zan_yu_/article/details/83574403