POJ 2528 Mayor's posters(线段树,区间覆盖,单点查询)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 45703 | Accepted: 13239 |
Description
- 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
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
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(线段树,区间覆盖,单点查询)的更多相关文章
- POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)
题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
- poj 2528 Mayor's posters(线段树)
题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...
- POJ 2528 Mayor's posters (线段树)
题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...
随机推荐
- passwd - 密码文件
描述 Passwd 是个文本文件, 它包含了一个系统帐户列表, 给出每个帐户一些有用的信息,比如用户 ID,组 ID, 家目录, shell,等. 通常它也包含了每个用户经过加密的密码. 它通常应该是 ...
- 12JDBC
1.JDBC概述 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java ...
- sqlmap 基本使用步骤(二)
post------------------------------------------------------------------1.使用 -rpython sqlmap.py -r pos ...
- LA 6834 Shopping
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- Jenkines邮件中添加图片
1.在Jenkins的邮件插件 Email-ext中的Default Content内容编写html文件,简单模板如下: <html> <head> </head&g ...
- C++指针的指针和指针的引用
https://www.cnblogs.com/li-peng/p/4116349.html
- Android之SAX解析笔记
books.xml: <?xml version="1.0" encoding="utf-8"?> <books> <book i ...
- python设置文字输出颜色
#!/usr/bin/env python # -*- coding:utf-8 -*- """ @Time: 2018/5/5 20:43 @Author: Jun H ...
- .HDF数据库与SQLSERVER / ORACLE的区别
无论ArcGIS的.gbd文件还是MapGIS的.hdf文件,都是数据库文件. 后缀是无意义的.有意义的是其中内在的逻辑和数据结构. https://zhidao.baidu.com/question ...
- HDU 5952 Counting Cliques(dfs)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...