题目链接:https://vjudge.net/problem/POJ-2528

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

题解:

1.经典的区间染色问题,可利用线段树的区间修改进行维护。

2.由于区间的范围很大,1e7。但是输入的数据最多只有2e4个,所有需要进行离散化。

3.那是否意味着只需要对输入的数据进行离散呢?

答:不是的。例如一组数据只有三张post:[1,3] 和 [6,10] 和 [1,10],实际答案为3张。如果只对上述的数字进行离散化,则变成:[1,2] 和 [3,4] 和 [1, 4],则答案就变成2张了。为什么会出现这种现象?原因是中间那一段区域[4,5]被忽略掉了。所以,如果两个相邻的数据的差值大于1,则需要对他们之间的区域也进行离散化。

注:根据题目意思,每个数字都代表着一个区域,而不是一个点。再加上没有出现的数字,某些连续的数字有代表着一个区域。所以这题离散的本质对象就是一段段区域,且这些区域是连续的。

数组离散(手写二分):

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; //叶子结点最多有4e4个
int color[MAXN<<];
int le[MAXN], ri[MAXN];
int tmp[MAXN<<], M[MAXN<<], visible[MAXN]; void push_down(int u, int l, int r)
{
if(color[u]!=)
{
color[u*] = color[u*+] = color[u];
color[u] = ;
}
} void set_val(int u, int l, int r, int x, int y, int val)
{
if(x<=l && r<=y)
{
color[u] = val;
return;
} push_down(u, l, r);
int mid = (l+r)/;
if(x<=mid) set_val(u*, l, mid, x, y, val);
if(y>=mid+) set_val(u*+, mid+, r, x, y, val);
} void query(int u, int l, int r)
{
if(l==r)
{
visible[color[u]] = ;
return;
} push_down(u, l, r);
int mid = (l+r)/;
query(u*, l, mid);
query(u*+, mid+, r);
} int binsearch(int x, int m)
{
int l = , r = m;
while(l<=r)
{
int mid = (l+r)/;
if(M[mid]<=x) l = mid+;
else r = mid-;
}
return r;
} int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
{
scanf("%d%d", &le[i], &ri[i]);
tmp[i*-] = le[i];
tmp[i*] = ri[i];
} int m = ;
sort(tmp+, tmp++*n);
for(int i = ; i<=*n; i++)
{
if(i!= && tmp[i]-tmp[i-]>) M[++m] = tmp[i]-;
if(i== || tmp[i]!=tmp[i-]) M[++m] = tmp[i];
} memset(color, , sizeof(color));
for(int i = ; i<=n; i++)
{
int l = binsearch(le[i], m);
int r = binsearch(ri[i], m);
set_val(, , m, l, r, i);
} memset(visible, , sizeof(visible));
query(, , m);
int ans = ;
for(int i = ; i<=n; i++)
if(visible[i]) ans++; printf("%d\n", ans);
}
}

map离散(超时):

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; int val[MAXN*];
int le[MAXN], ri[MAXN];
int tmp[MAXN*], visible[MAXN];
map<int, int>M; void push_down(int u, int l, int r)
{
if(val[u]!=)
{
val[u*] = val[u*+] = val[u];
val[u] = ;
}
} void set_val(int u, int l, int r, int x, int y, int v)
{
if(x<=l && r<=y)
{
val[u] = v;
return;
} push_down(u, l, r);
int mid = (l+r)/;
if(x<=mid) set_val(u*, l, mid, x, y, v);
if(y>=mid+) set_val(u*+, mid+, r, x, y, v);
} void query(int u, int l, int r)
{
if(l==r)
{
visible[val[u]] = ;
return;
} push_down(u, l, r);
int mid = (l+r)/;
query(u*, l, mid);
query(u*+, mid+, r);
} int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
{
scanf("%d%d", &le[i], &ri[i]);
tmp[i*-] = le[i];
tmp[i*] = ri[i];
} int m = ;
sort(tmp+, tmp++*n);
M.clear();
for(int i = ; i<=*n; i++)
{
if(i!= && tmp[i]-tmp[i-]>) M[tmp[i]-] = ++m;
if(i== || tmp[i]!=tmp[i-]) M[tmp[i]] = ++m;
} memset(val, false, sizeof(val));
for(int i = ; i<=n; i++)
set_val(, , m, M[le[i]], M[ri[i]], i); memset(visible, false, sizeof(visible));
query(, , m);
int ans = ;
for(int i = ; i<=n; i++)
if(visible[i]) ans++; printf("%d\n", ans);
}
}

POJ2528 Mayor's posters —— 线段树染色 + 离散化的更多相关文章

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

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

  2. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

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

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

  4. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

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

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

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

  6. poj2528 Mayor's posters(线段树区间覆盖)

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

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

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

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

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

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

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

随机推荐

  1. centos挂载本地镜像作为yum源

    1.安装Centos后默认的Yum源如下 ll /etc/yum.repos.d/   [root@localhost ~]# ll /etc/yum.repos.d/ total 32 -rw-r- ...

  2. 一张图表,人人都能建立自己的AARRR运营模型

    每次跟同行聊运营,聊用户,聊产品,最后都会回到AARRR模型上来,这个用户全生命周期模型概括了互联网产品运营的5个关键环节. 获客是运营的基础,促进用户活跃才能让产品有生命力,提升留存减少流失让用户规 ...

  3. jmeter给cookie设置sessionId避免其他脚本多次登录

    1.相关知识: http头部可以设置:浏览器显示内容类型,如content-type:text/html http头部可以存放:浏览器的cookie信息——cookie是对用户身份进行判断的内容 ht ...

  4. Oracle常用内置数据表查询

    Oracle 查询库中所有表名.字段名.字段名说明,查询表的数据条数.表名.中文表名. 查询所有表名:select t.table_name from user_tables t;查询所有字段名:se ...

  5. java中filter的用法

    filter过滤器主要使用于前台向后台传递数据是的过滤操作.程度很简单就不说明了,直接给几个已经写好的代码: 一.使浏览器不缓存页面的过滤器 Java代码   import javax.servlet ...

  6. spring几种依赖注入方式以及ref-local/bean,factory-bean,factory-method区别联系

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  7. middle(bzoj 2653)

    Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...

  8. Codevs 2666 2666 Accept Ratio

    时间限制: 1 s  空间限制: 32000 KB   题目等级 : 钻石 Diamond 题目描述 Description 某陈痴迷于pku的ACM题库,常常彻夜奋斗刷题.他最近的目标是在NOIP0 ...

  9. Mongodb主、副、仲裁节点集群安装

    mongodb 的集群方式主要分为三种Replica Set / Sharding / Master-Slaver ,这里只说明最简单的集群搭建方式(生产环境),如果有多个节点可以此类推或者查看官方文 ...

  10. linux命令2——进程相关

    (1)ps  -ef :可以看到内核的线程.