Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 56864   Accepted: 16445

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

Source

第一遍TLE代码,至今还未找出错误;
还小啊~~
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 1005
int m,ans,li[N],ri[N];
int x[N<<],col[N<<],hash[N];
void pushdown(int rt){
col[rt<<]=col[rt<<|]=col[rt];
col[rt]=-;
}
void updata(int rt,int l,int r,int val,int x,int y){
if(x>=l&&y<=r){
col[rt]=val;return ;
}
if(col[rt]!=-) pushdown(rt);
int mid=(x+y)>>;
if(mid>=l) updata(rt<<,l,r,val,x,mid);
if(mid<r) updata(rt<<|,l,r,val,mid+,y);
}
void query(int rt,int l,int r){
if(l==r){
if(!hash[col[rt]]){
hash[col[rt]]=;
ans++;
}
return ;
}
if(col[rt]!=-) pushdown(rt);
int mid=(l+r)>>;
query(rt<<,l,mid);
query(rt<<|,mid+,r);
}
int search(int ll,int hh,int xx){
while(ll<=hh){
int mid=(ll+hh)>>;
if(x[mid]==xx) return mid;
else if(x[mid]>xx) hh=mid-;
else ll=mid+;
}
return -;
}
int main(){
int t,n;
scanf("%d",&t);
while(t--){
memset(col,-,sizeof col);
memset(hash,,sizeof hash);
int tot();
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&li[i],&ri[i]);
x[++tot]=li[i];
x[++tot]=ri[i];
}
sort(x+,x+tot+);
m=;
for(int i=;i<=tot;i++){
if(x[i]!=x[i-]) x[++m]=x[i];
}
for(int i=m;i>;i--){
if(x[i]-x[i-]>) x[++m]=x[i]-;
}
sort(x+,x+m+);
for(int i=;i<=n;i++){
int l=search(,m,li[i]);
int r=search(,m,ri[i]);
updata(,l,r,i,,m);
}
ans=;
query(,,m);
printf("%d\n",ans);
}
return ;
}

题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报
思路:这题数据范围很大,直接搞超时+超内存:

需要:线段树2的板子+离散化

离散化,如下面的例子(题目的样例),因为单位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~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。

所以离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多
而这题的难点在于每个数字其实表示的是一个单位长度(并且一个点),这样普通的离散化会造成许多错误(包括我以前的代码,poj这题数据奇弱)
给出下面两个简单的例子应该能体现普通离散化的缺陷:
1-10 1-4 5-10
1-10 1-4 6-10
为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]
如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.
线段树功能:update:成段替换 query:简单hash

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 10005
int m,li[N],ri[N];
int x[N<<],col[N<<],ans;
bool hash[N];
void pushdown(int rt) {
col[rt<<]=col[rt<<|]=col[rt];
col[rt]=-;
}
void updata(int rt,int L,int R,int c,int l,int r){
if(l>=L&&r<=R){
col[rt]=c;return ;
}
if(col[rt]!=-) pushdown(rt);
int mid=(l+r)>>;
if(mid>=L) updata(rt<<,L,R,c,l,mid);
if(mid<R) updata(rt<<|,L,R,c,mid+,r);
}
void query(int rt,int l,int r){
if(l==r){
if(!hash[col[rt]]){
hash[col[rt]]=;
ans++;
}
return;
}
if(col[rt]!=-) pushdown(rt);
int mid=(l+r)>>;
query(rt<<,l,mid);
query(rt<<|,mid+,r);
}
int search(int ll,int hh,int xx){
int mid;
while (ll<=hh){
mid=(ll+hh)>>;
if(x[mid]==xx) return mid;
else if(x[mid]>xx) hh=mid-;
else ll=mid+;
}
return -;
}
int main(){
int t,n,i;
scanf("%d",&t);
while(t--){
memset(col,-,sizeof(col));
memset(hash,,sizeof(hash));
int tot=;
scanf("%d",&n);
for(i=;i<=n;i++){
scanf ("%d %d",&li[i],&ri[i]);
x[++tot]=li[i];
x[++tot]=ri[i];
}
sort(x+,x+tot+);
m=;
for(i=;i<=tot;i++){
if(x[i]!=x[i-]) x[++m]=x[i];
}
for(i=m;i>;i--){
if(x[i]-x[i-]>) x[++m]=x[i]-;
}
sort(x+,x+m+);
for(i=; i<=n; i++){
int l=search(,m,li[i]);
int r=search(,m,ri[i]);
updata(,l,r,i,,m);
}
ans=;
query(,,m);
printf("%d\n",ans);
}
return ;
}

