题目链接: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. jQuery.data() 的实现方式

    jQuery.data() 的作用是为普通对象或 DOM Element 附加(及获取)数据. 下面将分三个部分分析其实现方式:     1. 用name和value为对象附加数据:即传入三个参数,第 ...

  2. 自动化项目配置或用例文件格式推荐--yaml

    关于yaml YAML语言的设计目标,就是方便人类读写.如果你想要实现一些用ini不好做到的配置,可以使用yaml格式作为配置文件 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使 ...

  3. wps左侧显示目录

    单击视图----文档结构图,在下拉选项中选择靠左即可,如图所示

  4. 数据库服务器的监控 赛门铁克 Veritas i3 APM 查找指定时间段最耗服务器资源的TopSQL

  5. python006 Python3 运算符

    Python3 运算符什么是运算符?本章节主要说明Python的运算符.举个简单的例子 4 +5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符.Python语言 ...

  6. 【链表】2017多校训练三 HDU 6058 Kanade's sum

    acm.hdu.edu.cn/showproblem.php?pid=6058 [题意] 给定一个排列,计算 [思路] 计算排列A中每个数的贡献,即对于每个ai,计算有ni个区间满足ai是区间中的第k ...

  7. 洛谷P2057 善意的投票

    题目描述 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来 ...

  8. hdu 4045 Machine scheduling [ dp + 斯特林数]

    传送门 Machine scheduling Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. python学习之-- importlib模块

    importlib 模块 Python提供了importlib包作为标准库的一部分.目的就是提供Python中import语句的实现(以及__import__函数).另外,importlib允许程序员 ...

  10. PAT (Advanced Level) 1037. Magic Coupon (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...