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. ssl协议以及生成

    一.https协议https是一安全为目标的httpt通道,简单讲师http的安全版.即http下加入ssl层,https的安全基础是ssl,因此加密的详细内容就需要ssl.http和https的区别 ...

  2. JAVA多线程---wait() & join()

    题外话: interrupt()方法  并不能中断一个正常运行的线程!!! class myThread extends Thread{ @Override public void run(){ fo ...

  3. Divide Sum 比赛时竟然想不出。。。。。。。

    Divide Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitSt ...

  4. html基础知识笔记

    HTML基础 1.1HTML文件的基本结构和W3C标准 1.1.1HTML简介 HTML是一种描述网页的语言,一种超文本标记的语言! 1.1.2HTML文件的基本结构 头部(head) 头部是网页的标 ...

  5. ZOJ2150 Raising Modulo Numbers 快速幂

    ZOJ2150 快速幂,但是用递归式的好像会栈溢出. #include<cstdio> #include<cstdlib> #include<iostream> # ...

  6. Java面向对象(封装性概论)

     Java面向对象(封装性概论) 知识概要:                   (1)面向对象概念 (2)类与对象的关系 (3)封装 (4)构造函数 (5)this关键字 (6)static关键 ...

  7. 如何获取url上面的参数

    例如 :网页.html?id=0 //获取url中"?"符后的字串 gofunction getRequest() { var url = window.location.sear ...

  8. OpenCV中的绘图函数-OpenCV步步精深

    OpenCV 中的绘图函数 画线 首先要为画的线创造出环境,就要生成一个空的黑底图像 img=np.zeros((512,512,3), np.uint8) 这是黑色的底,我们的画布,我把窗口名叫做i ...

  9. cocos2dx - v2.3.3编辑器简单使用及不同分辨率适配

    准备工具 Cocos 引擎当前最新版本:v2.3.3       官网下载地址: http://www.cocos.com/download/ 前述:cocos自带UI编辑器在最新的版本有了很大的改动 ...

  10. d01

    基础 <head>   <meta http-equiv="Content-Type" content="text/html; charset=utf- ...