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

    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 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<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 int flag;
     6 struct p
     7 {
     8     int x,d;
     9 };p s[24000];
    10 bool com1(p A,p B)
    11 {
    12     return A.x<B.x;
    13 }
    14 bool com2(p A,p B)
    15 {
    16     if (A.d==B.d) return A.x<B.x;
    17     return A.d>B.d;
    18 }
    19 struct pp
    20 {
    21     int l,r,v;
    22 };pp tree[1000000];
    23 void build(int l,int r,int p)
    24 {
    25     tree[p].l=l;
    26     tree[p].r=r;
    27     tree[p].v=0;
    28     if (l==r) return ;
    29     int m=(l+r)/2;
    30     build(l,m,2*p);
    31     build(m+1,r,2*p+1);
    32     return ;
    33 }
    34 void find(int l,int r,int p)
    35 {
    36     if (tree[p].v) return ;
    37     if (tree[p].l==l&&tree[p].r==r)
    38     {
    39         flag=1;
    40         tree[p].v=1;
    41         return ;
    42     }
    43     int m=(tree[p].l+tree[p].r)/2;
    44     if (l>m) find(l,r,2*p+1);
    45     else if (m>=r) find (l,r,2*p);
    46     else
    47     {
    48         find(l,m,2*p);
    49         find(m+1,r,2*p+1);
    50     }
    51     tree[p].v=tree[2*p].v&&tree[2*p+1].v;
    52     return ;
    53 }
    54 int main()
    55 {
    56     int c,n,i,j,ans;
    57     scanf("%d",&c);
    58     while (c--)
    59     {
    60         scanf("%d",&n);
    61         for (i=1;i<=2*n;i+=2)
    62         {
    63             scanf("%d%d",&s[i].x,&s[i+1].x);
    64             s[i].d=s[i+1].d=i;
    65         }
    66         sort (s+1,s+2*n+1,com1);
    67         int p1=0,p2=0;
    68         for (i=1;i<=2*n;i++)
    69         {
    70             if (s[i].x==p1)
    71                s[i].x=p2;
    72             else
    73             {
    74                 p1=s[i].x;
    75                 s[i].x=++p2;
    76             }
    77           //  printf("**%d**
    ",s[i].x);
    78         }
    79         build(1,2*n,1);
    80         ans=0;
    81         sort(s+1,s+2*n+1,com2);
    82         for (i=1;i<=2*n;i+=2)
    83         {
    84             flag=0;
    85             find(s[i].x,s[i+1].x,1);
    86           //  printf("**%d***%d**
    ",s[i].x,s[i+1].x);
    87             if (flag) ans++;
    88         }
    89         printf("%d
    ",ans);
    90     }
    91     return 0;
    92 }
  • 相关阅读:
    SAP Cloud for Customer Sales Lead明细页面视图的UI模型
    如何基于SAP CDS view创建OData服务
    使用SAP HANA Web-based Development工具进行SQLScript练习
    SAP ABAP守护进程(ABAP Daemon)的实现方式
    使用SAP云平台Mobile Service开发移动应用
    SAP CRM WebClient UI Excel Export的运行时执行明细
    MySQL里面的子查询实例
    hash_hmac 签名
    redis单例模式写法
    jQuery 短信验证码倒计时
  • 原文地址:https://www.cnblogs.com/pblr/p/4737715.html
Copyright © 2011-2022 走看看