結果
問題 | No.1031 いたずら好きなお姉ちゃん |
ユーザー | kimiyuki |
提出日時 | 2020-07-15 05:14:53 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 139 ms / 3,500 ms |
コード長 | 6,930 bytes |
コンパイル時間 | 1,123 ms |
コンパイル使用メモリ | 88,668 KB |
実行使用メモリ | 29,832 KB |
最終ジャッジ日時 | 2024-11-20 18:31:23 |
合計ジャッジ時間 | 8,992 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 6 ms
5,248 KB |
testcase_04 | AC | 6 ms
5,248 KB |
testcase_05 | AC | 6 ms
5,248 KB |
testcase_06 | AC | 7 ms
5,248 KB |
testcase_07 | AC | 6 ms
5,248 KB |
testcase_08 | AC | 6 ms
5,248 KB |
testcase_09 | AC | 6 ms
5,248 KB |
testcase_10 | AC | 6 ms
5,248 KB |
testcase_11 | AC | 113 ms
16,084 KB |
testcase_12 | AC | 102 ms
15,972 KB |
testcase_13 | AC | 133 ms
18,888 KB |
testcase_14 | AC | 113 ms
16,724 KB |
testcase_15 | AC | 126 ms
17,912 KB |
testcase_16 | AC | 113 ms
15,880 KB |
testcase_17 | AC | 118 ms
17,948 KB |
testcase_18 | AC | 121 ms
17,364 KB |
testcase_19 | AC | 136 ms
18,128 KB |
testcase_20 | AC | 137 ms
18,736 KB |
testcase_21 | AC | 129 ms
17,480 KB |
testcase_22 | AC | 102 ms
15,800 KB |
testcase_23 | AC | 119 ms
16,148 KB |
testcase_24 | AC | 131 ms
18,032 KB |
testcase_25 | AC | 124 ms
19,808 KB |
testcase_26 | AC | 127 ms
17,656 KB |
testcase_27 | AC | 108 ms
16,476 KB |
testcase_28 | AC | 134 ms
18,940 KB |
testcase_29 | AC | 129 ms
18,932 KB |
testcase_30 | AC | 129 ms
18,764 KB |
testcase_31 | AC | 134 ms
18,928 KB |
testcase_32 | AC | 139 ms
18,824 KB |
testcase_33 | AC | 131 ms
18,836 KB |
testcase_34 | AC | 131 ms
29,832 KB |
testcase_35 | AC | 90 ms
24,824 KB |
testcase_36 | AC | 91 ms
23,476 KB |
testcase_37 | AC | 91 ms
25,044 KB |
testcase_38 | AC | 86 ms
22,644 KB |
testcase_39 | AC | 91 ms
21,272 KB |
testcase_40 | AC | 85 ms
21,120 KB |
testcase_41 | AC | 88 ms
22,568 KB |
testcase_42 | AC | 89 ms
22,612 KB |
testcase_43 | AC | 86 ms
20,732 KB |
testcase_44 | AC | 92 ms
19,644 KB |
testcase_45 | AC | 86 ms
19,756 KB |
testcase_46 | AC | 98 ms
23,556 KB |
testcase_47 | AC | 104 ms
21,600 KB |
testcase_48 | AC | 110 ms
24,916 KB |
testcase_49 | AC | 97 ms
22,504 KB |
testcase_50 | AC | 99 ms
22,928 KB |
testcase_51 | AC | 108 ms
22,744 KB |
testcase_52 | AC | 109 ms
23,492 KB |
testcase_53 | AC | 109 ms
23,484 KB |
testcase_54 | AC | 2 ms
5,248 KB |
testcase_55 | AC | 2 ms
5,248 KB |
ソースコード
#line 1 "main.cpp" #define PROBLEM #line 2 "/home/user/Library/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 "/home/user/Library/graph/cartesian_tree.hpp" #include <functional> #include <vector> #line 5 "/home/user/Library/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; int l = -1; while (p != -1 and cmp(a[i], a[p])) { int pp = parent[p]; if (l != -1) { parent[l] = p; } parent[p] = i; p = pp; } parent[i] = p; } return parent; } #line 2 "/home/user/Library/graph/format.hpp" #include <cassert> #include <utility> #line 6 "/home/user/Library/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 "/home/user/Library/graph/subtree.hpp" #include <algorithm> #line 4 "/home/user/Library/graph/subtree.hpp" struct subtree_info_t { int parent; // in the entire tree int depth; // in the entire tree int size; // of the subtree int height; // of the subtree }; /** * @brief subtree info / それぞれの部分木の size とか height とかをまとめて求めておいてくれるやつ * @arg g must be a tree * @note O(n) time * @note O(n) space on heap */ std::vector<subtree_info_t> prepare_subtree_info(std::vector<std::vector<int> > const & g, int root) { int n = g.size(); std::vector<subtree_info_t> info(n, (subtree_info_t) { -1, -1, -1, -1 }); std::vector<int> topological(n); topological[0] = root; info[root].parent = root; info[root].depth = 0; int r = 1; for (int l = 0; l < r; ++ l) { int i = topological[l]; for (int j : g[i]) if (j != info[i].parent) { topological[r ++] = j; info[j].parent = i; info[j].depth = info[i].depth + 1; } } while ((-- r) >= 0) { int i = topological[r]; info[i].size = 1; info[i].height = 0; for (int j : g[i]) if (j != info[i].parent) { info[i].size += info[j].size; info[i].height = std::max(info[i].height, info[j].height + 1); } } info[root].parent = -1; return info; } #line 5 "/home/user/Library/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 3 "/home/user/Library/monoids/min.hpp" #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 8 "main.cpp" #include <cstdio> #line 12 "main.cpp" using namespace std; int64_t solve1(int n, const vector<int> & p) { // prepare a data structure for LIS vector<int> depth(n); { vector<int> parent = construct_cartesian_tree<int, greater<int> >(p); vector<vector<int> > children; int root; tie(children, root) = children_from_parent(parent); auto g = adjacent_list_from_children(children); auto info = prepare_subtree_info(g, root); REP (x, n) { depth[x] = info[x].depth; } } sparse_table<min_monoid<int> > table(ALL(depth)); auto lis = [&](int l, int r) { if (l == r) return 0; return depth[l] - table.range_get(l, r) + 1; }; // fold the Cartesian tree vector<int> parent = construct_cartesian_tree(p); vector<vector<int> > children; int root; tie(children, root) = children_from_parent(parent); int64_t ans = 0; auto go = [&](auto && go, int l, int m, int r) -> void { if (l == r) { return; } ans += lis(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; }