zoukankan      html  css  js  c++  java
  • POJ 2528 Mayor's posters (线段树+离散化)

                                      Mayor's posters
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions:75394   Accepted: 21747

    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

    思路:
    好久没有写博客了,做这个题,我写了一整天,就因为一句话没有写好,我实在是想骂人呀!
    首先离散化,避免超时,避免超内存,这个我相信你在别的博客上可以看见,我就不多说,线段树的节点里面,存的是下面全部相同的数字,也就是说,如果这个节点,代表的是l到r的范围内的数,
    如果l到r里面,数字完全相同,那么,这个节点就存下这个数,否则,那我们就存下-1.如果查询的时候遇到-1,那我们就继续向下查询就行了,直到查到不为-1或到达叶子节点为止。
    我的错误是update的最后一行,忽略了下面两个节点都是-1的情况
    AC代码
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    int s[400086];
    bool book[400086];
    struct node
    {
        int l,r,num;
        bool lazy;
    }tree[1600099];
    int x[100086],y[100086];
    
    
    void build(int t,int l,int r)
    {
        tree[t].l=l;tree[t].r=r;
        tree[t].num=-1;tree[t].lazy=false;
        if(l==r){return;}
        int mid=(l+r)/2;
        build(t<<1,l,mid);
        build((t<<1)|1,mid+1,r);
    }
    
    void push_down(int t)
    {
        tree[t].lazy=false;
        tree[t<<1].num=tree[t].num;
        if(tree[t<<1].l!=tree[t<<1].r){tree[t<<1].lazy=true;}
    
        tree[(t<<1)|1].num=tree[t].num;
        if(tree[(t<<1)|1].l!=tree[(t<<1)|1].r){tree[(t<<1)|1].lazy=true;}
    }
    
    void update(int t,int l,int r,int e)
    {
        if(tree[t].lazy){push_down(t);}
        if(tree[t].l==l&&r==tree[t].r){
            tree[t].num=e;
            if(l!=r){tree[t].lazy=true;}
            return;
        }
    
        int mid=(tree[t].l+tree[t].r)>>1;
        if(r<=mid){update(t<<1,l,r,e);}
        else if(l>mid){update((t<<1)|1,l,r,e);}
        else{
            update((t<<1),l,mid,e);
            update((t<<1)|1,mid+1,r,e);
        }
        if(tree[t<<1].num!=tree[(t<<1)|1].num||tree[t<<1].num==-1||tree[(t<<1)|1].num==-1){tree[t].num=-1;}
    }
    
    int query(int t)
    {
        int ans=0;
        if(tree[t].num==-1){
            if(tree[t].lazy){push_down(t);}
            if(tree[t].l==tree[t].r){return 0;}
            ans+=query(t<<1);
            ans+=query((t<<1)|1);
        }
        else{
            if(!book[tree[t].num]){book[tree[t].num]=true;return 1;}
        }
        return ans;
    }
    
    int main()
    {
        int T,n;
        scanf("%d",&T);
        while(T--){
            scanf("%d",&n);
            memset(book,0,sizeof(book));
            memset(s,0,sizeof(s));
            int nn=0;
            for(int i=1;i<=n;i++){
                scanf("%d%d",&x[i],&y[i]);
                s[++nn]=x[i];s[++nn]=y[i];
            }
            sort(s+1,s+nn+1);
            int siz=unique(s+1,s+nn+1)-s-1;
            int k=siz;
            for(int i=1;i<=k-1;i++){
                if(s[i+1]-s[i]>1){s[++siz]=s[i]+1;}
            }
            sort(s+1,s+siz+1);
            build(1,1,siz);
            int l,r;
            for(int i=1;i<=n;i++){
                l=lower_bound(s+1,s+siz+1,x[i])-s;
                r=lower_bound(s+1,s+siz+1,y[i])-s;
                update(1,l,r,i);
            }
            printf("%d
    ",query(1));
    
        }
    }
    

      

     
  • 相关阅读:
    sql两个字段相加减,第三个字段没有值的原因.
    CF 447B(DZY Loves Strings-贪心)
    Appium AndroidKeyCode
    初探Java多线程
    模拟实现Spring IoC功能
    Cordova 5 架构学习 Weinre远程调试技术
    快学Scala习题解答—第三章 数组相关操作
    org.hibernate.AssertionFailure: null id in com.you.model.User entry (don&#39;t flush the Session after a
    Swift3.0语言教程替换子字符串
    DHCP欺骗(DHCP Sproofing)
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/9366590.html
Copyright © 2011-2022 走看看