• POJ 2528 Mayor's posters 线段树+离散化


    题目链接: http://poj.org/problem?id=2528

    解题思路: 线段树+离散化。10000个线段,20000个点,所以线段树最大是80000。离散化的方法:首先保存每个端点,然后排序+去重(unique函数)之后从头到尾映射一下就可以了。

    更新时可以从后往前进行,这样,由于前面的只会被后面的盖住,因此更新到已经有海报的区间就不必继续更新了,当然pushdown操作也是必不可少。

    代码:

     1 const int maxn = 1e4 + 5;
     2 int hash[10000005];
     3 int tree[maxn * 8], n;
     4 int st[maxn], ed[maxn], pos[maxn * 2];
     5 int vis[maxn];
     6 
     7 void build(int l, int r, int k){
     8     tree[k] = -1;
     9     if(l == r) return;
    10     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    11     build(l, mid, lc);
    12     build(mid + 1, r, rc);
    13 }
    14 void update(int ul, int ur, int x, int l, int r, int k){
    15     if(ul <= l && ur >= r){
    16         if(tree[k] == -1)
    17             tree[k] = x;
    18         return;
    19     }
    20     if(ul > r || ur < l) return;
    21     if(l == r){
    22         if(tree[k] == -1) tree[k] = x;
    23         return;
    24     }
    25     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    26     if(tree[k] != -1){
    27         if(tree[lc] == -1) tree[lc] = tree[k];
    28         if(tree[rc] == -1) tree[rc] = tree[k];
    29         tree[k] = -1;
    30     }
    31     
    32     update(ul, ur, x, l, mid, lc);
    33     update(ul, ur, x, mid + 1, r, rc);
    34 }
    35 int query(int qu, int l, int r, int k){
    36     if(qu == l && l == r){
    37         return tree[k];
    38     }
    39     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    40     if(tree[k] != -1){
    41         if(tree[lc] == -1) tree[lc] = tree[k];
    42         if(tree[rc] == -1) tree[rc] = tree[k];
    43         tree[k] = -1;
    44     }
    45     
    46     if(qu <= mid) return query(qu, l, mid, lc);
    47     else return query(qu, mid + 1, r, rc);
    48 }
    49 int main(){
    50     int T;
    51     scanf("%d", &T);
    52     while(T--){
    53         memset(hash, -1, sizeof(hash));
    54         memset(vis, 0, sizeof(vis));
    55         scanf("%d", &n);
    56         int cnt = 0;
    57         for(int i = 0; i < n; i++){
    58             scanf("%d %d", &st[i], &ed[i]);
    59             pos[cnt++] = st[i];
    60             pos[cnt++] = ed[i];
    61         }
    62         sort(pos, pos + cnt);
    63         cnt = unique(pos, pos + cnt) - pos;
    64         for(int i = 1; i <= cnt; i++)
    65             hash[pos[i - 1]] = i;
    66         build(1, cnt, 1);
    67         for(int i = n - 1; i >= 0; i--){
    68             update(hash[st[i]], hash[ed[i]], i + 1, 1, cnt, 1);
    69         }
    70         for(int i = 1; i <= cnt; i++){
    71             int tmp = query(i, 1, cnt, 1);
    72             if(tmp != -1) vis[tmp] = 1;
    73         }
    74         int ans = 0;
    75         for(int i = 1; i <= n; i++) ans += vis[i];
    76         printf("%d
    ", ans);
    77     }
    78 }

    题目:

    Mayor's posters
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 66581   Accepted: 19226

    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
    

    Source

  • 相关阅读:
    poj 3415 后缀数组+单调栈
    hdu 3450 后缀数组
    hdu 2774 后缀数组
    后缀数组模板(倍增法)
    hdu 4405 概率dp
    zoj 3329 概率dp
    [日常摸鱼]bzoj2875[NOI2012]随机数生成器-矩阵快速幂
    [日常摸鱼]bzoj1038[ZJOI2008]瞭望塔-半平面交
    [日常摸鱼]bzoj1007[HNOI2008]水平可见直线-半平面交(对偶转凸包)
    [日常摸鱼]bzoj3083遥远的国度-树链剖分
  • 原文地址:https://www.cnblogs.com/bolderic/p/7294750.html
Copyright © 2020-2023  润新知