This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "structure/others/permutation-tree.hpp"
順列 $A$ に対応する順列木(Permutation Tree, 析合树) を構築します。
$ \max\limits_{l \leq i \leq r} A_i - \min\limits_{l \leq i \leq r} A_i = r-l$ が成立するような $[l, r]$ を良い連続部分列と定義する. この良い連続部分列を効率的に求めます。
順列木のそれぞれのノードはいくつかのタイプに分類されます。定義を以下に示します。
LEAF
: 葉ノード。数列のある 1 要素に対応する。JOIN
: そのノードが持つ子の列から、連続する 2 つ以上の子を選んだときに全てが良い連続部分列となる。CUT
: そのノードが持つ子の列から、どの連続する 2 つ以上の子を選んでも良い連続部分列にはならない。PermutationTree()
順列木を初期化します。
static NP build(vector<int> &A)
順列 A
に対応する順列木を返します。Node
には、ノードのタイプ type
, そのノードに対応する順列の index の区間 $[l, r)$, ノードに含まれる要素の値 [min_v, max_v)
, 子 ch
が格納されます。
A
は $[0, n)$ からなる順列#include "../class/range-add-range-min.hpp"
#include "../segment-tree/lazy-segment-tree.hpp"
/**
* @see https://codeforces.com/blog/entry/78898
*/
struct PermutationTree {
public:
enum NodeType { JOIN_ASC, JOIN_DESC, LEAF, CUT };
struct Node {
NodeType type;
int l, r; // [l, r)
int min_v, max_v; // [min_v, max_v)
vector<Node *> ch;
size_t size() const { return r - l; }
bool is_join() const { return type == JOIN_ASC or type == JOIN_DESC; };
bool is_leaf() const { return type == LEAF; }
bool is_cut() const { return type == CUT; }
};
using NP = Node *;
PermutationTree() = default;
private:
static void add_child(NP t, NP c) {
t->ch.emplace_back(c);
t->l = min(t->l, c->l);
t->r = max(t->r, c->r);
t->min_v = min(t->min_v, c->min_v);
t->max_v = max(t->max_v, c->max_v);
}
public:
static NP build(vector<int> &A) {
int n = (int)A.size();
vector<int> desc{-1};
vector<int> asc{-1};
vector<NP> st;
LazySegmentTree seg{RangeAddRangeMin<int>(), vector<int>(n)};
for (int i = 0; i < n; i++) {
while (~desc.back() and A[i] > A[desc.back()]) {
seg.apply(desc[desc.size() - 2] + 1, desc.back() + 1,
A[i] - A[desc.back()]);
desc.pop_back();
}
while (~asc.back() and A[i] < A[asc.back()]) {
seg.apply(asc[asc.size() - 2] + 1, asc.back() + 1,
A[asc.back()] - A[i]);
asc.pop_back();
}
desc.emplace_back(i);
asc.emplace_back(i);
NP t = new Node{LEAF, i, i + 1, A[i], A[i] + 1, {}};
for (;;) {
NodeType type = CUT;
if (not st.empty()) {
if (st.back()->max_v == t->min_v) {
type = JOIN_ASC;
} else if (t->max_v == st.back()->min_v) {
type = JOIN_DESC;
}
}
if (type != CUT) {
NP r = st.back();
if (type != r->type) {
r = new Node{type, r->l, r->r, r->min_v, r->max_v, {r}};
}
add_child(r, t);
st.pop_back();
t = r;
} else if (seg.prod(0, i + 1 - (int)t->size()) == 0) {
t = new Node{CUT, t->l, t->r, t->min_v, t->max_v, {t}};
do {
add_child(t, st.back());
st.pop_back();
} while (t->max_v - t->min_v != t->size());
reverse(begin(t->ch), end(t->ch));
} else {
break;
}
}
st.emplace_back(t);
seg.apply(0, i + 1, -1);
}
return st[0];
}
};
#line 1 "structure/class/range-add-range-min.hpp"
template <typename T>
struct RangeAddRangeMin {
using S = T;
using F = T;
static constexpr S op(const S &a, const S &b) { return min(a, b); }
static constexpr S e() { return numeric_limits<T>::max(); }
static constexpr F mapping(const S &x, const F &f) { return x + f; }
static constexpr F composition(const F &f, const F &g) { return f + g; }
static constexpr F id() { return {0}; }
};
#line 2 "structure/class/acted-monoid.hpp"
template <typename S2, typename Op, typename E, typename F2, typename Mapping,
typename Composition, typename Id>
struct LambdaActedMonoid {
using S = S2;
using F = F2;
S op(const S &a, const S &b) const { return _op(a, b); }
S e() const { return _e(); }
S mapping(const S &x, const F &f) const { return _mapping(x, f); }
F composition(const F &f, const F &g) const { return _composition(f, g); }
F id() const { return _id(); }
LambdaActedMonoid(Op _op, E _e, Mapping _mapping, Composition _composition,
Id _id)
: _op(_op),
_e(_e),
_mapping(_mapping),
_composition(_composition),
_id(_id) {}
private:
Op _op;
E _e;
Mapping _mapping;
Composition _composition;
Id _id;
};
template <typename Op, typename E, typename Mapping, typename Composition,
typename Id>
LambdaActedMonoid(Op _op, E _e, Mapping _mapping, Composition _composition,
Id _id)
-> LambdaActedMonoid<decltype(_e()), Op, E, decltype(_id()), Mapping,
Composition, Id>;
/*
struct ActedMonoid {
using S = ?;
using F = ?;
static constexpr S op(const S& a, const S& b) {}
static constexpr S e() {}
static constexpr S mapping(const S &x, const F &f) {}
static constexpr F composition(const F &f, const F &g) {}
static constexpr F id() {}
};
*/
#line 2 "structure/segment-tree/lazy-segment-tree.hpp"
template <typename ActedMonoid>
struct LazySegmentTree {
using S = typename ActedMonoid::S;
using F = typename ActedMonoid::F;
private:
ActedMonoid m;
int n{}, sz{}, height{};
vector<S> data;
vector<F> lazy;
inline void update(int k) {
data[k] = m.op(data[2 * k + 0], data[2 * k + 1]);
}
inline void all_apply(int k, const F &x) {
data[k] = m.mapping(data[k], x);
if (k < sz) lazy[k] = m.composition(lazy[k], x);
}
inline void propagate(int k) {
if (lazy[k] != m.id()) {
all_apply(2 * k + 0, lazy[k]);
all_apply(2 * k + 1, lazy[k]);
lazy[k] = m.id();
}
}
public:
LazySegmentTree() = default;
explicit LazySegmentTree(ActedMonoid m, int n) : m(m), n(n) {
sz = 1;
height = 0;
while (sz < n) sz <<= 1, height++;
data.assign(2 * sz, m.e());
lazy.assign(2 * sz, m.id());
}
explicit LazySegmentTree(ActedMonoid m, const vector<S> &v)
: LazySegmentTree(m, v.size()) {
build(v);
}
void build(const vector<S> &v) {
assert(n == (int)v.size());
for (int k = 0; k < n; k++) data[k + sz] = v[k];
for (int k = sz - 1; k > 0; k--) update(k);
}
void set(int k, const S &x) {
k += sz;
for (int i = height; i > 0; i--) propagate(k >> i);
data[k] = x;
for (int i = 1; i <= height; i++) update(k >> i);
}
S get(int k) {
k += sz;
for (int i = height; i > 0; i--) propagate(k >> i);
return data[k];
}
S operator[](int k) { return get(k); }
S prod(int l, int r) {
if (l >= r) return m.e();
l += sz;
r += sz;
for (int i = height; i > 0; i--) {
if (((l >> i) << i) != l) propagate(l >> i);
if (((r >> i) << i) != r) propagate((r - 1) >> i);
}
S L = m.e(), R = m.e();
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) L = m.op(L, data[l++]);
if (r & 1) R = m.op(data[--r], R);
}
return m.op(L, R);
}
S all_prod() const { return data[1]; }
void apply(int k, const F &f) {
k += sz;
for (int i = height; i > 0; i--) propagate(k >> i);
data[k] = m.mapping(data[k], f);
for (int i = 1; i <= height; i++) update(k >> i);
}
void apply(int l, int r, const F &f) {
if (l >= r) return;
l += sz;
r += sz;
for (int i = height; i > 0; i--) {
if (((l >> i) << i) != l) propagate(l >> i);
if (((r >> i) << i) != r) propagate((r - 1) >> i);
}
{
int l2 = l, r2 = r;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) all_apply(l++, f);
if (r & 1) all_apply(--r, f);
}
l = l2, r = r2;
}
for (int i = 1; i <= height; i++) {
if (((l >> i) << i) != l) update(l >> i);
if (((r >> i) << i) != r) update((r - 1) >> i);
}
}
template <typename C>
int find_first(int l, const C &check) {
if (l >= n) return n;
l += sz;
for (int i = height; i > 0; i--) propagate(l >> i);
S sum = m.e();
do {
while ((l & 1) == 0) l >>= 1;
if (check(m.op(sum, data[l]))) {
while (l < sz) {
propagate(l);
l <<= 1;
auto nxt = m.op(sum, data[l]);
if (not check(nxt)) {
sum = nxt;
l++;
}
}
return l + 1 - sz;
}
sum = m.op(sum, data[l++]);
} while ((l & -l) != l);
return n;
}
template <typename C>
int find_last(int r, const C &check) {
if (r <= 0) return -1;
r += sz;
for (int i = height; i > 0; i--) propagate((r - 1) >> i);
S sum = m.e();
do {
r--;
while (r > 1 and (r & 1)) r >>= 1;
if (check(m.op(data[r], sum))) {
while (r < sz) {
propagate(r);
r = (r << 1) + 1;
auto nxt = m.op(data[r], sum);
if (not check(nxt)) {
sum = nxt;
r--;
}
}
return r - sz;
}
sum = m.op(data[r], sum);
} while ((r & -r) != r);
return -1;
}
};
#line 3 "structure/others/permutation-tree.hpp"
/**
* @see https://codeforces.com/blog/entry/78898
*/
struct PermutationTree {
public:
enum NodeType { JOIN_ASC, JOIN_DESC, LEAF, CUT };
struct Node {
NodeType type;
int l, r; // [l, r)
int min_v, max_v; // [min_v, max_v)
vector<Node *> ch;
size_t size() const { return r - l; }
bool is_join() const { return type == JOIN_ASC or type == JOIN_DESC; };
bool is_leaf() const { return type == LEAF; }
bool is_cut() const { return type == CUT; }
};
using NP = Node *;
PermutationTree() = default;
private:
static void add_child(NP t, NP c) {
t->ch.emplace_back(c);
t->l = min(t->l, c->l);
t->r = max(t->r, c->r);
t->min_v = min(t->min_v, c->min_v);
t->max_v = max(t->max_v, c->max_v);
}
public:
static NP build(vector<int> &A) {
int n = (int)A.size();
vector<int> desc{-1};
vector<int> asc{-1};
vector<NP> st;
LazySegmentTree seg{RangeAddRangeMin<int>(), vector<int>(n)};
for (int i = 0; i < n; i++) {
while (~desc.back() and A[i] > A[desc.back()]) {
seg.apply(desc[desc.size() - 2] + 1, desc.back() + 1,
A[i] - A[desc.back()]);
desc.pop_back();
}
while (~asc.back() and A[i] < A[asc.back()]) {
seg.apply(asc[asc.size() - 2] + 1, asc.back() + 1,
A[asc.back()] - A[i]);
asc.pop_back();
}
desc.emplace_back(i);
asc.emplace_back(i);
NP t = new Node{LEAF, i, i + 1, A[i], A[i] + 1, {}};
for (;;) {
NodeType type = CUT;
if (not st.empty()) {
if (st.back()->max_v == t->min_v) {
type = JOIN_ASC;
} else if (t->max_v == st.back()->min_v) {
type = JOIN_DESC;
}
}
if (type != CUT) {
NP r = st.back();
if (type != r->type) {
r = new Node{type, r->l, r->r, r->min_v, r->max_v, {r}};
}
add_child(r, t);
st.pop_back();
t = r;
} else if (seg.prod(0, i + 1 - (int)t->size()) == 0) {
t = new Node{CUT, t->l, t->r, t->min_v, t->max_v, {t}};
do {
add_child(t, st.back());
st.pop_back();
} while (t->max_v - t->min_v != t->size());
reverse(begin(t->ch), end(t->ch));
} else {
break;
}
}
st.emplace_back(t);
seg.apply(0, i + 1, -1);
}
return st[0];
}
};