zoukankan      html  css  js  c++  java
  • POJ2528 Mayor's poster

    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 l i 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 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+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
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<vector>
     6 #include<algorithm>
     7 using namespace std;
     8 const int maxn=20010;
     9 int T,N,ans,le[maxn],ri[maxn],vis[maxn];
    10 vector<int> vec;
    11 struct Node{
    12     int l,r,num;
    13 } tree[maxn<<2];
    14 int getid(int x) { return lower_bound(vec.begin(),vec.end(),x)-vec.begin()+1; }
    15 void build(int pos,int l,int r)
    16 {
    17     tree[pos].l=l,tree[pos].r=r;
    18     if(l==r) 
    19     {
    20         tree[pos].num=-1;
    21         return ;
    22     }
    23     int mid=(l+r)>>1;
    24     build(pos<<1,l,mid);
    25     build(pos<<1|1,mid+1,r);
    26 }
    27 
    28 void pushdown(int pos)
    29 {
    30     tree[pos<<1].num=tree[pos<<1|1].num=tree[pos].num;
    31     tree[pos].num=-1;
    32 }
    33 
    34 void update(int l,int r,int pos,int val)
    35 {
    36     if(tree[pos].l>=l&&tree[pos].r<=r) 
    37     {
    38         tree[pos].num=val;
    39         return ;
    40     }
    41     if(tree[pos].num!=-1) pushdown(pos);
    42     int mid=(tree[pos].l+tree[pos].r)>>1;
    43     if(r<=mid) update(l,r,pos<<1,val);
    44     else if(l>=mid+1) update(l,r,pos<<1|1,val);
    45     else update(l,mid,pos<<1,val),update(mid+1,r,pos<<1|1,val);
    46 }
    47 
    48 void query(int l,int r,int pos)
    49 {
    50     if(tree[pos].num!=-1)
    51     {
    52         if(!vis[tree[pos].num]) ans++,vis[tree[pos].num]=1;
    53         return ;
    54     }
    55     if(l==r) return ;
    56     int mid=(tree[pos].l+tree[pos].r)>>1;
    57     query(l,mid,pos<<1); query(mid+1,r,pos<<1|1);    
    58 }
    59 
    60 int main()
    61 {
    62     scanf("%d",&T);
    63     while(T--)
    64     {
    65         scanf("%d",&N);
    66         memset(vis,0,sizeof vis);
    67         vec.clear(); ans=0;
    68         for(int i=1;i<=N;i++)
    69         {
    70             scanf("%d%d",&le[i],&ri[i]);
    71             vec.push_back(le[i]);
    72             vec.push_back(ri[i]);
    73         } 
    74         sort(vec.begin(),vec.end());
    75         vec.erase(unique(vec.begin(),vec.end()),vec.end());
    76         int len=vec.size();
    77         build(1,1,len);
    78         for(int i=1;i<=N;i++)
    79         {
    80             int l=getid(le[i]),r=getid(ri[i]);
    81             update(l,r,1,i);
    82         }
    83         query(1,len,1);
    84         printf("%d
    ",ans);
    85     }
    86     
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    linux命令大全
    linux几个常用命令
    linux基础入门命令自我总结——乱
    linux标准目录的结构——各个目录都是干啥的
    python基础知识9——模块2——常见内置模块
    python基础知识8——模块1——自定义模块和第三方开源模块
    手写数据结构-链表
    手写数据结构-基于动态数组的循环队列
    手写数据结构-基于动态数组的队列
    手写数据结构-基于动态数组的栈
  • 原文地址:https://www.cnblogs.com/csushl/p/9386565.html
Copyright © 2011-2022 走看看