题目链接: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. Oracle の ty_str_split + MySQL の proc_split

    oracle实现字符串分割 功能描述:用指定分隔符切割输入的字符串,返回一维数组,每个数组元素为一个子串. ); CREATE OR REPLACE FUNCTION fn_split (p_str ...

  2. sql无效字符 执行sql语句报错解决方案

    以为是sql中参数赋值有问题,但是将sql语句直接copy到PLSQL中执行,却没问题,纠结了好久,原来是 insert语句多了:唉,坑爹 http://www.jb51.net/article/32 ...

  3. NYOJ-104最大和(动归题)及连续最大和核心

    最大和 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描述 给定一个由整数组成二维矩阵(r*c),现在需要找出它的一个子矩阵,使得这个子矩阵内的所有元素之和最大,并把这个子矩 ...

  4. bzoj3637 CodeChef SPOJ - QTREE6 Query on a tree VI 题解

    题意: 一棵n个节点的树,节点有黑白两种颜色,初始均为白色.两种操作:1.更改一个节点的颜色;2.询问一个节点所处的颜色相同的联通块的大小. 思路: 1.每个节点记录仅考虑其子树时,假设其为黑色时所处 ...

  5. Codeforces474E - Pillars

    Portal Description 给出一个\(n(n\leq10^5)\)的正整数序列\(\{a_n\}(a_i\leq10^{15})\)和正整数\(d(d\leq10^9)\),求\(\{a_ ...

  6. 任务查询系统(bzoj 3932)

    Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si ...

  7. svg学习之旅(3)

    常用标签: <g>标签 是一个容器(分组)标签,用来组合元素的 - 共用属性 - transform = "translate(0,0)"<text>标签 ...

  8. java打开本地应用程序(调用cmd)---Runtime用法详解

    有时候我们需要借助java程序打开电脑自带的一些程序,可以直接打开或者借助cmd命令窗口打开一些常用的应用程序或者脚本,在cmd窗口执行的命令都可以通过这种方式运行. 例如: package cn.x ...

  9. iOS FMDB 无法更新二进制数据的问题

    使用FMDB很方便的实现了(通过数据库字段名而不是字段索引)数据的读取,插入,更新,删除.但是我在更新图片时发现通过格式化字符(@“%@”,data/NSData/)传入的二进制数据更新到数据库后不能 ...

  10. zookeeper一二三

    1.zookeeper介绍 ZooKeeper 是一个开源的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现.分布式应用程序可以基于 ZooKeeper 实现诸如数据发布/订阅. ...