POJ2528 Mayor's posters(线段树区间更新,离散化)

2023-10-05 23:59

本文主要是介绍POJ2528 Mayor's posters(线段树区间更新,离散化),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

Mayor's posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 66425 Accepted: 19177

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 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

Source

Alberta Collegiate Programming Contest 2003.10.18

[Submit]   [Go Back]   [Status]   [Discuss]

思路:

在一面长度为很长很长的墙上贴海报,每张海报的高度一样,唯一的区别是贴海报的位置不同,问最后能看见几张海报。

这一道题直接搞肯定TLE+MLE,所以我们要用线段树+离散化来搞。

在这道题之前,我也不知道离散化是什么东西,离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了。

通俗点说,离散化就是压缩区间,使原有的长区间映射到新的短区间,但是区间压缩前后的覆盖关系不变。举个例子:

有一条1到10的数轴(长度为9),给定4个区间[2,4] [3,6] [8,10] [6,9],覆盖关系就是后者覆盖前者,每个区间染色依次为 1 2 3 4。

现在我们抽取这4个区间的8个端点,2 4 3 6 8 10 6 9

然后删除相同的端点,这里相同的端点为6,则剩下2 4 3 6 8 10 9

对其升序排序,得2 3 4 6 8 9 10

然后建立映射

2     3     4     6     8     9   10

↓     ↓      ↓     ↓     ↓     ↓     ↓

1     2     3     4     5     6     7

那么新的4个区间为 [1,3] [2,4] [5,7] [4,6],覆盖关系没有被改变。新数轴为1到7,即原数轴的长度从9压缩到6,显然构造[1,7]的线段树比构造[1,10]的线段树更省空间,搜索也更快,但是求解的结果却是一致的。

------------------------------------------------但是这样有缺陷------------------

而这题的难点在于每个数字其实表示的是一个单位长度(并且一个点),这样普通的离散化会造成许多错误
给出下面两个简单的例子应该能体现普通离散化的缺陷:
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


