結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー kimiyukikimiyuki
提出日時 2020-07-16 03:24:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 3,500 ms
コード長 7,136 bytes
コンパイル時間 1,347 ms
コンパイル使用メモリ 116,440 KB
実行使用メモリ 30,028 KB
最終ジャッジ日時 2023-08-14 23:36:08
合計ジャッジ時間 8,026 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 54 ms
17,068 KB
testcase_12 AC 54 ms
17,172 KB
testcase_13 AC 66 ms
20,328 KB
testcase_14 AC 58 ms
17,664 KB
testcase_15 AC 63 ms
18,996 KB
testcase_16 AC 57 ms
16,868 KB
testcase_17 AC 64 ms
19,508 KB
testcase_18 AC 60 ms
18,492 KB
testcase_19 AC 65 ms
19,388 KB
testcase_20 AC 70 ms
20,176 KB
testcase_21 AC 63 ms
18,568 KB
testcase_22 AC 55 ms
16,944 KB
testcase_23 AC 58 ms
17,320 KB
testcase_24 AC 69 ms
19,396 KB
testcase_25 AC 71 ms
20,176 KB
testcase_26 AC 68 ms
18,808 KB
testcase_27 AC 62 ms
17,872 KB
testcase_28 AC 72 ms
20,176 KB
testcase_29 AC 68 ms
20,240 KB
testcase_30 AC 71 ms
20,188 KB
testcase_31 AC 69 ms
20,312 KB
testcase_32 AC 69 ms
20,316 KB
testcase_33 AC 70 ms
20,364 KB
testcase_34 AC 73 ms
29,976 KB
testcase_35 AC 69 ms
25,064 KB
testcase_36 AC 68 ms
23,720 KB
testcase_37 AC 70 ms
25,572 KB
testcase_38 AC 69 ms
24,448 KB
testcase_39 AC 67 ms
23,276 KB
testcase_40 AC 68 ms
23,256 KB
testcase_41 AC 68 ms
24,768 KB
testcase_42 AC 67 ms
23,960 KB
testcase_43 AC 67 ms
22,484 KB
testcase_44 AC 65 ms
22,084 KB
testcase_45 AC 64 ms
21,560 KB
testcase_46 AC 67 ms
25,200 KB
testcase_47 AC 68 ms
25,340 KB
testcase_48 AC 70 ms
25,668 KB
testcase_49 AC 69 ms
25,272 KB
testcase_50 AC 68 ms
25,420 KB
testcase_51 AC 73 ms
29,884 KB
testcase_52 AC 74 ms
30,028 KB
testcase_53 AC 73 ms
30,024 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "graph/cartesian_tree.yukicoder-1031.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1031"
#line 2 "utils/macros.hpp"
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
#line 2 "graph/cartesian_tree.hpp"
#include <functional>
#include <vector>
#line 5 "graph/cartesian_tree.hpp"

/**
 * @brief Cartesian tree ($O(n)$)
 * @note the smallest value is the root
 * @note if a is not distinct, the way for tie-break is undefined
 * @return the binary tree as the list of parents
 */
template <class T, class Comparator = std::less<int> >
std::vector<int> construct_cartesian_tree(const std::vector<T> & a, const Comparator & cmp = Comparator()) {
    int n = a.size();
    std::vector<int> parent(n, -1);
    REP3 (i, 1, n) {
        int p = i - 1;  // parent of i
        int l = -1;  // left child of i
        while (p != -1 and cmp(a[i], a[p])) {
            int pp = parent[p];  // parent of parent of i
            if (l != -1) {
                parent[l] = p;
            }
            parent[p] = i;
            l = p;
            p = pp;
        }
        parent[i] = p;
    }
    return parent;
}
#line 2 "graph/format.hpp"
#include <cassert>
#include <utility>
#line 6 "graph/format.hpp"

std::pair<std::vector<std::vector<int> >, int> children_from_parent(const std::vector<int> & parent) {
    int n = parent.size();
    std::vector<std::vector<int> > children(n);
    int root = -1;
    REP (x, n) {
        if (parent[x] == -1) {
            assert (root == -1);
            root = x;
        } else {
            children[parent[x]].push_back(x);
        }
    }
    assert (root != -1);
    return std::make_pair(children, root);
}

std::vector<std::vector<int> > adjacent_list_from_children(const std::vector<std::vector<int> > & children) {
    int n = children.size();
    std::vector<std::vector<int> > g(n);
    REP (x, n) {
        for (int y : children[x]) {
            g[x].push_back(y);
            g[y].push_back(x);
        }
    }
    return g;
}
#line 2 "utils/greedily_increasing_subsequence.hpp"
#include <stack>
#include <tuple>
#line 5 "data_structure/sparse_table.hpp"

/**
 * @brief Sparse Table (idempotent monoid)
 * @note the unit is required just for convenience
 * @note $O(N \log N)$ space
 */
