• POJ-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 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


     

    思路:

     

    • 线段树Lazy标记加离散化,但不能按正常方式离散化,因为可能会造成两点之间存在区间被标记但离散化后区间被压缩为0的情况,因此要在间距大于1的两点中加一个点。
    • 离散化理解:离散化就是在很大的区间中,我们只关心其中各点的大小关系,而点的数量实际远远不能填充整个区间,因此我们可以将每个点映射到从0开始的连续的点上,就大大压缩了空间。
    • 离散化过程:去重+排序后二分查找确定位置并用位置的值代替原值(大小关系并没有改变)。

     

    Code:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<queue>
     5 #include<set>
     6 #include<cstring>
     7 using namespace std;
     8 #define lson o<<1
     9 #define rson o<<1|1
    10 #define INF 0x3f3f3f3f
    11 #define M(a, b) memset(a, b, sizeof(a))
    12 const int maxn = 1e5 + 10;
    13 int Lazy[maxn<<2], L[maxn], R[maxn], cnt[maxn<<1], qL, qR, ans, sz, v;
    14 set<int> s;
    15  
    16 void build() {
    17     M(Lazy, 0);
    18     ans = 0;
    19     sz = 0;
    20     s.clear();
    21 }
    22 
    23 void pushdown(int o) {
    24     if (Lazy[o]) {
    25         Lazy[lson] = Lazy[rson] = Lazy[o];
    26         Lazy[o] = 0;
    27     }
    28 }
    29 
    30 void update(int o, int L, int R) {
    31     if (qL <= L && R <= qR) {
    32         Lazy[o] = v;
    33     }
    34     else {
    35         int M = (L + R) >> 1;
    36         pushdown(o);
    37         if (qL <= M) update(lson, L, M);
    38         if (M < qR) update(rson, M+1, R);
    39     }
    40 }
    41 
    42 void query(int o, int L, int R) {
    43     int x = Lazy[o];
    44     if (x) {
    45         if (!s.count(x)) {
    46             s.insert(x);
    47             ++ans;
    48         }
    49         return;
    50     }
    51     if (L == R) return;
    52     else {
    53         int M = (L + R) >> 1;
    54         query(lson, L, M);
    55         query(rson, M+1, R);
    56     }
    57 }
    58 
    59 int main() {
    60     int T, n;
    61     scanf("%d", &T);
    62     while(T--) {
    63         build();
    64         scanf("%d", &n);
    65         int tot = 0;
    66         for (int i = 0; i < n; ++i) {
    67             scanf("%d%d", &L[i], &R[i]);
    68             cnt[tot++] = L[i];
    69             cnt[tot++] = R[i];
    70         }
    71         sort(cnt, cnt+tot);
    72         int m = unique(cnt, cnt+tot) - cnt;
    73         int temp = m;
    74         for (int i = 1; i < temp; ++i)
    75             if (cnt[i] - cnt[i-1] > 1)
    76                 cnt[m++] = cnt[i-1] + 1;
    77         sort(cnt, cnt+m);
    78         for (int i = 0; i < n; ++i) {
    79             qL = lower_bound(cnt, cnt+m, L[i]) - cnt;
    80             qR = lower_bound(cnt, cnt+m, R[i]) - cnt;
    81             v = ++sz;
    82             update(1, 0, m);
    83         }
    84         query(1, 0, m);
    85         printf("%d
    ", ans);
    86     }
    87 
    88     return 0;
    89 }
  • 相关阅读:
    Windows XP下安装和配置Apache2.2.22服务器+PHP5+Mysql5
    win7下80端口被(Pid=4)占用的解决方法
    netty入门实例
    java NIO经典实例
    Eclipse下快速打开本地文件插件EasyExplorer(转)
    Nexus配置
    Maven依赖(转)
    【原创】C#玩高频数字彩快3的一点体会
    【原创】.NET读写Excel工具Spire.Xls使用(2)Excel文件的控制
    【踩坑经历】一次Asp.NET小网站部署踩坑和解决经历
  • 原文地址:https://www.cnblogs.com/robin1998/p/6480678.html
Copyright © 2020-2023  润新知