zoukankan      html  css  js  c++  java
  • poj 2528 Mayor's posters

    Mayor's posters
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions:31053   Accepted: 9019

    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

    线段树,更新区间。
    一个细节bug查了好久,就是二分查找的地方,没写等于号。
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 #define lson l, m, rt<<1
     6 #define rson m+1, r, rt<<1|1
     7 const int maxn = 111111;
     8 int li[maxn], ri[maxn], x[maxn*3], col[maxn<<4], cnt;
     9 bool hash[maxn];
    10 void PushDown(int rt){
    11   if (col[rt] != -1){
    12     col[rt<<1] = col[rt<<1|1] = col[rt];
    13     col[rt] = -1;
    14   }
    15 }
    16 void update(int L,int R, int c, int l, int r, int rt){
    17   if (L <= l && R >= r){col[rt] = c; return;}
    18   PushDown(rt);
    19   int m = (l + r) >> 1; 
    20   if (L <= m) update(L, R, c, lson); if (R > m) update(L, R, c, rson);
    21 }
    22 void query(int l, int r, int rt){
    23   if (col[rt] != -1){
    24     if (!hash[col[rt]]) cnt++;
    25     hash[col[rt]] = true; return;
    26   }
    27   if (l == r) return; 
    28   int m = (l + r) >> 1;
    29   query(lson); query(rson);
    30 }
    31 int bin(int key, int n, int a[]){
    32   int l = 0, r = n - 1;
    33   while (l <= r){
    34     int m = (l + r) >> 1;
    35     if (a[m] == key) return m;
    36     if (a[m] < key) l = m + 1; else r = m - 1;
    37   }
    38   return -1;
    39 }
    40 int main(void){
    41 #ifndef ONLINE_JUDGE
    42   freopen("poj2528.in", "r", stdin);
    43 #endif
    44   int t; scanf("%d", &t);
    45   while (t--){
    46     int n; scanf("%d",&n); int k = 0;
    47     for (int i = 0; i < n; ++i){
    48       scanf("%d%d", li+i, ri+i); x[k++] = li[i]; x[k++] = ri[i];
    49     }
    50     sort(x, x + k); 
    51     int m = 1;
    52     for (int i = 1; i < k; ++i){
    53       if (x[i] != x[i-1]) x[m++] = x[i];
    54     }
    55     for (int i = m - 1; i > 0; --i){
    56       if (x[i] != x[i-1] + 1) x[m++] = x[i-1] + 1;
    57     }
    58     sort(x, x + m);
    59     memset(col, -1, sizeof(col));
    60     for (int i = 0; i < n; ++i){
    61       int l = bin(li[i], m, x);
    62       int r = bin(ri[i], m, x);
    63       update(l, r, i, 0, m, 1);
    64     }
    65     cnt = 0; memset(hash, false, sizeof(hash));
    66     query(0, m, 1); printf("%d\n", cnt);
    67   }
    68   return 0;
    69 }

    膜拜一下NotOnlySuccess神犇……

     
  • 相关阅读:
    农历查询
    C#颜色转换函数
    在IIS部署Silverlight网站
    silverlight双击事件处理
    关于List.Sort想到的
    sql获取总列数
    NHibernate的no persister for
    如何快速构建React组件库
    如何用canvas拍出 jDer's工作照
    Picker 组件的设计与实现
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/2944478.html
Copyright © 2011-2022 走看看