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. 中国移动飞信WAP登陆分析及脚本

    中国移动飞信WAP网页版 http://f.10086.cn/im5/ 用WAP飞信登录并向好友发送信息,同时用wireshark抓包. 1.过滤POST表单提交数据包(wireshark规则: ht ...

  2. [js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形)

    绘制曲线,经常会用到路径的知识,如果你对路径有疑问,可以参考我的这篇文章[js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解. arc:画 ...

  3. Linux 环境下java安装及配置

    操作系统环境: Red Hat Enterpriser  Linux 6.5 jdk版本:  jdk1.8.0_144 1 从官网下载Linux操作系统对应的jdk版本文件 2 安装jdk 3 安装完 ...

  4. 使用node.js检查js语法错误

    如果没有一些工具和插件写JavaScript代码遇到语法错误找起来很费时间,请教了同事怎么用node.js检查 用浏览器测试的时候报语法错误. 1.点击红圈中的蓝色按钮,下次刷新是会在抛出异常的时候自 ...

  5. 安装 node-sass 时报错

    在安装 node-sass 时报错,截图如下 解决方法如下: npm install --save node-sass --registry=https://registry.npm.taobao.o ...

  6. Nginx学习——Nginx简单介绍和Linux环境下的安装

    一:Nginx的简介 百科百科:Nginx Nginx 是一个俄罗斯的哥们开发的,并将其进行了开源. Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器, ...

  7. input中range相关操作

    利用mousover触发函数对range的操作练习 <!DOCTYPE html> <html> <head> <meta charset="utf ...

  8. 【京东个人中心】——Nodejs/Ajax/HTML5/Mysql爬坑之注册与登录监听

    一.引言 在数据库和静态页面都创建好之后,下面就该接着完成后台Node.js监听注册和登录的部分了.这个部分主要使用的技术是:Node.js的Express框架和ajax异步请求.登录和注册的代码实现 ...

  9. js X年X周 转成 具体日期

    function getWeekDate(theyear,weekcount) { var year = theyear; var week = weekcount; if(year=="& ...

  10. MVC使用jQuery从视图向控制器传递Model,数据验证,MVC HTML辅助方法小结

    //MVC HTML辅助类常用方法记录 (1)@Html.DisplayNameFor(model => model.Title)是显示列名, (2)@Html.DisplayFor(model ...