题目链接

题意

用不同颜色的线段覆盖数轴,问最终数轴上有多少种颜色?

注:只有最上面的线段能够被看到;即,如果有一条线段被其他的线段给完全覆盖住,则这个颜色是看不到的。

法一:线段树

按题意按顺序模拟即可。

法二:线段树+离线

将整个过程倒过来看待,如果要加进去的线段所在的区域已经完全被覆盖,那么这条线段就没有贡献,否则就有\(1\)的贡献。

法三:并查集+离线

离线处理思想同上。

用\(fa[\ ]\)数组记录某个元素左边距其最近的没有被覆盖的点的坐标。那么对于当前覆盖的线段\([l,r]\),只要\(find(r)\lt l\),就意味着当前这一段已经完全被覆盖,所以贡献为\(0\).

并查集的思想类似BZOJ 3211 花神游历各国

注意点

这道题的坑点在于:离散化

如果普通地进行离散化,考虑

3
1 10
1 4
6 10

会被离散化成

3
1 4
1 2
3 4

本来有三种颜色,处理过之后就只有两种颜色了。

问题在于:顺序的连续并不代表位置的连续。

处理时在中间插入额外的点即可。

Code

Ver. 1

#include <stdio.h>
#include <algorithm>
#include <string.h>
#define lson rt<<1
#define rson rt<<1|1
#define maxn 100010
using namespace std;
typedef long long LL;
int a[maxn], l[maxn], r[maxn], b[maxn], ans;
bool vis[maxn];
struct tree { int l, r, c, flag; }tr[maxn*4];
void build(int rt, int l, int r) {
tr[rt].l = l, tr[rt].r = r; tr[rt].flag = tr[rt].c = 0;
if (l==r) return;
int mid = l+r >> 1;
build(lson, l, mid), build(rson, mid+1, r);
}
void push_down(int rt) {
if (tr[rt].flag) {
tr[lson].flag = tr[rson].flag
= tr[lson].c = tr[rson].c = tr[rt].flag;
tr[rt].flag = 0;
}
}
void push_up(int rt) {
tr[rt].c = tr[lson].c == tr[rson].c ? tr[lson].c : 0;
}
void modify(int rt, int l, int r, int c) {
if (tr[rt].l == l && tr[rt].r == r) {
tr[rt].c = tr[rt].flag = c;
return;
}
push_down(rt);
int mid = tr[rt].l + tr[rt].r >> 1;
if (r <= mid) modify(lson, l, r, c);
else if (l > mid) modify(rson, l, r, c);
else modify(lson, l, mid, c), modify(rson, mid+1, r, c);
push_up(rt);
}
void query(int rt) {
if (tr[rt].l == tr[rt].r || tr[rt].c) {
if (!vis[tr[rt].c] && tr[rt].c) ++ans, vis[tr[rt].c] = true;
return;
}
push_down(rt);
query(lson); query(rson);
}
void work() {
int n;
scanf("%d", &n);
int tot=0;
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &l[i], &r[i]);
a[++tot] = l[i], a[++tot] = r[i];
}
sort(a+1,a+tot+1);
tot = unique(a+1,a+tot+1)-a-1; b[1] = a[1]; int cnt = 1;
for (int i = 2; i <= tot; ++i) {
if (a[i]-a[i-1] == 1) b[++cnt] = a[i];
else b[++cnt] = a[i-1] + 1, b[++cnt] = a[i];
} build(1,1,cnt);
for (int i = 1; i <= n; ++i) {
int pl = lower_bound(b+1, b+1+cnt, l[i]) - b,
pr = lower_bound(b+1, b+1+cnt, r[i]) - b;
modify(1, pl, pr, i);
} ans = 0; memset(vis, 0, sizeof(vis));
query(1);
printf("%d\n", ans);
}
int main() {
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}

Ver. 2

