Fu_L's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: cartesian_tree
(src/tree/cartesian_tree.hpp)

cartesian_tree

pair<Graph<int>, int> cartesian_tree(vector<T> a)

数列 a から導かれるCartesian Treeとその根のインデックスを返します.

Cartesian Treeとは,長さ $n$ の数列 a の区間 $[0, n)$ に対して次の操作を再帰的に繰り返すことにより得られる二分木のことです.

Cartesian Treeの $2$ つの頂点 $(u, v)$ の LCA は 区間 $[u, v]$ の最小値に対応します.

計算量

Depends on

Verified with

Code

#pragma once
#include "../template/template.hpp"
#include "../graph/graph_template.hpp"
template <typename T>
pair<Graph<int>, int> cartesian_tree(const vector<T>& a) {
    const int n = (int)a.size();
    Graph<int> g(n);
    vector<int> p(n, -1), st;
    st.reserve(n);
    for(int i = 0; i < n; ++i) {
        int prv = -1;
        while(!st.empty() and a[i] < a[st.back()]) {
            prv = st.back();
            st.pop_back();
        }
        if(prv != -1) p[prv] = i;
        if(!st.empty()) p[i] = st.back();
        st.emplace_back(i);
    }
    int root = -1;
    for(int i = 0; i < n; ++i) {
        if(p[i] != -1) g.add_directed_edge(p[i], i);
        else root = i;
    }
    return {g, root};
}
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 4 "src/tree/cartesian_tree.hpp"
template <typename T>
pair<Graph<int>, int> cartesian_tree(const vector<T>& a) {
    const int n = (int)a.size();
    Graph<int> g(n);
    vector<int> p(n, -1), st;
    st.reserve(n);
    for(int i = 0; i < n; ++i) {
        int prv = -1;
        while(!st.empty() and a[i] < a[st.back()]) {
            prv = st.back();
            st.pop_back();
        }
        if(prv != -1) p[prv] = i;
        if(!st.empty()) p[i] = st.back();
        st.emplace_back(i);
    }
    int root = -1;
    for(int i = 0; i < n; ++i) {
        if(p[i] != -1) g.add_directed_edge(p[i], i);
        else root = i;
    }
    return {g, root};
}
Back to top page