Mayor's posters

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u


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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=;
struct Node{
int color;//0表示没有贴海报,-1表示贴有多个海报
int l,r;
}a[MAXN*];
struct Query{
int l,r;
}qre[MAXN];
int n;
int hax[MAXN],cnt;
int vis[MAXN],res;
void pushUp(int rt)
{
if(a[rt<<].color==a[(rt<<)|].color)
{
a[rt].color=a[rt<<].color;
}
else
{
a[rt].color=-;
}
}
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
if(l==r)
{
a[rt].color=;
return ;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
pushUp(rt);
}
void pushDown(int rt)
{
a[rt<<].color=a[rt].color;
a[(rt<<)|].color=a[rt].color;
}
void update(int rt,int l,int r,int val)
{
if(a[rt].l==l&&a[rt].r==r)
{
a[rt].color=val;
return ;
}
if(a[rt].color!=&&a[rt].color!=-)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
{
update(rt<<,l,r,val);
}
else if(mid<l)
{
update((rt<<)|,l,r,val);
}
else
{
update(rt<<,l,mid,val);
update((rt<<)|,mid+,r,val);
}
pushUp(rt);
}
void query(int rt,int l,int r)
{
if(a[rt].color==)
{
return ;
}
if(a[rt].l==l&&a[rt].r==r)
{
if(a[rt].color!=-)
{
if(!vis[a[rt].color])
{
vis[a[rt].color]=;
res++;
}
return ;
}
}
if(a[rt].color!=&&a[rt].color!=-)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
query(rt<<,l,mid);
query((rt<<)|,mid+,r);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
cnt=;
res=;
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&qre[i].l,&qre[i].r);
hax[cnt++]=qre[i].l;
hax[cnt++]=qre[i].r;
}
sort(hax,hax+cnt);
cnt=unique(hax,hax+cnt)-hax;
build(,,cnt);
int col=;
for(int i=;i<n;i++)
{
int l=lower_bound(hax,hax+cnt,qre[i].l)-hax+;
int r=lower_bound(hax,hax+cnt,qre[i].r)-hax+;//需要+1
update(,l,r,col);
col++;
}
query(,,cnt);
printf("%d\n",res);
}
return ;
}

最新文章

  1. ajaxform使用
  2. 【linux】jdk安装
  3. gulp-nodemon 和 gulp-livereload 配置
  4. CentOS6设置密码过期时间
  5. 09day2
  6. UVA 11426 GCD - Extreme (II) 欧拉函数
  7. 深入了解当前ETL中用到的一些基本技术
  8. bzoj1588 [HNOI2002]营业额统计(Treap)
  9. 验证整数 Double 转 int 两种写法
  10. Finally-操作返回值
  11. ROS(indigo)机器人操作系统学习有趣丰富的Gazebo仿真示例evarobot
  12. html5标签基础
  13. netty的编解码器理解(转)
  14. 配置MySQL的数据源
  15. 【转】Oracle之索引
  16. springboot启动的时候日志缺少Mapping日志等
  17. lightoj 1282 取对数的操作
  18. BZOJ1069 SCOI2007最大土地面积(凸包+旋转卡壳)
  19. Android studio的主题颜色修改
  20. centos5 升级到centos6

热门文章

  1. Netty ByteBuf(图解之 2)| 秒懂
  2. Frobenius inner product
  3. jqweui tabbar使用示例
  4. 基本jquery
  5. CSS知识点 2
  6. _CRT_SECURE_NO_WARNINGS
  7. shell 字符串操作
  8. JsonSchema使用详解
  9. unigui中TUniDBEdit的OnEndDrag问题
  10. vs2015配置boost c++