题目链接: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. java基础之switch

    switch 语句由一个控制表达式和多个case标签组成. switch 控制表达式支持的类型有byte.short.char.int.enum(Java 5).String(Java 7). swi ...

  2. XTU 二分图和网络流 练习题 J. Drainage Ditches

    J. Drainage Ditches Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Ja ...

  3. Vijos1655 - 萌萌的糖果博弈

    Portal Description 两人轮流操作两堆初始数量分别为\(a,b(a,b\leq2^{127})\)的石子.每人每次进行如下操作: 取走一堆石子,并将另一堆分成两个非零堆. 如果另一堆只 ...

  4. 关于srand()rand()的用法

    转自:http://baike.baidu.com/link?url=bhos65ZKp8lEq_6chSsmQv29jHrqjN_IFGVMNod6BuicQ-3oCP5VsEn3RBjXBPvA7 ...

  5. UVA12103 贪心+置换

    题意:给出26个大写字母的置换B,问是否存在一个置换A,舍得A^2=B,如果存在输出Yes,否则输出No 题解: 研究一下置换A与A^2关系. 假设A=(a1 a2 a3)(b1 b2 b3 b4) ...

  6. CodeForces - 320B Ping-Pong (Easy Version)

    题目最开始 完全不懂 配合案例也看不懂-_- 总之就是用传递性 问能否从a区间到b区间 dfs(x,y) 走遍与第x区间所有的 联通区间 最后检验 第y区是否被访问过 是一道搜索好题 搜索还需加强 # ...

  7. 【HDOJ6301】Distinct Values(贪心,set)

    题意:给定一个n个数的数列与m个区间,要求每个区间内的数字互不相同,求使得数列字典序最小的方案 n<=1e5 思路: #include<cstdio> #include<vec ...

  8. eclispe使用

    eclipse 快捷键 ctrl+shif+o     :去除多余引用 ctrl+shift+x    :转大写 ctrl+shift+y    :转小写 ctrl+o :查找方法 Alt+ ← :回 ...

  9. HDU——2119 Matrix

    Matrix Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  10. poj——1274 The Perfect Stall

    poj——1274   The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25709   A ...