题目:

BZOJ1483

分析:

(这题码了一下午,码了近250行,但是意外跑的比本校各位神仙稍快,特写博客纪念)

首先能看出一个显然的结论:颜色段数只会变少不会变多。

我们考虑用并查集维护区间,对于每个区间维护它的起点和终点。建\(n\)棵平衡树,第\(i\)棵存颜色为\(i\)的区间。把\(x\)变成\(y\)时进行启发式合并,同时对于\(x\)上的每个结点\([a,b]\),在\(y\)中找\(a-1\)和\(b+1\)所在区间。如果存在则合并,并答案减\(1\);若不存在则向\(y\)中插入新结点。时间复杂度\(O(m\log ^2n)\)

代码:

代码不难写,就是长……

#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; namespace zyt
{
template<typename T>
inline void read(T &x)
{
char c;
bool f = false;
x = 0;
do
c = getchar();
while (c != '-' && !isdigit(c));
if (c == '-')
f = true, c = getchar();
do
x = x * 10 + c - '0', c = getchar();
while (isdigit(c));
if (f)
x = -x;
}
template<typename T>
inline void write(T x)
{
static char buf[20];
char *pos = buf;
if (x < 0)
putchar('-'), x = -x;
do
*pos++ = x % 10 + '0';
while (x /= 10);
while (pos > buf)
putchar(*--pos);
}
const int N = 1e6 + 10;
int n, m, p[N], st[N], ed[N];
int f(const int x)
{
return x == p[x] ? x : p[x] = f(p[x]);
}
int ans = 0;
class Splay
{
private:
struct node
{
int val, size;
node *fa, *s[2];
node(const int _val, node *_fa)
: val(_val), fa(_fa)
{
size = 1;
s[0] = s[1] = NULL;
}
}*head;
bool dir(const node *rot)
{
return rot == rot->fa->s[1];
}
void update(node *rot)
{
rot->size = 1;
if (rot->s[0])
rot->size += rot->s[0]->size;
if (rot->s[1])
rot->size += rot->s[1]->size;
}
void rotate(node *rot)
{
node *f = rot->fa, *ff = f->fa;
bool d = dir(rot);
rot->fa = ff;
if (ff)
ff->s[dir(f)] = rot;
else
head = rot;
f->s[d] = rot->s[!d];
if (rot->s[!d])
rot->s[!d]->fa = f;
rot->s[!d] = f;
f->fa = rot;
update(f);
}
void splay(node *rot, const node *goal = NULL)
{
while (rot && rot->fa && rot->fa != goal)
{
node *f = rot->fa, *ff = f->fa;
if (ff == goal)
rotate(rot);
else if (dir(rot) ^ dir(f))
rotate(rot), rotate(rot);
else
rotate(f), rotate(rot);
}
update(rot);
}
void del(node *rot, Splay &s)
{
if (!rot)
return;
int x = f(rot->val);
bool flag = false;
if (st[x] > 1 && s.find(f(st[x] - 1)))
{
p[x] = f(st[x] - 1);
ed[f(st[x] - 1)] = ed[x];
--ans, flag = true;
}
x = f(x);
if (ed[x] < n && s.find(f(ed[x] + 1)))
{
p[x] = f(ed[x] + 1);
st[f(ed[x] + 1)] = st[x];
--ans, flag = true;
}
if (!flag)
s.insert(rot->val);
if (rot->s[0])
del(rot->s[0], s);
if (rot->s[1])
del(rot->s[1], s);
delete rot;
}
node *find(const int val)
{
node *rot = head;
while (1)
{
if (!rot || rot->val == val)
return rot;
if (val < rot->val)
rot = rot->s[0];
else
rot = rot->s[1];
}
}
public:
void insert(const int val)
{
if (!head)
{
head = new node(val, NULL);
return;
}
node *rot = head;
while (1)
{
if (val < rot->val)
{
if (rot->s[0])
rot = rot->s[0];
else
{
rot->s[0] = new node(val, rot);
splay(rot->s[0]);
break;
}
}
else
{
if (rot->s[1])
rot = rot->s[1];
else
{
rot->s[1] = new node(val, rot);
splay(rot->s[1]);
break;
}
}
}
}
size_t size()
{
if (head)
return head->size;
else
return 0;
}
friend void merge(Splay &a, Splay &b);
}tree[N];
inline void merge(Splay &a, Splay &b)
{
if (a.size() < b.size())
swap(a, b);
b.del(b.head, a);
b.head = NULL;
}
int arr[N], tmp[N];
int work()
{
read(n), read(m);
int last = 1;
for (int i = 1; i <= n; i++)
read(arr[i]);
p[1] = st[1] = ed[1] = 1;
for (int i = 2; i <= n; i++)
{
if (arr[i] != arr[i - 1])
{
tree[arr[i - 1]].insert(last);
st[i] = last = i, ++ans;
}
p[i] = last;
ed[last] = i;
}
tree[arr[n]].insert(last), ++ans;
while (m--)
{
int opt;
read(opt);
if (opt == 1)
{
int x, y;
read(x), read(y);
if (x != y)
merge(tree[y], tree[x]);
}
else
write(ans), putchar('\n');
}
return 0;
}
}
int main()
{
return zyt::work();
}

