题目:
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
题意:就是给咱们一面强 ,高度固定,然后会有n个人的海报会贴在上边,给出每个海报的始末位置,问最后咱们能够看出来是有几张海报(只要能看到数目就行,不要求看全)
解题思路:首先,需要离散化一下,因为数据不小,会T ,然后记录每个区间的id值
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define Maxn 16010000
#define maxn 10008
#define inf 0x3f3f3f3f
using namespace std;
struct node{
int lt;
int rt;
}ss[maxn];
int ans;
int vis[maxn];
int lis[maxn*2];
int ha[Maxn];
struct post{
int lt,rt;
int id;//记录颜色种类
int type;//向下缓存,lazy
}poster[maxn<<3];
void build(int l,int r,int cur)
{
poster[cur].lt=l;
poster[cur].rt=r;
poster[cur].id=0;
poster[cur].type=0;
if(l==r) return ;
int m=(poster[cur].lt+poster[cur].rt)>>1;
build(l,m,cur<<1);
build(m+1,r,cur<<1|1);
}
void updata(int lt,int rt,int cur,int id)
{
if(poster[cur].lt>=lt&&poster[cur].rt<=rt)
{
poster[cur].id=id;
poster[cur].type=id;
return ;//恰好在覆盖范围内
}
if(poster[cur].type&&poster[cur].lt!=poster[cur].rt)
{
poster[cur<<1].type=poster[cur<<1|1].type=poster[cur].type;
poster[cur<<1].id=poster[cur<<1|1].id=poster[cur].id;
poster[cur].type=0;
}//向下压缩,lazy
int mid=(poster[cur].lt+poster[cur].rt)>>1;
if(mid>=lt)
updata(lt,rt,cur<<1,id);
if(mid<rt)
updata(lt,rt,cur<<1|1,id);//向下分支
if(poster[cur].lt!=poster[cur].rt)
{
if(poster[cur<<1].id==poster[cur<<1|1].id)
poster[cur].id=poster[cur<<1].id;
else
poster[cur].id=0;//由下向上分析是否父区间颜色统一
}
}
void query(int lt,int rt,int cur)
{
if(poster[cur].lt<lt||poster[cur].rt>rt)
return ;
if(poster[cur].id)
{
if(!vis[poster[cur].id])
{
ans++;
vis[poster[cur].id]=1;
}
return;
}
if(poster[cur].lt!=poster[cur].rt)
{
int mid=(poster[cur].lt+poster[cur].rt)>>1;
query(lt,mid,cur<<1);
query(mid+1,rt,cur<<1|1);
}
}
int main()
{
int cas,n;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
int k=0;
memset(ha,0,sizeof(ha));
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
scanf("%d%d",&ss[i].lt,&ss[i].rt);
lis[k++]=ss[i].lt;
lis[k++]=ss[i].rt;
}
sort(lis,lis+k);
int j=0;
for(int i=0;i<k;i++)
{
if(ha[lis[i]]==0)
ha[lis[i]]=++j;
}//离散化
build(1,j,1);
for(int i=0;i<n;i++)
{
updata(ha[ss[i].lt],ha[ss[i].rt],1,i+1);
}
ans=0;
query(1,j,1);
printf("%d\n",ans);
}
return 0;
}