POJ - 2528Mayor's posters (离散化+线段树区间覆盖)

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 l
i 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 <= l
i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l
i, l
i+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是一个单位长度,将下面的

1   2   3   4  6   7   8   10

—  —  —  —  —  —  —  —

1   2   3   4  5   6   7   8

离散化  X[1] = 1; X[2] = 2; X[3] = 3; X[4] = 4; X[5] = 6; X[6] = 7; X[7] = 8,X[8] = 10;

于是将一个很大的区间映射到一个较小的区间之中了,然后再对每一张海报依次更新在宽度为1~8的墙上(用线段树),

最后统计不同颜色的段数。但是只是这样简单的离散化是错误的,

如三张海报为:1~10 1~4 6~10离散化时 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10

第一张海报时:墙的1~4被染为1;

第二张海报时:墙的1~2被染为2,3~4仍为1

;第三张海报时:墙的3~4被染为3,1~2仍为2。

最终,第一张海报就显示被完全覆盖了,于是输出2,但实际上明显不是这样,正确输出为3。

新的离散方法为:在相差大于1的数间加一个数,例如在上面1 4 6 10中间加5(算法中实际上1,4之间,6,10之间都新增了数的)

X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 5, X[ 4 ] = 6, X[ 5 ] = 10

这样之后,第一次是1~5被染成1;第二次1~2被染成2;第三次4~5被染成3最终,1~2为2,3为1,4~5为3,于是输出正确结果3。

代码如下:

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<algorithm>
  4 #include<string.h>
  5 using namespace std;
  6
  7 const int MAXN = 20005;
  8 int T;
  9 int N;
 10 int x[MAXN] , y[MAXN];
 11 int lisan[MAXN*3];
 12 bool vis[MAXN*3];
 13 int tree[MAXN*16];   //离散化要开16倍空间
 14 int size ;
 15 int num = 0;
 16 int tmp;
 17 int ans ;
 18 void pushdown(int rt )
 19 {
 20     tree[rt*2] = tree[rt];
 21     tree[rt*2+1] = tree[rt];
 22     tree[rt] = 0;
 23 }
 24 void Update(int tpx ,int tpy ,int value, int l ,int r ,int rt)
 25 {
 26     if(l>=tpx&&r<=tpy)
 27     {
 28         tree[rt] = value;
 29         return;
 30     }
 31     if(tree[rt]!=0)
 32     pushdown(rt);
 33     int m = (l+r)/2;
 34
 35     if(tpx<=m)
 36     {
 37         Update(tpx,tpy,value,l,m,rt*2);
 38     }
 39     if(tpy>m)
 40     {
 41         Update(tpx,tpy,value,m+1,r,rt*2+1);
 42     }
 43
 44 }
 45 void Query(int l ,int r ,int rt)
 46 {
 47     if(tree[rt]!=0)
 48     {
 49         if(!vis[tree[rt]])
 50         {
 51             ans++;
 52             vis[tree[rt]] = 1;
 53         }
 54         return ;
 55     }
 56     if(l==r)
 57     return;
 58     if(tree[rt]!=0)
 59     {
 60         pushdown(rt);
 61     }
 62     int m = (l+r)/2;
 63     Query(l,m,rt*2);
 64     Query(m+1,r,rt*2+1);
 65 }
 66
 67 int main()
 68 {
 69     scanf("%d",&T);
 70     while(T--)
 71     {
 72         scanf("%d",&N);
 73         num = 0;
 74         memset(tree,0,sizeof(tree));
 75         memset(vis,0,sizeof(vis));
 76         for(int i = 1 ; i <= N ;i++)
 77         {
 78             scanf("%d%d",&x[i],&y[i]);
 79              lisan[num++] = x[i];
 80              lisan[num++] = y[i];
 81         }
 82         sort(lisan,lisan+num);    //排序后离散化
 83         size = unique(lisan,lisan+num)-lisan;   //获得离散化后的长度
 84         tmp = size;
 85         for(int i = 1 ; i < tmp ;i++)
 86         {
 87             if(lisan[i]-lisan[i-1]>1)    //这样离散化才正确;见上面分析;
 88             {
 89                 lisan[size++] = lisan[i-1] + 1;
 90             }
 91         }
 92         sort(lisan,lisan+size);  //再排序一次
 93         for(int i = 1 ; i <= N ;i++ )
 94         {
 95             int tpx = lower_bound(lisan,lisan+size,x[i])-lisan;//获取位置
 96             int tpy = lower_bound(lisan,lisan+size,y[i])-lisan;
 97             Update(tpx,tpy,i,0,size-1,1);
 98         }
 99         ans = 0 ;
100         Query(0,size-1,1);
101         printf("%d\n",ans);
102     }
103     return 0;
104 }

POJ - 2528Mayor's posters (离散化+线段树区间覆盖)

原文地址:https://www.cnblogs.com/yewanting/p/10800301.html

时间: 2024-10-05 06:40:38

POJ - 2528Mayor's posters (离散化+线段树区间覆盖)的相关文章

poj 2528 Mayor&#39;s posters(线段树区间覆盖、离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49385   Accepted: 14304 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

POJ 2528 Mayor&#39;s posters(线段树区间染色+离散化或倒序更新)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

POJ 2528 Mayor&#39;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

POJ - 2528 Mayor&#39;s posters (离散化+线段树区间修改)

https://cn.vjudge.net/problem/POJ-2528 题意 给定一些海报,可能相互重叠,告诉你每个海报的宽度(高度都一样的)和先后叠放顺序,问没有被完全盖住的有多少张? 分析 海报最多10000张,但是墙有10000000块瓷砖长,海报不会落在瓷砖中间. 如果直接建树,就算不TLE,也会MLE.即单位区间长度太多. 其实10000张海报,有20000个点,最多有19999个区间.对各个区间编号,就是离散化.然后建树. 可以直接进行区间修改,最后再统计. 这里采用比较巧妙的

POJ 2528 Mayor&#39;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

POJ 2528 Mayor&amp;#39;s posters 离散化+线段树

题目大意:给出一些海报和贴在墙上的区间.问这些海报依照顺序贴完之后,最后能后看到多少种海报. 思路:区间的范围太大,然而最多仅仅会有10000张海报,所以要离散化. 之后用线段树随便搞搞就能过. 关键是离散化的方法,这个题我时隔半年才A掉,之前一直就TTT,我还以为是线段树写挂了. 当我觉得我自己的水平这样的水线段树已经基本写不挂的时候又写了这个题,竟然还是T. 后来我对照别人的代码,才发现是我的离散化写渣了. 以下附AC代码(79ms),这个离散化写的比較优雅.时间也非常快,以后就这么写了.

POJ 2528——Mayor&#39;s posters——————【线段树区间替换、找存在的不同区间】

Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2528 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been plac

POJ 2528 Mayor&#39;s posters(离散化 线段树)

题意  在墙上贴n张海报  输入每张海报的的左右端点坐标  问最后可以看到多少张海报  能看到一点也是能看到 先把线段树初始化为0 输入一张海报  就把那个区间变成这张海报的序号  最后判断墙上有多少个不同的序号就行了 但是海报坐标的端点值高达10000000  直接用线段树会超时   但是注意到海报最多只有10000张  也就是最多有20000个不同的坐标  于是可以利用离散化的知识   把所有坐标排序 注意所有右端点坐标+1也要加入排序(注意1,10 ; 1,3;  7,10这种情况  如果

poj 2528 Mayor&#39;s posters (线段树+区间离散)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45031   Accepted: 13080 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post