zoukankan      html  css  js  c++  java
  • POJ2528 Mayor's poster 线段树+插入节点建树解法

    此题和普通的线段树题相比,最大的挑战在于区间太大,如果一开始就建立一个完整的线段树,空间太大。所以,先只建立根节点,每来一组数据,用普通的线段树查找方法沿根节点查找,如果此组数据的子树不存在,则建立子树。最后递归删除线段树。

    结果为:

    15308kB 80ms

    如果最后不删除树,时间所减少,空间增大。为:

    20324kB60ms

    #include <stdio.h>
    #include <string.h>
    struct InTree{
        int l,u;
        InTree *left,*right;
        int color;//-2表示该区间是混合色或者还未涂色
        InTree(int low,int upp){
            l= low; u = upp;
            left = right = 0;
            color = -2;
        }
    };
    void Insert(InTree *root,int low,int upp,int c){
        if(low <= root->l && root->u <=upp){
            root->color = c;
            return;
        }
        else{
            if(root->left == 0){
                root->left = new InTree(root->l,(root->l+root->u)/2);
            }
            if(root->right == 0){
                root->right = new InTree((root->l+root->u)/2+1,root->u);
            }
    
            if(root->color >= 0){//当本区间是单一色时,需要将颜色下推
                root->right->color = root->left->color = root->color;
                root->color = -2;
            }
    
            if(low <= (root->l+root->u)/2){
                Insert(root->left,low,upp,c);
            }
            if(upp >= (root->l+root->u)/2 + 1){
                Insert(root->right,low,upp,c);
            }
        }
    }
    
    void Query(InTree *root,bool *map){
    //查询root子树中全涂成一种颜色的区间,并将区间的颜色对应的map位置置为true
        if(root == 0)
            return;
        else if(root->color >=0)
            map[root->color] = true;
        else{
            Query(root->left,map);
            Query(root->right,map);
        }
    }
    
    void Delete(InTree *root){
    //递归删除节点
        if(root == 0)
            return;
        else{
            Delete(root->left);
            Delete(root->right);
            delete root;
        }
    }
    int main(){
        int c;
        int n;
        bool *map = new bool[10000];
        scanf("%d",&c);
        while(c>0){
            --c;
            scanf("%d",&n);
            InTree* root = new InTree(1,10000000);
            int low,upp;
            for(int i=0;i<n;i++){
                scanf("%d %d",&low,&upp);
                Insert(root,low,upp,i);
            }
            memset(map,0,10000*sizeof(bool));
            Query(root,map);
            int cnt=0;
            for(int i=0;i<n;i++){
                if(map[i])cnt++;
            }
            printf("%d\n",cnt);
            Delete(root);
        }
        return 0;
    }

    /*
    2528:Mayor's posters
    查看 提交 统计 提示 提问
    时间限制: 1000ms 内存限制: 65536kB
    描述
    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.
    输入
    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.
    输出
    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.


    样例输入
    1
    5
    1 4
    2 6
    8 10
    3 4
    7 10
    样例输出
    4
    */

  • 相关阅读:
    hbase编码
    kafka常用命令
    国产十大数据库排行榜
    After Titans
    kingbase7获取唯一索引和子分区键的view
    准提道人收孔宣
    MySQL使用全文索引
    instead of触发器实现复杂视图dml和应用逻辑
    中国oracle ace名单
    第六十象 癸亥
  • 原文地址:https://www.cnblogs.com/zhuyuanhao/p/3262860.html
Copyright © 2011-2022 走看看