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. linux安装 rsync 客户端和相关权限认证

    [root@rsync-client-inotify /]# yum install rsync -y [root@rsync-client-inotify /]# echo "redhat ...

  2. Debug to add expression

    Debug expression

  3. c# 匿名委托

    using System; namespace AnonymousMethod { delegate void ArithmeticOperation(double operand1, double ...

  4. 命令行界面CLI

    1.hive   -e --执行一个或多个查询 hive -e "select * from student limit 3" 2. hive -e     > 将临时查询保 ...

  5. [python 学习] 类

    #!/usr/bin/python # -*- encoding:utf-8 -*- class Animal: animal_num = 0 class Dog(Animal): #类帮助文档 't ...

  6. bzoj1969 [Ahoi2005]LANE 航线规划 树链剖分

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1969 题解 如果我们把整个图边双联通地缩点,那么最终会形成一棵树的样子. 那么在这棵树上,\( ...

  7. python 小工具 重命名当前文件夹内所有的文件,升序命名

    背景:一个朋友想升序重命名他的照片,但是太多了不想手动所以,emememem os这个模块,不用说,rename,filedir等 #conding=utf8 import os path = os. ...

  8. Linux安装mysql5.6.33

    1.下载mysql安装包: 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:我这里选择的5.6.33,通用版,lin ...

  9. php key()函数 语法

    php key()函数 语法 作用:返回数组内部指针当前指向元素的键名.大理石构件支架 语法:key(array) 参数: 参数 描述 array 必需.规定要使用的数组. 说明:返回数组内部指针当前 ...

  10. github配置和使用

    通过手册指导生产ssh key或取已有的ssh key root@iZwz93telmwbh624e5zetqZ:~# ls -al ~/.ssh total drwx------ root root ...