题意:

在1~10000000这个区间中读取n个海报的区间信息,后面的海报会覆 盖前面的海报,问最后能看到几张海报.(本题是一道bug题下面会提)

题目:

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)离散化,将所有出现过的点存储在排序,然后去掉相同的点。如下面的例子 (题目的样例),因为单位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[7] = 8; X[8] = 10

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

1.先把每条线段的单独存下来,同时把每个端点的值存在另一个数组里,
在此把图放上for(int i=1; i<=m; i++){scanf("%d%d",&a[i],&b[i]);c[k++]=a[i];c[k++]=b[i];}
2.来一发排序,sort即可,线段树是以点的形式存储了线段,所以难免会有局限,(在这里我们只看点,其实
是有问题的,正确着做反而是错误的,因为若【2,3】,【4,5】,线段树只对点操作,认为是连续的,实际
不连续,所有应该两次sort,第一次需判断两点之间是否间隔为一,若是在两点之间加一个数,第二次sort
去重。)sort(c,c+k);
3.然后把数组中重复的元素去掉,因为接下来我们要用线段树查找来确定每条线段的标号,
去重才能保证标号唯一。for(int i=0; i<k; i++){if(c[i]!=c[i+1])d[c[i]]=++tot;}
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=1e4+10;//线段树开4倍空间
const int N=1e7+10;
int a[M],b[M],c[M*2],d[N],dp[M*8],add[M*8],book[M*2],ans;
void dfs(int o)
{if(add[o])//懒惰标记下移{add[o<<1]=add[o<<1|1]=add[o];dp[o<<1]=dp[o<<1|1]=add[o];add[o]=0;}
}
void update(int o,int l,int r,int x,int y,int c)//区间更新
{if(x<=l&&y>=r)/*对值覆盖*/{add[o]=c;dp[o]=c;return ;}dfs(o);int mid=(r+l)>>1;if(x<=mid)update(o<<1,l,mid,x,y,c);if(y>mid)update(o<<1|1,mid+1,r,x,y,c);
}
void query(int o,int l,int r)
{if(l==r){if(!book[dp[o]]){ans++;book[dp[o]]=1;}return ;}dfs(o);int mid=(l+r)>>1;query(o<<1,l,mid);query(o<<1|1,mid+1,r);
}
int main()
{int t;scanf("%d",&t);while(t--){int m,k=0,tot=0;memset(book,0,sizeof(book));memset(add,0,sizeof(add));scanf("%d",&m);for(int i=1; i<=m; i++){scanf("%d%d",&a[i],&b[i]);c[k++]=a[i];c[k++]=b[i];}sort(c,c+k);/*排序操作,目的去重*/for(int i=0; i<k; i++){if(c[i]!=c[i+1]) /*离散化操作,去重*/d[c[i]]=++tot;}for(int i=1; i<=m; i++){int x=d[a[i]];int y=d[b[i]];update(1,1,tot,x,y,i);//由于每次的海报不同所以直接给海报一个编号}ans=0;query(1,1,tot);printf("%d\n",ans);}return 0;
}

Mayor's posters POJ - 2528 (离散化+线段树)相关推荐

  1. poj 2528 离散化+线段树 hdu 1698 线段树 线段树题目类型一:染色计数 外加离散化

    第一次听到离散化是今年省赛的时候,一道矩形并的题,很水,就两个矩形... 今天再去做线段树已经发现离散化忘得差不多了...水逼的悲哀啊... 先看简单点的hdu 1698 http://acm.hdu ...

  2. poj/OpenJ_Bailian - 2528 离散化+线段树

    传送门:http://bailian.openjudge.cn/practice/2528?lang=en_US //http://poj.org/problem?id=2528 题意: 给你n长海报 ...

  3. POJ - 2528 Mayor's posters (浮水法+线段树/离散化+线段树)

    题目链接 题意: n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000) .求出最后还能看见多少张海报. 分析1 离散 ...

  4. POJ - 2528 (线段树区间染色 + 离散化)

    mle了30来发 花了两个半小时 终于找到bug a了这题 a了之后不知道该开心还是该骂自己sb 题目思路 题目挺简单的就是区间染色 最后统计总区间的颜色个数 因为区间数不算大 但是区间的端点值可以很 ...

  5. POJ 2352 Stars (线段树)

    POJ 2352 Stars (线段树) 手动博客搬家:本文发表于20170819 22:11:49, 原地址https://blog.csdn.net/suncongbo/article/detai ...

  6. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

  7. [牛客网#35D 树的距离]离散化+线段树合并

    [牛客网#35D 树的距离]离散化+线段树合并 分类:Data Structure SegMent Tree Merge 1. 题目链接 [牛客网#35D 树的距离] 2. 题意描述 wyf非常喜欢树 ...

  8. 850. 矩形面积 II:扫描线+离散化+线段树

    Difficulty: hard 标签: 扫描线, 离散化, 线段树 题目链接 力扣 题目解析 面试代码 /** x轴方向使用扫描线,y轴方向使用线段树维护扫描线的长度和每个区间覆盖的次数.由于y轴方 ...

  9. 离散化/线段树 (POJ - 2528 Mayor's posters)

    Mayor's posters https://vjudge.net/problem/POJ-2528#author=szdytom 线段树 + 离散化 讲解:https://blog.csdn.ne ...

最新文章

  1. ACM MM'21 | 超轻量8.5M!更高效的RGB-D显著性检测模型DFM-Net
  2. 带给你灵感的20个漂亮的单页网站设计作品
  3. BWA SAM文件格式
  4. Linux时间矫正流程,Linux 时间矫正命令
  5. SpringMVC以及SSM整合
  6. (转)版本管理工具介绍——SVN篇(一)
  7. 大道至简第七第八章读后感
  8. 立足共赢 | 开宸引领绿色会展经济新形式
  9. 为何网站天天更新收录还是那么慢吗
  10. mysql事务隔离级别回顾
  11. 触控屏c语言程序,触摸屏编程软件 C-more Micro Programming Software V3.0
  12. Rational Rose如何删除线段
  13. leetcode解题思路分析(一百二十八)1053 - 1078 题
  14. storm人偶_STORM TOYS 真人快打系列 MOTARO 茂太郎 可动人偶
  15. java大数据开发是做什么的
  16. 【vscode】调试cocos creator (Js 无法命中断点问题解决)
  17. 揭秘三端型肖特基二极管检测好坏的方法
  18. QST青软实训学员学习总结-2020/5/24-STM32
  19. 可视化脑洞|1896年以来奥运奖牌数据背后的故事
  20. 明解C语言第九章部分答案和头歌部分答案

热门文章

  1. Android之Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains
  2. 二分查找找下标或者值
  3. mysql分页原理和sqlserver里面序列的用法
  4. 油管螺纹尺寸对照表_yt15硬质合金刀片尺寸|A320焊接刀头参数
  5. 为什么卫星天线长得像口大锅?
  6. 看了这几幅图,感觉自己物理白学了!
  7. 谁还没个黑历史了。。。 | 今日最佳
  8. mysql映射超_Hibernate的映射类型 hibernate mysql映射类型
  9. ftp上传图片出现550_FtpClient 实现文件上传
  10. mysql连接主备_mysql主备配置方法