poj2528的更多相关文章

  1. poj3468,poj2528

    其实这两题都是基础的线段树,但对于我这个线段树的初学者来说,总结一下还是很有用的: poj3468显然是线段树区间求和,区间更改的问题,而poj2528是对区间染色,问有多少种颜色的问题: 线段树的建 ...

  2. poj2528(线段树+离散化)

    题目链接:https://vjudge.net/problem/POJ-2528 题意:在区间[1,1e7]内染色,依次染n(<=1e4)中颜色,给出每种颜色染色的范围,可重叠,求最终有多少种颜 ...

  3. poj-2528线段树练习

    title: poj-2528线段树练习 date: 2018-10-13 13:45:09 tags: acm 刷题 categories: ACM-线段树 概述 这道题坑了我好久啊啊啊啊,,,, ...

  4. 线段树---poj2528 Mayor’s posters【成段替换|离散化】

    poj2528 Mayor's posters 题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要 ...

  5. POJ2528 Mayor's posters —— 线段树染色 + 离散化

    题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...

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

    题目传送门: POJ-2528 题意就是在一个高度固定的墙面上贴高度相同宽度不同的海报,问贴到最后还能看到几张?本质上是线段树区间更新问题,但是要注意的是题中所给数据范围庞大,直接搞肯定会搞出问题,所 ...

  7. ACM/ICPC 之 数据结构-线段树+区间离散化(POJ2528)

    这道题用线段树做更方便更新和查询,但是其数据范围很大,因此要将离散化和线段树结合起来,算是一道比较经典的线段树+离散化的例题. 线段树的离散化有很多方法,在这里,我先用一次结点离散化,间接将源左右端点 ...

  8. Mayor's posters(线段树+离散化POJ2528)

    Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...

  9. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  10. poj2528 Mayor's posters(线段树之成段更新)

    Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 37346Accepted: 10864 Descr ...

随机推荐

  1. Centos 7 进入单用户模式图文详解

    由于昨晚做了一个很傻X的事情,所以有幸进入了CentOS 7 的单用户模式. CentOS 7 在进入单用户的时候和6.x做了很多的改变, 下面让我们来看看如何进入单用户模式. 如何进入CentOS ...

  2. Mycat探索之旅(3)----Mycat的全局序列号

    一.本地文件方式 原理:此方式MyCAT将sequence配置到文件中,当使用到sequence中的配置后,MyCAT会更下classpath中的sequence_conf.properties文件中 ...

  3. HDU 2819 Swap (行列匹配+输出解)

    题意:是否能使对角线上全是1 ,这个简单直接按行列匹配.难在路径的输出,我们知道X,Y左右匹配完了之后,不一定是1–1,2–2,3–3--这种匹配.可能是1–3,2–1,3–2,我们要把他们交换成前一 ...

  4. [转]2年SQL Server DBA调优方面总结

      2年SQL Server DBA调优方面总结 当2年dba 我觉得,有些东西需要和大家分享探讨,先书单. 书单 1.<深入解析SQL Server 2008 系列> 这个就是mssql ...

  5. hdu 5105 Math Problem(数学)

    pid=5105" target="_blank" style="">题目链接:hdu 5105 Math Problem 题目大意:给定a.b ...

  6. n皇后问题[分支限界法]

    问题: 如何能够在 n×n 的国际象棋棋盘上放置八个皇后,使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上. 分析: 我们可以用一串数字来表示问题 ...

  7. SAP query传输以后须要又一次生成程序

    近期有个需求,须要改动一个Query,在DEV改动好并測试通过后.传输到QAS,可是报表还是没变化,着实郁闷了一下,这是万能的google帮上忙了,原来传到其它系统以后还须要generate prog ...

  8. AMD 3600+ X2 CPU配合昂达A69T主板超频教程

    本次超频使用电脑硬件配置如下: AMD 3600+ X2 90nm 昂达 A69T HDMI 宇瞻 DDR II 1G 667普条 西数 WD2500KS SATAII  250G 16M 戴尔 普通 ...

  9. Mac 常用命令介绍

    1.查看所有shell cat /etc/shells 2.查看当前使用的shell类型 $ echo $SHELL 3.

  10. 使用CSS3实现响应式标题全屏居中和站点前端性能

    要实现标题全屏居中(同一时候在垂直和水平方向居中).有若干种方法,包含使用弹性布局.表格单元.绝对定位和自己主动外边距等. 全屏居中 当中眼下比較流行也比較easy理解的方法是使用绝对定位+偏移实现. ...