• POJ2528 线段树离散化


    Mayor's posters
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 62771   Accepted: 18120

    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. 

    Sample Input

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

    Sample Output

    4
    

    Source

    题意:
    10000000 长的墙,有n张海报依次贴在墙上,每张海报贴在[a,b]范围,问最后没有完全被覆盖的海报有多少。
    代码:
    //离散化一:比如对于如下区间集合,[1,1000],[500,2000],[1500,2500].那么
    //把所有区间端点1,500,1000,1500,2000,2500离散化后就是1,2,3,4,5,6.离散化
    //后所得区间为:[1,3],[2,5],[4,6].可以知道离散化前可见区间有3个,但是离散
    //化后只有区间[1,3]和区间[4,6]可见.所以离散化一的方式是有问题的
    //离散化二:对于区间端点的离散化,如果离散化之前相邻的两个数不是类似于a与a+1的差距1关系,
    //那么就自动在后面的这个数的离散化结果上加1.比如:[1,10],[1,5],[7,10] 离散化后
    //的区间为[1,7][1,3],[5,7]
    //离散化方式二主要就是让本来不相邻的数继续保持不相邻即可.
    
    //离散化之后用二分查找对应的离散化后的点,本题val数组要开大。
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    const int maxn=10000;
    bool vis[maxn*4+10];
    int mp[maxn*4+10],val[maxn*14+10],t,n,ans;
    struct node
    {
        int l,r;
    }nodes[maxn*4+10];
    int Bsearch(int a,int b,int *c)
    {
        int l=0,r=b-1,mid;
        while(l<=r){
            mid=(l+r)>>1;
            if(c[mid]==a) return mid;
            else if(c[mid]<a) l=mid+1;
            else r=mid-1;
        }
        return -1;
    }
    void Pushdown(int rt)
    {
        if(val[rt]>=0){
            val[rt<<1]=val[rt<<1|1]=val[rt];
        }
        val[rt]=-1;
    }
    void Update(int ql,int qr,int c,int l,int r,int rt)
    {
        if(ql<=l&&qr>=r){
            val[rt]=c;
            return ;
        }
        Pushdown(rt);
        int m=(l+r)>>1;
        if(ql<=m) Update(ql,qr,c,l,m,rt<<1);
        if(qr>m) Update(ql,qr,c,m+1,r,rt<<1|1);
    }
    void Query(int l,int r,int rt)
    {
        if(val[rt]>=0){
            if(!vis[val[rt]]) ans++;
            vis[val[rt]]=1;
            return;
        }
        if(l==r) return;
        Pushdown(rt);
        int m=(l+r)>>1;
        Query(l,m,rt<<1);
        Query(m+1,r,rt<<1|1);
    }
    int main()
    {
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            int m=0;
            for(int i=0;i<n;i++){
                scanf("%d%d",&nodes[i].l,&nodes[i].r);
                mp[m++]=nodes[i].l;mp[m++]=nodes[i].r;
            }
            sort(mp,mp+m);
            m=unique(mp,mp+m)-mp;//去重,加入相关的点
            for(int i=m-1;i>=1;i--){
                if(mp[i]-mp[i-1]>1) mp[m++]=mp[i]-1;
            }
            sort(mp,mp+m);
            memset(vis,0,sizeof(vis));
            memset(val,-1,sizeof(val));
            for(int i=0;i<n;i++){
                nodes[i].l=Bsearch(nodes[i].l,m,mp);
                nodes[i].r=Bsearch(nodes[i].r,m,mp);
                Update(nodes[i].l,nodes[i].r,i,0,m-1,1);
            }
            ans=0;
            Query(0,m-1,1);
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Project 01 PlantAndZomb
    iOS平台内存使用原则
    IOS之UIKit_Day22--23
    IOS之UIKit_Day21
    IOS之UIKit_Day20
    浅谈javascript数据类型,对象,类
    从输入一个url到加载页面发生了什么?
    d3.js(2)-svg
    d3.js(1)
    console.time和performance.now()
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6605331.html
Copyright © 2020-2023  润新知