Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 51098   Accepted: 14788

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 li 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 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+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

思路:
很经典的题。。线段树+离散化,难点主要在于需要用特殊的离散化方法,一般的离散化方法会出错
比如下面这串数据
3
1-10
1-4
6-10
一般离散化后就会变成 a[0] = 1;a[1] = 4;a[2] = 6;a[3]= 10;
离散化将把这些数据的下标作为值放进线段树处理后就会成为:
0 - 3 为1颜色
0 - 1 为2颜色
2 - 3 为3颜色
最后只存在两种颜色
但正确的过程应该是:
1 - 10 为1颜色
1 - 4 为2颜色
6 - 10 为3颜色
最后存在三种颜色 实现代码:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 2e5+;
int col[M<<],Hash[M<<],cnt,a[M],l[M],r[M];
void pushdown(int rt){
if(col[rt]!=-){
col[rt<<] = col[rt<<|] = col[rt];
col[rt] = -;
}
} void update(int L,int R,int c,int l,int r,int rt){
if(L <= l&&R >= r){
col[rt] = c;
return ;
}
pushdown(rt);
mid;
if(L <= m) update(L,R,c,lson);
if(R > m) update(L,R,c,rson);
} void query(int l,int r,int rt){
if(col[rt]!=-){
if(Hash[col[rt]]==) cnt++;
Hash[col[rt]] = ;
return ;
}
if(l == r) return;
mid;
query(lson);
query(rson);
} int bin(int key,int n,int a[]){
int l = ,r = n-;
while(l <= r){
mid;
if(a[m] == key) return m;
else if(a[m] < key) l = m+;
else r = m-;
}
return -;
}
int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie();
int t,n;
cin>>t;
while(t--){
cin>>n;
memset(col,-,sizeof(col));
memset(Hash,,sizeof(Hash));
int nn = ;
cnt = ;
for(int i = ;i < n;i++){
cin>>l[i]>>r[i];
a[nn++] = l[i];a[nn++] = r[i];
}
sort(a,a+nn);
int m = ;
for(int i = ;i < nn;i ++){
if(a[i]!=a[i-]) a[m++] = a[i];
}
for(int i = m-;i > ;i --){
if(a[i]!=a[i-]+) a[m++] = a[i] + ;
}
sort(a,a+m);
for(int i = ;i < n;i ++){
int li = bin(l[i],m,a);
int ri = bin(r[i],m,a);
update(li,ri,i,,m,);
}
query(,m,);
cout<<cnt<<endl;
}
}

poj 2528 (线段树+特殊离散化)的更多相关文章

  1. Mayor's posters POJ - 2528 线段树(离散化处理大数?)

    题意:输入t组数据,输入n代表有n块广告牌,按照顺序贴上去,输入左边和右边到达的地方,问贴完以后还有多少块广告牌可以看到(因为有的被完全覆盖了). 思路:很明显就是线段树更改区间,不过这个区间的跨度有 ...

  2. poj 2528(线段树+离散化) 市长的海报

    http://poj.org/problem?id=2528 题目大意是市长竞选要贴海报,给出墙的长度和依次张贴的海报的长度区间(参考题目给的图),问最后你能看见的海报有几张 就是有的先贴的海报可能会 ...

  3. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  4. poj 2528 线段树+离散化

    题意:在墙上贴一堆海报(只看横坐标,可以抽象成一线段),新海报可以覆盖旧海报.求最后能看到多少张海报 sol:线段树成段更新.铺第i张海报的时候更新sg[i].x~sg[i].y这一段为i. 然而坐标 ...

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

    离散化其实就是把所有端点放在一起,然后排序去个重就好了. 比如说去重以后的端点个数为m,那这m个点就构成m-1个小区间.然后给这m-1个小区间编号1~m-1,再用线段树来做就行了. 具体思路是,从最后 ...

  6. poj 2528 线段树 离散化的小技巧

    题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报思路:直接搞超时+超内存,需要离散化.离散化简单的来说就是只取我们需要的值来 用,比如说区间[1000,2000],[1990,2012] ...

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

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

  8. Mayor's posters POJ - 2528 线段树区间覆盖

    //线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...

  9. POJ 2528 线段树

    坑: 这道题的坐标轴跟普通的坐标轴是不一样的-- 此题的坐标轴 标号是在中间的-- 线段树建树的时候就不用[l,mid][mid,r]了(这样是错的) 直接[l,mid][mid+1,r]就OK了 D ...

  10. ACM/ICPC 之 数据结构-线段树+区间离散化(POJ2528)

    这道题用线段树做更方便更新和查询,但是其数据范围很大,因此要将离散化和线段树结合起来,算是一道比较经典的线段树+离散化的例题. 线段树的离散化有很多方法,在这里,我先用一次结点离散化,间接将源左右端点 ...

随机推荐

  1. memcpy、memmove、memset、memchr、memcmp、strstr详解

    第一部分 综述 memcpy.memmove.memset.memchr.memcmp都是C语言中的库函数,在头文件string.h中.memcpy和memmove的作用是拷贝一定长度的内存的内容,m ...

  2. Flask安装教程

    第1步:确保本机已经安装有python,下载easy_install到本地某一目录,双击ez_setup.py,python将自动下载到python安装目录/Scripts 下面,然后在系统环境变量的 ...

  3. 电信3G上网卡自己主动重拨

    须要使用电信3G无线上网卡做一个測试,由于要长时间使用,在使用的过程中老掉线,所以做了一个自己主动重连的批处理,好了,闲话少叙,开工: 先建立一个拨号连接: 001.png watermark/2/t ...

  4. 20155321 《网络攻防》 Exp1 PC平台逆向破解(5)M

    20155321 <网络攻防> Exp1 PC平台逆向破解(5)M 实践目标 本次实践的对象是linux的可执行文件 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...

  5. Octocat,看着喜欢就都下载下来了

    看见github的octocat很喜欢,就用c#写了个程序统统download了,附上一个比较高效的下载程序,以及文末的图片压缩包. 用到了Jumony解析网页. HttpClient client ...

  6. flask 与 vue.js 2.0 实现 todo list

    实现了后端与前端分离,后端提供 RESTful api. 后端 flask 与前端 vue 的数据传输都是 json. 本文使用 vue.js 2.0 对前一个例子:flask, SQLAlchemy ...

  7. Shiro安全框架学习笔记

    一.Shiro框架简单介绍 Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权.Shiro在JavaSE和JavaEE项目中都可以使用.它主要用来处理身份认证,授权,企业会话管理 ...

  8. 微信小程序中跳转另一个小程序

    wx.navigateToMiniProgram({ appId: 'xxxxxxxxxxxxxxxxxx', // 要跳转的小程序的appid path: 'page/index/index', / ...

  9. OPENSTACK重装系统失败导致虚拟机状态为error

    重装系统失败导致虚拟机状态为error DASHBOARD查看虚拟机状态: 查看日志: 磁盘不足导致下载新镜像失败. Virsh list -all 无法发现虚拟机,底层盘消失(因为重装系统时nova ...

  10. win10安装tensorflow-gpu

    1.安装anaconda (最好使用清华源下载) 2.打开cmd conda create -n tensorflow pip python=3.6 activate tensorflow pip i ...