#include <stdio.h>
#include <algorithm>
#include <map>
#define lson rt<<1
#define rson rt<<1|1
#define maxn 100010
using namespace std;
typedef long long LL;
int a[maxn], l[maxn], r[maxn], b[maxn];
struct tree { int l, r; bool cov, flag; }tr[maxn*4];
map<int,int> mp;
void build(int rt, int l, int r) {
tr[rt].l = l, tr[rt].r = r; tr[rt].cov = 0; tr[rt].flag = 0;
if (l==r) return;
int mid = l+r >> 1;
build(lson, l, mid), build(rson, mid+1, r);
}
void push_down(int rt) {
if (tr[rt].flag) {
tr[lson].cov = tr[rson].cov = tr[lson].flag = tr[rson].flag = 1;
tr[rt].flag = 0;
}
}
void push_up(int rt) {
tr[rt].cov = tr[lson].cov && tr[rson].cov;
}
bool insert(int rt, int l, int r) {
if (tr[rt].l==l && tr[rt].r==r) {
if (tr[rt].cov) return true;
tr[rt].cov = tr[rt].flag = true;
return false;
}
push_down(rt);
int mid = tr[rt].l + tr[rt].r >> 1;
bool ret;
if (r <= mid) ret = insert(lson, l, r);
else if (l > mid) ret = insert(rson, l, r);
else {
// 注意!这里不能直接写成 ret = insert(lson, l, mid) & ans2 = insert(rson, mid+1, r);
// 因为根据短路原理,如果前面的为假,后面的就直接不做了!
bool ans1 = insert(lson, l, mid), ans2 = insert(rson, mid+1, r);
ret = ans1&ans2;
}
push_up(rt);
return ret;
}
void work() {
int n;
scanf("%d", &n);
int tot=0;
for (int i = 0; i < n; ++i) {
scanf("%d%d", &l[i], &r[i]);
a[++tot] = l[i], a[++tot] = r[i];
}
sort(a+1,a+tot+1);
tot = unique(a+1,a+tot+1)-a-1; b[1] = a[1]; int cnt = 1;
for (int i = 2; i <= tot; ++i) {
if (a[i]-a[i-1] == 1) b[++cnt] = a[i];
else b[++cnt] = a[i-1] + 1, b[++cnt] = a[i];
} build(1,1,cnt);
int ans=0;
for (int i = n-1; i >= 0; --i) {
int pl = lower_bound(b+1, b+1+cnt, l[i]) - b,
pr = lower_bound(b+1, b+1+cnt, r[i]) - b;
if (!insert(1, pl, pr)) ++ans;
}
printf("%d\n", ans);
}
int main() {
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}

Ver. 3

#include <stdio.h>
#include <algorithm>
#include <map>
#define maxn 100010
using namespace std;
typedef long long LL;
int a[maxn], l[maxn], r[maxn], fa[maxn], b[maxn];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
map<int,int> mp;
void work() {
int n;
scanf("%d", &n);
int tot=0;
for (int i = 0; i < n; ++i) {
scanf("%d%d", &l[i], &r[i]);
a[++tot] = l[i], a[++tot] = r[i];
}
sort(a+1,a+tot+1);
tot = unique(a+1,a+tot+1)-a-1; b[1] = a[1]; int cnt = 1;
for (int i = 2; i <= tot; ++i) {
if (a[i]-a[i-1] == 1) b[++cnt] = a[i];
else b[++cnt] = a[i-1] + 1, b[++cnt] = a[i];
}
for (int i=0; i<=cnt; ++i) fa[i] = i; int ans=0;
for (int i = n-1; i >= 0; --i) {
int pl = lower_bound(b+1, b+1+cnt, l[i]) - b,
pr = lower_bound(b+1, b+1+cnt, r[i]) - b;
if (find(pr)<pl) continue;
++ans;
for (int j = find(pr); j >= pl; j = find(j-1)) {
fa[find(j)] = find(pl-1);
}
}
printf("%d\n", ans);
}
int main() {
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}

poj 2528 Mayor's posters 线段树 || 并查集 离线处理的更多相关文章

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

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

  2. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  3. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

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

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

  5. poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  6. POJ 2528 Mayor's posters (线段树)

    题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...

  7. poj 2528 Mayor's posters(线段树)

    题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...

  8. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

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

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

随机推荐

  1. GoF23种设计模式之创建型模式之单态模式

    1概述 保证一个类仅有一个实例,并提供一个访问它的全局访问点. 2适用性 1.当类只能有一个实例而且客户可以从一个总所周知的访问点访问它的时候. 2.当这个唯一实例应该是通过子类化可扩展的,并且客户应 ...

  2. stm32 flash和sram

    FLASH是用来存储程序的,SRAM是用来存储程序运行中的中间变量

  3. dfs 的全排列

    #include <iostream> #include <algorithm> #include <cstdio> #include <string> ...

  4. redis配置密码 redis常用命令

    redis配置密码 1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 [plain] view plain copy   #requi ...

  5. #2 create and populate a database && realistic and practical applications (PART 2)

    Extends from the last chapter , This chapter takes a look at some real-world problems that can occur ...

  6. Logistic回归python实现小样例

    假设现在有一些点,我们用一条直线对这些点进行拟合(该线称为最佳拟合直线),这个拟合过程就称作回归.利用Logistic回归进行分类的主要思想是:根据现有数据对分类边界线建立回归公式,依次进行分类.Lo ...

  7. cakephp中使用 find('count')方法

    对于find('count',array('group'=>'user_id')); Model.php中这样描述: /** * Handles the before/after filter ...

  8. loj2090 「ZJOI2016」旅行者

    分治+最短路,很套路的 #include <algorithm> #include <iostream> #include <cstring> #include & ...

  9. MySQL一对一:一对多:多对多

    学生表和课程表可以多对多 一个学生可以学多门课程 一门课程可以有多个学生: 多对多 *** 一个学生对应一个班级 一个班级对应多个学生: 一对多 *** 一个老师对应多个学生 多个学生对应一个老师:一 ...

  10. 精通CSS高级Web标准解决方案(4、对链接应用样式)

    4.1 简单的链接样式 锚可以作为内部引用,也可以作为外部链接,应该区分对待. 伪类选择器: :link 用来寻找没有访问过的链接 :visited 用来寻找已经访问过的链接 a:link{color ...