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

POJ2528(离散化+线段树区间更新)的更多相关文章

  1. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  2. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  3. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  4. POJ-2528 Mayor's posters(线段树区间更新+离散化)

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

  5. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  6. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  7. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  8. hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新

    #1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...

  9. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

随机推荐

  1. Android笔记之GridView

    完整Demo链接:https://pan.baidu.com/s/1d_G9aCwBxpiYQcdQhwSDDw,提取码:5deh 效果图 activity_main.xml <?xml ver ...

  2. 我的Android进阶之旅------>如何将Android源码导入Eclipse中来查看(非常实用)

    Android源码下载完成的目录结构如如所示: step1:将.classpath文件拷贝到源代码的根目录 Android源码支持多种IDE,如果是针对APP层做开发的话,建议大家使用Eclipse开 ...

  3. 8.Django模型类例子

    这里定义4个模型 作者:一个作者有姓名 作者详情:包括性别,email,出生日期, 出版商:名称,地址,城市,省,国家,网站 书籍:名称,日期 分析: 作者详情和作者一对一的关系 一本书可以有多个作者 ...

  4. jetty源代码剖析

    近期使用jetty自己写了一个web server,如今闲了花了一天的时间看了一jetty的源代码,主要以server的启动为主线.进行了剖析,经过阅读对jetty的源代码大赞,写的简洁.清晰.架构也 ...

  5. 高性能javascript学习总结(2)--DOM编程

    我们知道,对DOM的操作都是非常的耗性能的,那么为什么会耗性能呢?      文档对象模型(DOM)是一个独立于语言的,使用 XML和 HTML 文档操作的应用程序接口(API).在浏览器中,主要与 ...

  6. hadoop1.2.1 datanode 由于权限无法启动 expected: rwxr-xr-x

    /************************************************************ STARTUP_MSG: Starting DataNode STARTUP ...

  7. 纯js做的select二级联动

    分步阅读 select 联动用到的范围很广,下面介绍一下简单的二级联动 方法/步骤   做一个简单的html页面,用于显示select联动,如图所示:   设置js,点击一级选择项时,创建其下对应的二 ...

  8. Python 3 接口与归一化设计

    一.接口与归一化设计: 1.归一化让使用者无需关心对象的类是什么,只需要知道这些对象都具备某些功能就可以了,这极大地降低了使用者的使用难度. 2.归一化使得高层的外部使用者可以不加区分的处理所有接口兼 ...

  9. jquery的几个语法总结和注意事项

    1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...

  10. sqoop导入增量数据

    使用sqoop导入增量数据. 核心参数 --check-column 用来指定一些列,这些列在增量导入时用来检查这些数据是否作为增量数据进行导入,和关系行数据库中的自增字段及时间戳类似这些被指定的列的 ...