【BZOJ1483】[HNOI2009]梦幻布丁(平衡树启发式合并+并查集)的更多相关文章

  1. bzoj1483: [HNOI2009]梦幻布丁(vector+启发式合并)

    1483: [HNOI2009]梦幻布丁 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 4022  Solved: 1640[Submit][Statu ...

  2. bzoj1483: [HNOI2009]梦幻布丁(链表+启发式合并)

    题目大意:一个序列,两种操作. ①把其中的一种数修改成另一种数 ②询问有多少段不同的数如1 2 2 1为3段(1 / 2 2 / 1). 昨晚的BC的C题和这题很类似,于是现学现写居然过了十分开心. ...

  3. 【BZOJ1483】[HNOI2009]梦幻布丁 链表+启发式合并

    [BZOJ1483][HNOI2009]梦幻布丁 Description N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2 ...

  4. BZOJ 1483: [HNOI2009]梦幻布丁( 链表 + 启发式合并 )

    把相同颜色的串成一个链表, 然后每次A操作就启发式合并, 然后计算对答案的影响. ----------------------------------------------------------- ...

  5. BZOJ 1483: [HNOI2009]梦幻布丁 [链表启发式合并]

    1483: [HNOI2009]梦幻布丁 题意:一个带颜色序列,一种颜色合并到另一种,询问有多少颜色段 一种颜色开一个链表,每次遍历小的合并到大的里,顺带维护答案 等等,合并方向有规定? 令col[x ...

  6. bzoj 1483: [HNOI2009]梦幻布丁 (链表启发式合并)

    Description N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色. 例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. Input ...

  7. 洛谷P3201 [HNOI2009]梦幻布丁(链表 + 启发式合并)

    题目链接 给出 \(n\) 个布丁,每个补丁都有其颜色.现在有 \(m\) 次操作,每次操作将第 \(x_i\) 种颜色全部变为第 \(y_i\) 种颜色. 操作中可能会插入询问,回答目前总共有多少段 ...

  8. [BZOJ 1483] [HNOI2009] 梦幻布丁 (线段树合并)

    [BZOJ 1483] [HNOI2009] 梦幻布丁 (线段树合并) 题面 N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1 ...

  9. BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)

    不难...treap + 启发式合并 + 并查集 搞搞就行了 --------------------------------------------------------------------- ...

随机推荐

  1. 利用python去调用shell命令时候的踩到的坑

    shell中 True的返回值是0 False的返回值是1 Python中 True的返回值是1 False的返回值是0

  2. 洛谷 2471 BZOJ 1067 [SCOI2007]降雨量

    [题解] 用线段树维护区间最大值(因为没有修改,St表也可以),然后由于x,y可能是降雨量未知的年份,需要进行分类讨论. #include<cstdio> #include<algo ...

  3. [bzoj1072][SCOI2007][排列perm] (状态压缩+数位dp+排列去重)

    Description 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0).例如123434有90种排列能被2整除,其中末位为2的有30种,末位为4的有60种. Input ...

  4. Spring MVC概述(2)

    1.Spring 为展现层提供基于MVC设计理念的优秀的Web框架,是目前最主流的MVC框架之一. 2.Spring 3.0后全面超越Struts2,成为最优秀的MVC框架. 3.Spring MVC ...

  5. 吧,其实spring自带的BeanUtils就有这样的功能,引入spring-beans和spring-core之后,就有BeanUtils.copyProperties(a, b);可以实现两个javabean之间的相互拷贝,自己写的就当是研究咯---https://www.cnblogs.com/NieXiaoHui/p/7150928.html

    吧,其实spring自带的BeanUtils就有这样的功能,引入spring-beans和spring-core之后,就有BeanUtils.copyProperties(a, b);可以实现两个ja ...

  6. noip模拟赛 水管工的难题

    [问题描述]你是一名优秀的水管工. 一天你遇到了一个棘手的难题. 你需要在一个长方体状的房间内连接一条贯穿房间内部的水管.房间的长为 X,宽为 Y,高为 Z, 整个房间可以看成是 X×Y×Z个小立方体 ...

  7. 清北学堂模拟赛d1t2 火柴棒 (stick)

    题目描述众所周知的是,火柴棒可以拼成各种各样的数字.具体可以看下图: 通过2根火柴棒可以拼出数字“1”,通过5根火柴棒可以拼出数字“2”,以此类推. 现在LYK拥有k根火柴棒,它想将这k根火柴棒恰好用 ...

  8. (15)MOG背景减少

    1.根据上一帧找出变化的东西(如行走的人),消除背景,即不变的东西   motion detection 2.存在自身移动时的噪声和周围物体缓慢移动的噪声(这里播放的视频,我不断移动,背景可能有轻微的 ...

  9. vijos 1237 隐形的翅膀

    隐形的翅膀 背景 小杉终于进入了天堂.他看到每个人都带着一双隐形翅膀,他也想要. (小杉是怎么看到的?……) 描述 天使告诉小杉,每只翅膀都有长度,两只翅膀的长度之比越接近黄金分割比例,就越完美. 现 ...

  10. Ubuntu 16.04下没有“用户和组”功能的问题解决

    在16.04以前的版本会自带“用户和组”的功能,但是在16.04发现系统只自带了“用户账户”的功能. 问题解决: 1.安装gnome-system-tools sudo apt-get install ...