template <class IdempotentMonoid>
struct sparse_table {
    typedef typename IdempotentMonoid::value_type value_type;
    std::vector<std::vector<value_type> > table;
    IdempotentMonoid mon;
    sparse_table() = default;

    /**
     * @note $O(N \log N)$ time
     */
    template <class InputIterator>
    sparse_table(InputIterator first, InputIterator last, const IdempotentMonoid & mon_ = IdempotentMonoid())
            : mon(mon_) {
        table.emplace_back(first, last);
        int n = table[0].size();
        int log_n = 32 - __builtin_clz(n);
        table.resize(log_n, std::vector<value_type>(n));
        REP (k, log_n - 1) {
            REP (i, n) {
                table[k + 1][i] = i + (1ll << k) < n ?
                    mon.mult(table[k][i], table[k][i + (1ll << k)]) :
                    table[k][i];
            }
        }
    }

    /**
     * @note $O(1)$
     */
    value_type range_get(int l, int r) const {
        if (l == r) return mon.unit();  // if there is no unit, remove this line
        assert (0 <= l and l < r and r <= (int)table[0].size());
        int k = 31 - __builtin_clz(r - l);  // log2
        return mon.mult(table[k][l], table[k][r - (1ll << k)]);
    }
};
#line 2 "monoids/min.hpp"
#include <algorithm>
#include <limits>

template <class T>
struct min_monoid {
    typedef T value_type;
    value_type unit() const { return std::numeric_limits<T>::max(); }
    value_type mult(value_type a, value_type b) const { return std::min(a, b); }
};
#line 9 "utils/greedily_increasing_subsequence.hpp"

/**
 * @brief Length of Greedily Increasing Subsequences (前処理 $O(n \log n)$ + $O(1)$)
 * @description computes the lengths of the greedily increasing subsubsequence for the given interval
 * @note the greedily increasing subsubsequence for a sequence $a$ means the subsubsequence of the elements $a_i$ which satisfy $\forall j \lt i. a_j \lt a_i$.
 */
class greedily_increasing_subsequence {
    std::vector<int> depth;
    sparse_table<min_monoid<int> > table;

public:
    greedily_increasing_subsequence() = default;

    int operator () (int l, int r) const {
        assert (0 <= l and l <= r and r <= (int)depth.size());
        if (l == r) return 0;
        return depth[l] - table.range_get(l, r) + 1;
    }

private:
    greedily_increasing_subsequence(const std::vector<int> & depth_)
            : depth(depth_), table(ALL(depth_)) {
    }

public:
    /**
     * @note this is just a constructor, but is needed to specify template arguments.
     */
    template <class T, class Comparator = std::less<T>, class RandomAccessIterator>
    static greedily_increasing_subsequence construct(RandomAccessIterator first, RandomAccessIterator last, const Comparator & cmp = Comparator()) {
        int n = std::distance(first, last);

        // make a forest
        std::vector<int> parent(n, -1);
        std::stack<int> stk;
        REP (i, n) {
            while (not stk.empty() and cmp(*(first + stk.top()), *(first + i))) {
                parent[stk.top()] = i;
                stk.pop();
            }
            stk.push(i);
        }

        // calculate depths
        std::vector<int> depth(n);
        REP_R (i, n) {
            if (parent[i] != -1) {
                depth[i] = depth[parent[i]] + 1;
            }
        }

        return greedily_increasing_subsequence(depth);
    }
};
#line 6 "graph/cartesian_tree.yukicoder-1031.test.cpp"
#include <cstdio>
#line 10 "graph/cartesian_tree.yukicoder-1031.test.cpp"

#include <iostream>
using namespace std;

int64_t solve1(int n, const vector<int> & p) {
    // prepare a data structure for the sequence
    auto f = greedily_increasing_subsequence::construct<int>(ALL(p));

    // construct the Cartesian tree
    vector<int> parent = construct_cartesian_tree(p);
    vector<vector<int> > children; int root; tie(children, root) = children_from_parent(parent);

    // fold the Cartesian tree
    int64_t ans = 0;
    auto go = [&](auto && go, int l, int m, int r) -> void {
        if (l == r) {
            return;
        }
        ans += f(m + 1, r);
        for (int x : children[m]) {
            if (x < m) {
                go(go, l, x, m);
            } else {
                go(go, m + 1, x, r);
            }
        }
    };
    go(go, 0, root, n);
    return ans;
}

int64_t solve(int n, vector<int> p) {
    int64_t ans = solve1(n, p);
    reverse(ALL(p));
    return ans + solve1(n, p);
}

int main() {
    int n; scanf("%d", &n);
    vector<int> p(n);
    REP (i, n) {
        scanf("%d", &p[i]);
    }
    long long ans = solve(n, p);
    printf("%lld\n", ans);
    return 0;
}
0