Description
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 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
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<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;
}
波比源码 » POJ 2528 Mayor's posters(离散化线段树)
Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across.Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…