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

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

由于点的个数到达了1e7,、而更新却1e4所以斟酌离散化。

然后想到了map结果果断超时,就只能换种方法。

普遍做法是2维数组mp[maxn<<2][2]放(l,r)区间。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int maxn=100100;
int col[maxn<<2],vis[maxn<<2];
int mp[maxn<<2][2];
struct node{
int val;//值
int num;//节点编号
}e[maxn<<2];
int t,m,ans;
void pushdown(int rs)
{
if(col[rs])
{
col[rs<<1]=col[rs<<1|1]=col[rs];
col[rs]=0;
}
}
void build(int rs,int l,int r)
{
col[rs]=0;
if(l==r)
return ;
int mid=(l+r)>>1;
build(rs<<1,l,mid);
build(rs<<1|1,mid+1,r);
}
void update(int x,int y,int c,int l,int r,int rs)
{
if(l>=x&&r<=y)
{
col[rs]=c;
return ;
}
pushdown(rs);
int mid=(l+r)>>1;
if(x<=mid) update(x,y,c,l,mid,rs<<1);
if(y>mid) update(x,y,c,mid+1,r,rs<<1|1);
}
void query(int l,int r,int rs)
{
if(col[rs])
{
if(!vis[col[rs]])
ans++;
vis[col[rs]]=1;
return ;
}
if(l==r) return ;
int mid=(l+r)>>1;
query(l,mid,rs<<1);
query(mid+1,r,rs<<1|1);
}
int cmp(node l1,node l2)
{
return l1.val<l2.val;
}
int main()
{
scanf("%d",&t);
while(t–)
{
scanf("%d",&m);
int l=1;
REP(i,m)
{
scanf("%d%d",&mp[i][0],&mp[i][1]);
e[i<<1].val=mp[i][0];
e[i<<1].num=-(i+1);//左端点置为负区分右端点,num可恢复原来的店编号
e[i<<1|1].val=mp[i][1];
e[i<<1|1].num=i+1;
}
sort(e,e+2*m,cmp);
int tmp=e[0].val,len=1;
REP(i,2*m)
{
if(e[i].val!=tmp)
{
len++;
tmp=e[i].val;
}
if(e[i].num<0)
mp[-e[i].num⑴][0]=len;
else
mp[e[i].num⑴][1]=len;
}
build(1,1,len);
CLEAR(vis,0);
REP(i,m)
update(mp[i][0],mp[i][1],i+1,1,len,1);//正常的更新
ans=0;
query(1,len,1);
printf("%d
",ans);
}
return 0;
}

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » POJ 2528 Mayor's posters(离散化线段树)

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