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。

代码如下:

 #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std; const int MAXN = ;
int T;
int N;
int x[MAXN] , y[MAXN];
int lisan[MAXN*];
bool vis[MAXN*];
int tree[MAXN*]; //离散化要开16倍空间
int size ;
int num = ;
int tmp;
int ans ;
void pushdown(int rt )
{
tree[rt*] = tree[rt];
tree[rt*+] = tree[rt];
tree[rt] = ;
}
void Update(int tpx ,int tpy ,int value, int l ,int r ,int rt)
{
if(l>=tpx&&r<=tpy)
{
tree[rt] = value;
return;
}
if(tree[rt]!=)
pushdown(rt);
int m = (l+r)/; if(tpx<=m)
{
Update(tpx,tpy,value,l,m,rt*);
}
if(tpy>m)
{
Update(tpx,tpy,value,m+,r,rt*+);
} }
void Query(int l ,int r ,int rt)
{
if(tree[rt]!=)
{
if(!vis[tree[rt]])
{
ans++;
vis[tree[rt]] = ;
}
return ;
}
if(l==r)
return;
if(tree[rt]!=)
{
pushdown(rt);
}
int m = (l+r)/;
Query(l,m,rt*);
Query(m+,r,rt*+);
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
num = ;
memset(tree,,sizeof(tree));
memset(vis,,sizeof(vis));
for(int i = ; i <= N ;i++)
{
scanf("%d%d",&x[i],&y[i]);
lisan[num++] = x[i];
lisan[num++] = y[i];
}
sort(lisan,lisan+num); //排序后离散化
size = unique(lisan,lisan+num)-lisan; //获得离散化后的长度
tmp = size;
for(int i = ; i < tmp ;i++)
{
if(lisan[i]-lisan[i-]>) //这样离散化才正确;见上面分析;
{
lisan[size++] = lisan[i-] + ;
}
}
sort(lisan,lisan+size); //再排序一次
for(int i = ; i <= N ;i++ )
{
int tpx = lower_bound(lisan,lisan+size,x[i])-lisan;//获取位置
int tpy = lower_bound(lisan,lisan+size,y[i])-lisan;
Update(tpx,tpy,i,,size-,);
}
ans = ;
Query(,size-,);
printf("%d\n",ans);
}
return ;
}

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

  1. Mayor's posters POJ - 2528 线段树区间覆盖

    //线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...

  2. HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)

    http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便, ...

  3. [NOI2015] 软件包管理器【树链剖分+线段树区间覆盖】

    Online Judge:Luogu-P2146 Label:树链剖分,线段树区间覆盖 题目大意 \(n\)个软件包(编号0~n-1),他们之间的依赖关系用一棵含\(n-1\)条边的树来描述.一共两种 ...

  4. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

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

    题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...

  6. POJ-2528 Mayor's posters(线段树区间更新+离散化)

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

  7. POJ 2528 ——Mayor's posters(线段树+区间操作)

    Time limit 1000 ms Memory limit 65536 kB Description The citizens of Bytetown, AB, could not stand t ...

  8. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  9. 【POJ】2528 Mayor's posters ——离散化+线段树

    Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K   Description The citizens of Bytetown, A ...

随机推荐

  1. request_mem_region,ioremap 和phys_to_virt()

    转载: request_mem_region,ioremap 和phys_to_virt()   Linux在头文件include/linux/ioport.h中定义了三个对I/O内存资源进行操作的宏 ...

  2. nc之一:NetCat简介与使用方法

    精品学习网考试频道小编应广大考生的需要,特为参加考试的考生策划了“NetCat简介与使用方法”专题等有关资料,供考生参考! 在入侵中它是最经典的工具之一 ,NetCat被所有的网络安全爱好者和研究者称 ...

  3. Python命令模块argparse学习笔记(一)

    首先是关于-h/--help参数的设置 description:位于help信息前,可用于描述helpprog:描述help信息中程序的名称epilog:位于help信息后usage:描述程序的用途a ...

  4. Python垃圾回收机制:gc模块

    在Python中,为了解决内存泄露问题,采用了对象引用计数,并基于引用计数实现自动垃圾回收. 由于Python 有了自动垃圾回收功能,就造成了不少初学者误认为不必再受内存泄漏的骚扰了.但如果仔细查看一 ...

  5. Emulator PANIC: Could not open: AVD2.3.1

    这是这两年的sdk才需要这样,以前这样根本没错的 在环境变量 里面增加一个系统变量ANDROID_SDK_HOME,值就是当前的系统用户文件夹的位置.比如c:\\Users\xxx(不要加.andro ...

  6. ROS Learning-003 beginner_Tutorials 创建ROS工作空间

    ROS Indigo beginner_Tutorials-02 创建ROS工作空间 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04. ...

  7. g2o20160424 CMakeLists.txt

    LIB_PREFIX: 设置生成库的前缀 SET(LIB_PREFIX g2o_) # The library prefix SET(LIB_PREFIX g2o_) 变量的默认配置 # defaul ...

  8. IB使用

    A:给控件添加方法或变量. 1.窗口上拖个控件 NSButton 2..点右上那张狗脸(Editor)对上的. 3.右键控件.拖到头文件中. 4 .选择加响应方法或变量.

  9. noi.ac day3t2 染色

    传送门 分析 dp[i][j]为考虑前i个位置,[i-j+1,i]中的颜色互不相同,并且ai-j与这段区间中的某一个位置颜色相同 我们枚举第i+1个位置和[i-j+1,i]中的哪一个颜色相同或者全部不 ...

  10. 100722E The Bookcase

    传送门 题目大意 给你一些书的高度和宽度,有一个一列三行书柜,要求放进去书后,三行书柜的高的和乘以书柜的宽度最小.问这个值最小是多少. 分析 我们可以先将所有书按照高度降序排好,这样对于每一层只要放过 ...