原理
珂朵莉树,又称 Old Driver Tree,简称 ODT,可以维护一切区间操作。
ODT 的使用必须满足以下条件:
- 数据随机
- 有区间推平操作
ODT 的原理是把相同的颜色段压在一起处理,使用 std::set 存储。节点的结构体如下:
struct node {
int l, r;
mutable T val; // 加上 mutable 就可以修改 set 内部元素了
node(int x, int y, const T& v) : l(x), r(y), val(v) {}
bool operator< (const node& other) const {return l < other.l;}
};split
split 是 ODT 最核心的操作。它将 分裂为 和 并返回后者。
对于一个对 的区间操作,都可以对 暴力迭代。
至于代码实现非常简单,会 std::set 就行。
auto split(int x) {
auto it = odt.lower_bound(node(x, 0, 0));
if (it != odt.end() && it->l == x) return it;
it--;
int l = it->l, r = it->r; T v = it->val;
odt.erase(it), odt.emplace(l, x - 1, v);
return odt.emplace(x, r, v).first;
}assign
区间推平操作,先 split 出来这段,删掉以后再加入节点即可。
void assign(int l, int r, const T& val) {
auto itr = split(r + 1), itl = split(l);
odt.erase(itl, itr), odt.emplace(l, r, val);
}其他操作同理。注意:ODT 必须先 split 右端再 split 左端!
复杂度
这个东西的复杂度是个玄学……
对于 assign,可以认为复杂度为 。对于其它操作,一般复杂度不超过 。
例题
Willem, Chtholly and Seniorious
CF896C Willem, Chtholly and Seniorious。
这是珂朵莉树的起源题,关键代码如下:
template <class T>
class OldDriverTree {
private:
struct node {
int l, r;
mutable T val;
node(int x, int y, const T& v) : l(x), r(y), val(v) {}
bool operator< (const node& other) const {return l < other.l;}
};
set<node> odt;
auto split(int x) {
auto it = odt.lower_bound(node(x, 0, 0));
if (it != odt.end() && it->l == x) return it;
it--;
int l = it->l, r = it->r; T v = it->val;
odt.erase(it), odt.emplace(l, x - 1, v);
return odt.emplace(x, r, v).first;
}
public:
OldDriverTree() {}
template <typename It>
OldDriverTree(const It& begin, const It& end) {
int i = 1;
for (It it = begin; it != end; it++, i++) odt.emplace(i, i, *it);
}
void assign(int l, int r, const T& val) {
auto itr = split(r + 1), itl = split(l);
odt.erase(itl, itr);
odt.emplace(l, r, val);
}
void add(int l, int r, const T& val) {
auto itr = split(r + 1), itl = split(l);
for (auto it = itl; it != itr; it++) it->val += val;
}
T kth(int l, int r, int k) {
auto itr = split(r + 1), itl = split(l);
vector<pair<T, int>> v;
for (auto it = itl; it != itr; it++) v.emplace_back(it->val, it->r - it->l + 1);
sort(v.begin(), v.end());
for (const auto& info : v) {
if (info.second < k) k -= info.second;
else return info.first;
}
return T();
}
T sump(int l, int r, const T& x, const T& mod) {
auto itr = split(r + 1), itl = split(l);
T res(0);
for (auto it = itl; it != itr; it++) {
res += power(it->val, x, y) * (it->r - it->l + 1) % mod;
res %= mod;
}
return res;
}
};序列
这是 ODT 的大毒瘤题,对于 操作,需要仔细分析操作顺序,一处写挂就会全部 RE/MLE。
template <class T>
struct OldDriverTree {
private:
struct node {
int l, r;
mutable T val;
node(int x, int y, const T& v) : l(x), r(y), val(v) {}
bool operator< (const node& other) const {return l < other.l;}
};
set<node> odt;
auto split(int x) {
auto it = odt.lower_bound(node(x, 0, 0));
if (it != odt.end() && it->l == x) return it;
it--;
int l = it->l, r = it->r; T v = it->val;
odt.erase(it), odt.emplace(l, x - 1, v);
return odt.emplace(x, r, v).first;
}
public:
OldDriverTree() {}
template <typename It>
OldDriverTree(const It& begin, const It& end) {
int i = 1;
for (It it = begin; it != end; it++, i++) odt.emplace(i, i, *it);
//odt.emplace(n + 1, n + 1, 0LL);
}
void assign(int l, int r, const T& val) {
auto itr = split(r + 1), itl = split(l);
odt.erase(itl, itr);
odt.emplace(l, r, val % mod);
}
void add(int l, int r, const T& val) {
auto itr = split(r + 1), itl = split(l);
for (auto it = itl; it != itr; it++) it->val += val, it->val %= mod;
}
T sum(int l, int r) {
auto itr = split(r + 1), itl = split(l);
T res(0);
for (auto it = itl; it != itr; it++) {
res += it->val * (it->r - it->l + 1);
res %= mod;
}
return res;
}
void reverse(int l, int r) {
if (l > r) std::swap(l, r);
auto itr = split(r + 1), itl = split(l);
vector<node> a;
for (auto it = itl; it != itr; it++) {
a.emplace_back(it->l, it->r, it->val);
}
odt.erase(itl, itr);
for (const node& i : a) {
odt.emplace(r - i.r + l, r - i.l + l, i.val);
}
}
void copy(int l1, int r1, int l2, int r2) {
auto itr1 = split(r1 + 1), itl1 = split(l1);
vector<node> a;
for (auto it = itl1; it != itr1; it++) {
a.emplace_back(it->l, it->r, it->val);
}
auto itr2 = split(r2 + 1), itl2 = split(l2);
odt.erase(itl2, itr2);
for (const node& i : a) {
odt.emplace(i.l - l1 + l2, i.r - l1 + l2, i.val);
}
}
void swap(int l1, int r1, int l2, int r2) {
if (l1 > l2) std::swap(l1, l2), std::swap(r1, r2);
vector<node> a, b;
auto itr1 = split(r1 + 1), itl1 = split(l1);
for (auto it = itl1; it != itr1; it++) {
a.emplace_back(it->l, it->r, it->val);
}
odt.erase(itl1, itr1);
auto itr2 = split(r2 + 1), itl2 = split(l2);
for (auto it = itl2; it != itr2; it++) {
b.emplace_back(it->l, it->r, it->val);
}
odt.erase(itl2, itr2);
for (const node& i : b) {
odt.emplace(i.l - l2 + l1, i.r - l2 + l1, i.val);
}
for (const node& i : a) {
odt.emplace(i.l - l1 + l2, i.r - l1 + l2, i.val);
}
}
};[SDOI2011] 染色
先使用重链剖分变成序列问题。
跳重链的时候,需要分别记录两边的值,最后还要判断 LCA 两侧重链颜色是否相同。代码如下,没有封装:
struct node {
int l, r;
mutable int val;
node(int x, int y, const int& v) : l(x), r(y), val(v) {}
bool operator< (const node& other) const {return l < other.l;}
};
set<node> odt;
using IT = set<node>::iterator;
IT split(int x) {
IT it = odt.lower_bound(node(x, 0, 0));
if (it != odt.end() && it->l == x) return it;
it--;
int l = it->l, r = it->r, v = it->val;
odt.erase(it), odt.emplace(l, x - 1, v);
return odt.emplace(x, r, v).first;
}
void assign(int l, int r, int val) {
IT itr = split(r + 1), itl = split(l);
odt.erase(itl, itr);
odt.emplace(l, r, val);
}
int dep[N], sz[N], fa[N], son[N], top[N];
int cnt, dfn[N], a[N];
void dfs1(int u, int f) {
sz[u] = 1, fa[u] = f;
for (int j = head[u]; j != 0; j = edge[j].next) {
int v = edge[j].to;
if (v == f) continue;
dep[v] = dep[u] + 1, dfs1(v, u), sz[u] += sz[v];
if (sz[v] > sz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int t) {
dfn[u] = ++cnt, top[u] = t;
odt.emplace(dfn[u], dfn[u], a[u]);
if (son[u]) dfs2(son[u], t);
for (int j = head[u]; j != 0; j = edge[j].next) {
int v = edge[j].to;
if (v != fa[u] && v != son[u]) dfs2(v, v);
}
}
struct ret {
int cnt, col, last;
};
ret qry(int l, int r) {
ret res; res.cnt = 1;
IT itr = split(r + 1), itl = split(l);
res.col = res.last = itl->val;
for (; itl != itr; itl++) {
res.cnt += (itl-> val != res.last), res.last = itl->val;
}
return res;
}
void update(int u, int v, int c) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
assign(dfn[top[u]], dfn[u], c);
u = fa[top[u]];
}
if (dfn[u] > dfn[v]) swap(u, v);
assign(dfn[u], dfn[v], c);
}
int lca(int u, int v) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
u = fa[top[u]];
}
return dep[u] < dep[v] ? u : v;
}
int query(int u, int v) {
int res = 0, lc = -1, h = lca(u, v);
while (top[u] != top[h]) {
ret p = qry(dfn[top[u]], dfn[u]);
if (p.last == lc) res--;
lc = p.col, res += p.cnt;
u = fa[top[u]];
}
ret p = qry(dfn[h], dfn[u]);
if (p.last == lc) res--;
lc = -1, res += p.cnt;
while (top[v] != top[h]) {
ret p = qry(dfn[top[v]], dfn[v]);
if (p.last == lc) res--;
lc = p.col, res += p.cnt;
v = fa[top[v]];
}
p = qry(dfn[h], dfn[v]);
if (p.last == lc) res--;
lc = -1, res += p.cnt;
return res - 1;
}这东西的复杂度事实上非常玄学。假设数据随机,复杂度应当是 。Hack 了个寂寞。