Mayor's posters

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

Submit Status

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
/*
题意:给横坐标轴染色,每次给一个区间内的染色,每染一次会把上一次的覆盖,经过n次染色只会,问你从最上面看,横坐标上总共有几种颜色 初步思路:区间染色问题,就是一个区间set问题,然后最后的查询的时候,记录一下就行了 #超内存:需要离散化
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n,l[],r[];
int t;
bool color[];//用于记录颜色
int res=;
int X[];
int k;
int kk;
/****************************线段树基础模板*********************************/ const int maxn=+;
#define lson i*2, l, m
#define rson i*2+1, m+1, r struct Segtree{ int setv[maxn<<]; void PushDown(int i)
{
if(setv[i]>){
setv[i*]=setv[i*+]=setv[i];
setv[i]=-;//向下更新完了就没有继续用的意义了
}
} void build(int i,int l,int r)
{
// cout<<l<<" "<<r<<" "<<i<<endl;
setv[i]=;
if(l==r)
return ;
int m=(l+r)>>;
build(lson);
build(rson);
}
void query(int ql,int qr,int i,int l,int r)
{
if(ql<=l&&r<=qr){
if(setv[i]>){
color[setv[i]]=true;
return ;
}
}
if(l==r) return ;
PushDown(i);
int m=(l+r)>>;
if(ql<=m) query(ql,qr,lson);
if(m<qr) query(ql,qr,rson);
} void update(int ql,int qr,int val,int i,int l,int r)
{
// cout<<l<<" "<<r<<endl;
if(ql<=l&&r<=qr)
{
setv[i]=val;
return ;
}
PushDown(i);
int m=(l+r)>>;
if(ql<=m) update(ql,qr,val,lson);
if(m<qr) update(ql,qr,val,rson);
}
};
Segtree segtree;
/****************************线段树基础模板*********************************/
void init(){
memset(color,false,sizeof color);
res=;
k=;
kk=;
}
int findx(int key){
int l=,r=k,mid;
while(l<=r){
mid=(l+r)>>;
if(X[mid]==key)
return mid;
else if(X[mid]>key)
r=mid-;
else l=mid+;
}
return ;
}
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&l[i],&r[i]);
X[kk++]=l[i];
X[kk++]=r[i];
}
sort(X+,X+kk+);
for(int i=;i<kk;i++){//离散化
if(X[i]!=X[i-])
X[++k]=X[i];
}
segtree.build(,,k);
for(int i=;i<=n;i++){
int L=findx(l[i]);
int R=findx(r[i]);
segtree.update(L,R,i,,,k);
}
segtree.query(,k,,,k);
for(int i=;i<=n;i++){
if(color[i]) res++;
}
printf("%d\n",res);
}
return ;
}
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4
 

Mayor's posters的更多相关文章

  1. POJ 2528 Mayor's posters

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

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

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  3. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

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

    题目链接: 传送门 Mayor's posters Time Limit: 1000MS     Memory Limit: 65536K Description The citizens of By ...

  5. Mayor's posters(线段树+离散化POJ2528)

    Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...

  6. poj2528 Mayor's posters(线段树之成段更新)

    Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 37346Accepted: 10864 Descr ...

  7. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 ...

  8. poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 43507   Accepted: 12693 ...

  9. Poj 2528 Mayor's posters 分类: Brush Mode 2014-07-23 09:12 84人阅读 评论(0) 收藏

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40570   Accepted: 11798 ...

  10. POJ 2528 Mayor’s posters

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37982   Accepted: 11030 ...

随机推荐

  1. 微信开发(2)–获取access_token

    一.access_token access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token.开发者需要进行妥善保存.access_token的存储至少要保 ...

  2. Data_Struct(LinkList)

    最近在学数据结构,学到链表这节作业有链表,毕竟菜鸟代码基本照看书上算法写的,再加上自己的小修改,这里先记录下来,万一哪天想看了,来看看. 里面有用到二级指针,还是不太理解,还有就是注释不多,后续有了更 ...

  3. AngularJS指南文档

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 核心概念 模板 在Angular应用当中,我们的工作就是将服务器的数据填充到客户端页面模 ...

  4. C++移动构造函数以及move语句简单介绍

    C++移动构造函数以及move语句简单介绍 首先看一个小例子: #include <iostream> #include <cstring> #include <cstd ...

  5. JavaScript 实现发布消息后,距离当前时间的实现

    某条消息发布后,距离当前时间多久的时间显示 //显示发布时间的函数 function pastTime(_createTime) { //var createTime = _createTime.su ...

  6. Java高新技术 Myeclipse 介绍

      Java高新技术   Myeclipse 介绍 知识概述:              (1)Myeclipse开发工具介绍 (2)Myeclipse常用开发步骤详解               ...

  7. javascript集合的交,并,补,子集,长度,新增,删除,清空等操作

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...

  8. [#1] YCbCr与RGB的转换公式

    1 YCbCr简介 YCbCr颜色空间是将RGB颜色空间进行坐标转换后得到的,常用于数字电视系统.Y取值范围:16~235 Cb.Cr的取值范围:16~240 YCbCr经常和YUV混淆.两者的主要差 ...

  9. CMake安装(源码方式)

    CMake主页是 https://cmake.org/download/ 一.不指定安装目录方式(不需要配置环境变量) 1.安装必备包(存在的包不用卸载,yum会自动更新) yum install - ...

  10. YYHS-NOIP模拟赛-mine

    题解 这道题不难想到用dp来做 dp[i][0]表示第i个格子放0 dp[i][1]表示第i个格子放1且第i-1个格子放雷 dp[i][2]表示第i个格子放2 dp[i][3]表示第i个格子放1且第i ...