Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 45703   Accepted: 13239

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

用 col[rt] 来记录如今是什么颜色, -1 表示这个区间有多个颜色..

区间的范围比较大,先对它进行离散一下。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lr rt<<1
#define rr rt<<1|1
typedef long long LL;
const int oo = 1e9+;
const double PI = acos(-1.0);
const double eps = 1e- ;
const int N = ;
const int mod = ;
int n , m ;
LL col[N<<] , tot ;
bool tag[N<<] ; void Down( int l , int r , int rt ) {
if( col[rt] != - ) col[lr] = col[rr] = col[rt] ;
}
void Up( int rt ){
if( col[lr] == col[rr] && col[lr] != - ) col[rt] = col[lr];
else col[rt] = - ;
}
void build( int l , int r , int rt ){
col[rt] = - ;
if( l == r ) return ;
int mid = (l+r)>>;
build(lson),build(rson);
}
void update( int l , int r , int rt , int L , int R , int c ) {
// cout << L <<' ' << R << ' ' << l << ' ' << r << endl ;
if( L == l && r == R ) {
col[rt] = c ; return ;
}
if( col[rt] == c ) return ;
else Down( l,r,rt ) , col[rt] = - ;
int mid = (l+r)>>;
if( R <= mid ) update(lson,L,R,c);
else if( L > mid ) update(rson,L,R,c);
else update(lson,L,mid,c) , update(rson,mid+,R,c);
Up(rt);
}
LL query( int l , int r , int rt , int x ) {
if( col[rt] != - ) return col[rt];
Down(l,r,rt);
int mid = (l+r)>>;
if( x <= mid ) return query(lson,x);
else return query(rson,x);
}
struct node {
int num , new_num , id ;
}e[N];
bool cmp1( const node &a , const node &b ) { return a.num < b.num ; }
bool cmp2( const node &a , const node &b ) { return a.id < b.id ; } int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif // LOCAL
int _ ; scanf("%d",&_);
while( _-- ) {
memset( tag , false , sizeof tag );
tot = ; scanf("%d",&n);
for( int i = ; i < n ; ++i ) {
scanf("%d",&e[tot].num); e[tot].id = tot ; tot++;
scanf("%d",&e[tot].num); e[tot].id = tot ; tot++;
}
sort( e , e + tot , cmp1 );
e[].new_num = ;
for( int i = ; i < tot ; ++i )
e[i].new_num = (e[i].num == e[i-].num?e[i-].new_num:e[i-].new_num+);
sort( e , e + tot , cmp2 );
n = tot+;
build(root);
for( int i = ; i < tot ; i+= ){
// cout << e[i].new_num << ' ' << e[i+1].new_num << endl ;
update(root,e[i].new_num,e[i+].new_num,i+);
}
// cout << "ok" << endl ;
for( int i = ; i <= n ; ++i ) {
// cout << query(root,i) << endl ;
int color = query(root,i) ;
if( color == - ) continue ;
tag[color] = true ;
}
int ans = ;
for( int i = ; i <= n ; ++i ) if(tag[i]) ans++;
printf("%d\n",ans);
}
}

POJ 2528 Mayor's posters(线段树,区间覆盖,单点查询)的更多相关文章

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

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

  2. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

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

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  4. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

  5. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  6. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  7. poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  8. poj 2528 Mayor's posters(线段树)

    题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...

  9. POJ 2528 Mayor's posters (线段树)

    题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...

随机推荐

  1. C# 获取一个文件的MD5值

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  2. linux命令中chmod 777 以及drwxr-xr-x分别代表什么意思

    最近跟一个运维人员学了点新东西,感觉以前没怎么注意,但现在感觉很有用,特来记录一下. linux使用==ll==命令列出列表的时候,前面总是有一堆drwxr-xr-x ,这些代表什么意思从来还没有去在 ...

  3. RedisTemplate 事务处理方法 watch multi exec 的使用

    @Autowired RedisTemplate<String,String> redisTemplate; redisTemplate.execute(new SessionCallba ...

  4. sklearn.metrics.mean_absolute_error

    注意多维数组 MAE 的计算方法 * >>> from sklearn.metrics import mean_absolute_error >>> y_true ...

  5. Usage of hdf2v3 and hdf2file

    备注 修改Filetype,再执行hdf2file或hdf2tab,可以输出不同类型的数据.把Filetype设置成8,就是 Tecplot 格式的数据. <!DOCTYPE html PUBL ...

  6. 关于scikit-learn

    机器学习scikit-learn scikit-learn官网学习资料非常丰富,完全可以自学: http://scikit-learn.org/ 目前就以scikit-learn为主要工具学习mach ...

  7. [洛谷P3205] HNOI2010 合唱队

    问题描述 为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个人的身高为Hi米(1000<=Hi<= ...

  8. (arm板子tensorflow安装)armv7板子pip安装的wheel

    树莓派之类的armv7板子在,安装 numpy,scipy时经常失败,因为安装过程是下载源码包到本地编译,然后再安装的,编译过程中往往就会失败. https://www.piwheels.org/si ...

  9. [USACO2011 Feb]Best Parenthesis

    Time Limit: 10 Sec Memory Limit: 128 MB Description Recently, the cows have been competing with stri ...

  10. No module named 'requests'---问题解决记录

    今天在用Pycharm执行脚本时,报错.如下: 问题排查: 1,检查是否安装了requests cmd输入命令:pip install requests 提示有新版本可以升级,那 我就升级了. 然后输 ...