代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define N 100050
#define ll long long
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int vis[N];
int li[N],ri[N];
int X[N*3];
int sum[N<<4];
int cnt;
void pushdown(int rt)
{if(sum[rt]!=-1){sum[rt<<1]=sum[rt<<1|1]=sum[rt];//直接覆盖sum[rt]=-1;}
}
void update(int L,int R,int c,int l,int r,int rt)
{if(L<=l&&r<=R){sum[rt]=c;return;}pushdown(rt);int m=(l+r)>>1;if(L<=m) update(L,R,c,lson);if(m<R) update(L,R,c,rson);
}
void query(int l,int r,int rt)
{if(sum[rt]!=-1){if(!vis[sum[rt]])cnt++;vis[sum[rt]]=1;return;}if(l==r) return;int m=(l+r)>>1;query(lson);query(rson);
}
int bin(int k,int n,int x[])//离散化哈希函数
{int l=0,r=n-1;while(l<=r){int m=(l+r)>>1;if(x[m]==k)return m;if(x[m]<k)l=m+1;elser=m-1;}return -1;
}
int main()
{int t,n;scanf("%d",&t);while(t--){scanf("%d",&n);int num=0;for(int i=0; i<n; i++){scanf("%d%d",&li[i],&ri[i]);X[num++]=li[i];X[num++]=ri[i];//用X记录所有出现过的数}sort(X,X+num);int m=1;//去重for(int i=1; i<num; i++)if(X[i]!=X[i-1])X[m++]=X[i];//离散化的技巧,在相差大于1的数间加一个数for(int i=m-1; i>0; i--)if(X[i]!=X[i-1]+1)X[m++]=X[i-1]+1;sort(X,X+m);mem(sum,-1);//初始化为-1//二分寻找当前的区间在离散化中对应的区间for(int i=0; i<n; i++){int l=bin(li[i],m,X);int r=bin(ri[i],m,X);update(l,r,i,0,m,1);}cnt=0;mem(vis,0);//查询[0,m]区间的个数query(0,m,1);printf("%d\n",cnt);}return 0;
}


经过STL优化后:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define N 100050
#define ll long long
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int vis[N];
int li[N],ri[N];
int X[N*3];
int sum[N<<4];
int cnt;
void pushdown(int rt)
{if(sum[rt]!=-1){sum[rt<<1]=sum[rt<<1|1]=sum[rt];sum[rt]=-1;}
}
void update(int L,int R,int c,int l,int r,int rt)
{if(L<=l&&r<=R){sum[rt]=c;return;}pushdown(rt);int m=(l+r)>>1;if(L<=m) update(L,R,c,lson);if(m<R) update(L,R,c,rson);
}
void query(int l,int r,int rt)
{if(sum[rt]!=-1){if(!vis[sum[rt]])cnt++;vis[sum[rt]]=1;return;}if(l==r) return;int m=(l+r)>>1;query(lson);query(rson);
}
int main()
{int t,n;scanf("%d",&t);while(t--){scanf("%d",&n);int num=0;for(int i=0; i<n; i++){scanf("%d%d",&li[i],&ri[i]);X[num++]=li[i];X[num++]=ri[i];}sort(X,X+num);int len=unique(X,X+num)-X;int m=len;for(int i=m-1; i>0; i--)if(X[i]!=X[i-1]+1)X[m++]=X[i-1]+1;sort(X,X+m);mem(sum,-1);for(int i=0; i<n; i++){int l=lower_bound(X,X+m,li[i])-X;int r=lower_bound(X,X+m,ri[i])-X;update(l,r,i,0,m,1);}cnt=0;mem(vis,0);query(0,m,1);printf("%d\n",cnt);}return 0;
}

--------------------------------------------------------------------------------------

2018年02月10日13:31:55更新:

在一面长度为很长很长的墙上贴海报,每张海报的高度一样,唯一的区别是贴海报的位置不同,问最后能看见几张海报。

因为墙的长度长,直接线段树可能超时,所以要进行离散化处理,将离散后的坐标映射到线段树中,并不影响最后所求的海报长度.在这道题中,因为海报是按照来算的,而不是,所以可能会出现离散化之后有缝隙的问题,比如:[1,10]、[1,3]、[6,10]离散化之后是[1,4]、[1,2]、[3,4]这样会导致2和3之间的缝隙丢失导致错误,所以再去重完毕后,进行一个处理,如果相邻数字间距大于1的话,在其中加上任意一个数字。这样会把原来的缝隙留出来.

线段树的作用是利用lazy标记,当需要覆盖的时候,就把当前这一段区间的lazy变成当前第几张海报的标号,查询的时候只需要利用一个数组标记一下当前点是否访问过且有lazy标记,有的话就记录一下数量


#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
const int N=10000+50;
int lazy[N<<4];
int vis[N],li[N],ri[N],X[N<<2];
int cnt;
void pushdown(int rt)//下放lazy
{if(~lazy[rt]){lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];lazy[rt]=-1;}
}
void update(int L,int R,int c,int l,int r,int rt)
{if(L<=l&&r<=R){lazy[rt]=c;return;}pushdown(rt);int m=(l+r)>>1;if(L<=m) update(L,R,c,lson);if(R>m) update(L,R,c,rson);
}
void query(int l,int r,int rt)
{if(~lazy[rt]){if(!vis[lazy[rt]])cnt++;vis[lazy[rt]]=1;return;}if(l==r) return;int m=(l+r)>>1;query(lson);query(rson);
}int main()
{int t,n;scanf("%d",&t);while(t--){scanf("%d",&n);int num=0;for(int i=0; i<n; i++){scanf("%d%d",&li[i],&ri[i]);X[num++]=li[i];X[num++]=ri[i];}sort(X,X+num);int m=unique(X,X+num)-X;//离散化for(int i=m-1; i>0; i--)//有缝隙的情况if(X[i]!=X[i-1]+1)X[m++]=X[i-1]+1;sort(X,X+m);mem(lazy,-1);for(int i=0; i<n; i++){int l=lower_bound(X,X+m,li[i])-X;//寻找位置int r=lower_bound(X,X+m,ri[i])-X;update(l,r,i,0,m,1);}cnt=0;mem(vis,0);//查询[0,m]区间个数query(0,m,1);printf("%d\n",cnt);}return 0;
}




这篇关于POJ2528 Mayor's posters(线段树区间更新,离散化)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/152330

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

AI行业应用(不定期更新)

ChatPDF 可以让你上传一个 PDF 文件,然后针对这个 PDF 进行小结和提问。你可以把各种各样你要研究的分析报告交给它,快速获取到想要知道的信息。https://www.chatpdf.com/

GIS图形库更新2024.8.4-9.9

更多精彩内容请访问 dt.sim3d.cn ,关注公众号【sky的数孪技术】,技术交流、源码下载请添加微信:digital_twin123 Cesium 本期发布了1.121 版本。重大新闻,Cesium被Bentley收购。 ✨ 功能和改进 默认启用 MSAA,采样 4 次。若要关闭 MSAA,则可以设置scene.msaaSamples = 1。但是通过比较,发现并没有多大改善。

poj 1127 线段相交的判定

题意: 有n根木棍,每根的端点坐标分别是 px, py, qx, qy。 判断每对木棍是否相连,当他们之间有公共点时,就认为他们相连。 并且通过相连的木棍相连的木棍也是相连的。 解析: 线段相交的判定。 首先,模板中的线段相交是不判端点的,所以要加一个端点在直线上的判定; 然后,端点在直线上的判定这个函数是不判定两个端点是同一个端点的情况的,所以要加是否端点相等的判断。 最后

HDU4737线段树

题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i,j)<=m的总数。 const int Max_N = 100008 ;int sum[Max_N<<2] , x[Max_N] ;int n , m ;void push_up(int t){sum[t] = sum[t<<1] | sum[t<<1|1] ;}void upd

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;