• [POJ2528]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
    

    Output

    4

    【思路】

    离散化预处理左右边界,线段树进行区间更新。

     1 #include <algorithm>
     2 #include <cmath>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <list>
     6 #include <map>
     7 #include <iostream>
     8 #include <queue>
     9 #include <set>
    10 #include <stack>
    11 #include <string>
    12 #include <vector>
    13 #include <iomanip>
    14 #define LL long long
    15 #define inf 0x3f3f3f3f 
    16 #define INF 0x3f3f3f3f3f3f
    17 #define PI 3.1415926535898
    18 #define F first
    19 #define S second
    20 #define lson  rt << 1
    21 #define rson  rt << 1 | 1
    22 using namespace std;
    23 
    24 const int maxn = 2e4 + 7;
    25 const int maxm = 2e5 + 7;
    26 int n, m, a[maxn], vis[maxn];
    27 pair<int, int> p[maxn];
    28 
    29 int tree[maxn * 4], lz[maxn * 4];
    30 void push_down(int rt, int l, int r) {
    31     if (lz[rt]) {
    32         lz[lson] = lz[rt];
    33         lz[rson] = lz[rt];
    34         tree[lson] = tree[rson] = lz[rt];
    35         lz[rt] = 0;
    36     }
    37 }
    38 void update_range(int rt, int l, int r, int L, int R, int add) {
    39     if (l <= L && r >= R) {
    40         lz[rt] = add;
    41         tree[rt] = add;
    42         return;
    43     }
    44     push_down(rt, L, R);
    45     int mid = (L + R) / 2;
    46     if (mid >= l) update_range(lson, l, r, L, mid, add);
    47     if (mid < r) update_range(rson, l, r, mid + 1, R, add);
    48 }
    49 int query_range(int rt, int l, int r, int L, int R) {
    50     if (lz[rt])
    51     {
    52         if (vis[lz[rt]]) return 0;
    53         vis[lz[rt]] = 1;
    54         return 1;
    55     }
    56     if (L == R) return 0;
    57     push_down(rt, L, R);
    58     int mid = (L + R) / 2;
    59     int sum = 0;
    60     if (mid >= l) sum += query_range(lson, l, r, L, mid);
    61     if (mid < r) sum += query_range(rson, l, r, mid + 1, R);
    62     return sum;
    63 }
    64 
    65 int main()
    66 {
    67     ios::sync_with_stdio(false);
    68     cin.tie(0);
    69     int t;
    70     cin >> t;
    71     while (t--)
    72     {
    73         memset(vis, 0, sizeof(vis));
    74         memset(lz, 0, sizeof(lz));
    75         memset(tree, 0, sizeof(tree));
    76         cin >> n;
    77         int cnt = 0;
    78         for (int i = 1; i <= n; i++)
    79         {
    80             cin >> p[i].F >> p[i].S;
    81             a[++cnt] = p[i].F;
    82             a[++cnt] = p[i].S;
    83         }
    84         sort(a + 1, a + cnt + 1);
    85         int sz = unique(a + 1, a + 1 + cnt) - a - 1;
    86         for (int i = 1; i <= n; i++)
    87         {
    88             p[i].F = lower_bound(a + 1, a + sz + 1, p[i].F) - a;
    89             p[i].S = lower_bound(a + 1, a + sz + 1, p[i].S) - a;
    90         }
    91         for (int i = 1; i <= n; i++)
    92         {
    93             update_range(1, p[i].F, p[i].S, 1, sz, i);
    94         }
    95         cout << query_range(1, 1, sz, 1, sz) << endl;
    96     }
    97 }
  • 相关阅读:
    面试题目-atof与ftoa
    数据结构-List
    数据结构-Vector
    面试题目-计算最大公约数
    数据结构-二分查找
    面试题目-用递归通过单字符输出打印多位的数字
    Linux-守护进程的实现
    面试题目-链表反转
    Linux-C程序的存储空间布局
    Linux-如何添加路由表
  • 原文地址:https://www.cnblogs.com/hfcdyp/p/13371853.html
Copyright © 2020-2023  润新知