zoukankan      html  css  js  c++  java
  • poj2528 Mayor's posters(线段树区间覆盖)

    Mayor's posters
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 50888   Accepted: 14737

    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

    离散化比较重要,如果两个点差值大于一,那么他们之中应该有空出来的区间,那么在他们之间加入一个点;但是如果差值为一,那么就不用在其中加点,因为他们之中并没有区间
    只有这样(1, 10), (1, 6), (8, 10) 这样6和8之间,只有加点才会使7被考虑在内

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 int xx[50005], yy[50005];
     6 int sor[50005], cnt, deal[50005], col[500005];
     7 int ans, vis[40005];
     8 int hi;
     9 void hash() {
    10     int last = sor[1];
    11     hi = 0;
    12     memset(deal, 0, sizeof(deal));
    13     deal[++hi] = last;
    14     for (int i = 2; i <= cnt; i++ ) {
    15         if (sor[i] == last) continue;
    16         if (sor[i] - last > 1) deal[++hi] = last + 1, deal[++hi] = sor[i], last = sor[i];
    17         else deal[++hi] = sor[i], last = sor[i];
    18     }
    19     sort(deal + 1, deal + 1 + hi);
    20     return;
    21 }
    22 int Bsearch(int x) {
    23     int low = 1, high = hi;
    24     while (low <= high) {
    25         int mid = low + high >> 1;
    26         if (deal[mid] == x) return mid;
    27         if (deal[mid] < x) low = mid + 1;
    28         else high = mid - 1;
    29     }
    30     return -1;
    31 }
    32 void Push_down(int o) {
    33     if (col[o] != -1) {
    34         col[o << 1] = col[o << 1 | 1] = col[o];
    35         col[o] = -1;
    36     }
    37 }
    38 void query(int o, int l, int r) {
    39     if (col[o] != -1) {
    40         if (!vis[col[o]]) ans++;
    41         vis[col[o]] = true;
    42         return;
    43     }
    44     if (l == r) return;
    45     int mid = (l + r) >> 1;
    46     query(o << 1, l, mid);
    47     query(o << 1 | 1, mid + 1, r);
    48 }
    49 void update(int o, int l, int r, int ql, int qr, int v) {
    50     if (ql <= l && r <= qr) {
    51         col[o] = v;
    52         return;
    53     }
    54     Push_down(o);
    55     int mid = (l + r)>> 1;
    56     if (ql <= mid) update(o << 1, l, mid, ql, qr, v);
    57     if (qr > mid) update(o << 1 | 1, mid + 1, r, ql, qr, v);
    58 }
    59 int main() {
    60     int t, n;
    61     scanf("%d", &t);
    62     while (t--) {
    63         ans = 0;
    64         scanf("%d", &n);
    65         cnt = 0;
    66         memset(sor, 0, sizeof(sor));
    67         for (int i = 1; i <= n; i++) {
    68             scanf("%d%d", &xx[i], &yy[i]);
    69             sor[++cnt] = xx[i], sor[++cnt] = yy[i];
    70         }
    71         sort(sor + 1, sor + cnt + 1);
    72         hash();
    73         memset(col, -1, sizeof(col));
    74         for (int i = 1; i <= n; i++) {
    75             int ql = Bsearch(xx[i]), qr = Bsearch(yy[i]);
    76             update(1, 1, hi, ql, qr, i);
    77         }
    78         memset(vis, 0, sizeof(vis));
    79         query(1, 1, hi);
    80         printf("%d
    ", ans);
    81     }
    82     return 0;
    83 }
    
    
  • 相关阅读:
    来电科技:基于Flink+Hologres的实时数仓演进之路
    实时计算 Flink 版总体介绍
    阿里云江岑:云原生在边缘形态下的升华
    xshell帮助
    版本控制工具之git
    批量修改ubuntu用户sudo免密码
    matplotlib按钮控制图像显示
    为ssh主机设置别名
    VScode正则表达式批量删除字符串
    SQL Server 操作XML数据
  • 原文地址:https://www.cnblogs.com/y7070/p/4755299.html
Copyright © 2011-2022 走看看