zoukankan      html  css  js  c++  java
  • poj2528

                                                                                                 Mayor's posters
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 34731   Accepted: 10072

    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
    
    #include<iostream>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cstdio>
    using namespace std;
    
    const int maxn = 20010;
    int map[maxn][2],sum,record[maxn];
    struct Node
    {
        int left,right,c;
        int mid()
        {
            return left + (right - left) / 2;
        }
    }tree[maxn*5];
    
    struct node
    {
        int point,num;
    }save[maxn*2];
    
    int cmp(node &a,node &b)
    {
        return a.point < b.point;
    }
     
    void build(int l,int r,int n)
    {
        tree[n].left = l;
        tree[n].right = r;
        tree[n].c = 0;
        if(l==r)
            return ;
        else 
        {
            int mid = (l+r)/2;
            build(l,mid,2*n);
            build(mid+1,r,2*n+1);
        }
    
    }
    
    void add(int l,int r,int n,int  color)
    {
        if(tree[n].left == l  && tree[n].right == r)
        {
            tree[n].c = color;
            return ;
        }
        if(tree[n].c > 0)
        {
            tree[2*n].c = tree[n].c;
            tree[2*n+1].c = tree[n].c;
            tree[n].c = 0;
        }
    
        if(l>=tree[2*n+1].left)
            add(l,r,2*n+1,color);
        else
        {
            if(r <= tree[2*n].right)
                add(l,r,2*n,color);
            else 
            {
                add(l,tree[2*n].right , 2*n,color);
                add(tree[2*n+1].left,r,2*n+1,color);
            }
        }
    }
    
    void update(int n)
    {
        if(tree[n].c!=0)
        {
            if(!record[tree[n].c])
            {
                sum++;
                record[tree[n].c] = 1;
            }
            return ;
        }
        update(2*n);
        update(2*n+1);
        return ;
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n,i;
            scanf("%d",&n);
            for(i=0;i<n;i++)
            {
                scanf("%d %d",&map[i][0],&map[i][1]);
                save[2*i].point = map[i][0];
                save[2*i+1].point = map[i][1];
                save[2*i].num = -(i+1);
                save[2*i+1].num = i+1;
            }
            sort(save,save + 2*n,cmp);
            int temp = save[0].point;
            int count =1;
            for(i=0;i<2*n;i++)
            {
                if(temp != save[i].point)
                {
                    count++;
                    temp = save[i].point;
                }
                if(save[i].num < 0)
                    map[-save[i].num - 1][0] = count;
                else 
                    map[save[i].num - 1][1] = count;
            }
            build(1,count,1);
            for(i=0;i<n;i++)
            {
                add(map[i][0],map[i][1],1,i+1);
            }
            memset(record,0,sizeof(record));
            sum = 0;
            update(1);
            printf("%d
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    Ubuntu16.04安装openldap和phpldapadmin
    Java 8 中的抽象类和接口到底有啥区别?
    Redis 开发陷阱及避坑指南!
    Java 中的 6 颗语法糖
    Java 8 有多牛逼?打破一切你对接口的认知!
    Git操作常用的命令都在这里了。
    Github 太狠了,居然把 "master" 干掉了!
    微服务业务日志收集方案,写得非常好!
    Maven基本介绍与安装
    IntelliJ IDEA 调试 Java 8 Stream,实在太香了!
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3255461.html
Copyright © 2011-2022 走看看