POJ2528 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 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.

  • 题目: 有一块很长的板子,我们往上贴广告,问最后能够看到的广告的数量。
  • 思路: 这题目用线段树做,坐标问题我们可以先离散化,我们利用线段树进行成段更新(没插入一段给该段一个标记数字),最后统计线段树中还存在多少个不同的数字即可。
  • 离散化的时候要注意一下,1-7 1-4 5-7离散化和1-7 1-4 6-7离散化的结果的一样的,可是第一个例子是完全覆盖了1-7的,
    而后面并没有覆盖1-7,那么我们需要对后面的离散化进行处理一下,我们只需要在4-6之间多穿插一个5即可。那么我们所做的处理就是排序后若两个数之差大于1,那么我们就在他们之间穿插一个数即可避免上面的问题了。
    如[1,2,6,7]我们在2,6之间随意加入一个大于2小于6的数即可。变成[1,2,3,6,7]这样子离散化即可。
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.*;
public class __2528 {
    public static Scanner cin;
    public static int[] x;
    public static Seg[] seg; //记录左右区间的大小
    public static int[] col;
    public static boolean[] flag;//记录某个数是否出现过
    public static int ans;
    public static class Seg{
        int l,r;
        public Seg(int l, int r) {
            this.l = l;
            this.r = r;
        }
    }
    public static void pushDown(int rt){
        if(col[rt]!=-1){ //懒惰标记
            col[rt<<1]=col[rt<<1|1]=col[rt];
            col[rt]=-1;
        }
    }
    public static void query(int l,int r,int rt){
        if(l==r){
            if(col[rt]==-1){ //如果为-1直接跳出
                return;
            }
            if(!flag[col[rt]]){  //不为-1判断是否存在,存在跳过,不存在加1
                ++ans;
                flag[col[rt]]=true;
            }
            return;
        }
        pushDown(rt);
        int mid=(l+r)>>1;
        query(l,mid,rt<<1);
        query(mid+1,r,rt<<1|1);
    }
    public static void update(int L,int R,int c,int l,int r,int rt){ 
        if(L<=l&&r<=R){
            col[rt]=c;
            return;
        }
        pushDown(rt);
        int mid=(l+r)/2;
        if(L<=mid){
            update(L,R,c,l,mid,rt<<1);
        }
        if(R>mid){
            update(L,R,c,mid+1,r,rt<<1|1);
        }
    }
    public static int lower_bound(int[] array,int l,int r,int key){
        while (l<r){
            int mid=(r+l)>>1;
            if(array[mid]>=key){
                r=mid;
            }else{
                l=mid+1;
            }
        }
        return l;
    }
    public static void main(String[] args) {
        cin=new Scanner(new InputStreamReader(System.in));      
        int Case=cin.nextInt();
        while (Case-->0){
            ans=0;
            int n=cin.nextInt();
            x=new int[n<<3];
            int tot=0;
            seg=new Seg[n];
            for(int i=0;i<n;++i){
                seg[i]=new Seg(cin.nextInt(),cin.nextInt());
                x[tot++]=seg[i].l;
                x[tot++]=seg[i].r;
            }
            Arrays.sort(x,0,tot);
            SortedSet<Integer> set=new TreeSet<Integer>(); //用于去重
            for(int i=0;i<tot;++i){ 
                set.add(x[i]);
            }
            int k=0;
            for(int t:set){
                x[k++]=t;
            }      
            int m=k;
            for(int i=1;i<k;++i){
                if(x[i]>x[i-1]+1){ //若两数之差大于1
                    x[m++]=x[i-1]+1;
                }
            }
            Arrays.sort(x,0,m);
            k=m;
            col=new int[k<<2];
            Arrays.fill(col,-1); //建树 初始化为-1
            flag=new boolean[k];
            for(int i=0;i<n;++i){
                int l=lower_bound(x,0,k-1,seg[i].l);
                int r=lower_bound(x,0,k-1,seg[i].r);
                update(l,r,i,0,k-1,1);
            }
            query(0,k-1,1);
            System.out.println(ans);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值