結果
問題 | No.1720 Division Permutation |
ユーザー | hitonanode |
提出日時 | 2021-10-24 11:39:34 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 175 ms / 4,000 ms |
コード長 | 12,539 bytes |
コンパイル時間 | 2,828 ms |
コンパイル使用メモリ | 198,944 KB |
実行使用メモリ | 25,424 KB |
最終ジャッジ日時 | 2024-10-01 22:50:26 |
合計ジャッジ時間 | 10,218 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 1 ms
5,248 KB |
testcase_07 | AC | 1 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 1 ms
5,248 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 1 ms
5,248 KB |
testcase_12 | AC | 2 ms
5,248 KB |
testcase_13 | AC | 163 ms
24,392 KB |
testcase_14 | AC | 165 ms
24,392 KB |
testcase_15 | AC | 95 ms
16,380 KB |
testcase_16 | AC | 153 ms
23,560 KB |
testcase_17 | AC | 113 ms
21,768 KB |
testcase_18 | AC | 111 ms
24,904 KB |
testcase_19 | AC | 163 ms
25,196 KB |
testcase_20 | AC | 164 ms
25,136 KB |
testcase_21 | AC | 161 ms
25,056 KB |
testcase_22 | AC | 160 ms
25,004 KB |
testcase_23 | AC | 160 ms
25,360 KB |
testcase_24 | AC | 159 ms
25,424 KB |
testcase_25 | AC | 157 ms
25,200 KB |
testcase_26 | AC | 157 ms
24,876 KB |
testcase_27 | AC | 175 ms
25,008 KB |
testcase_28 | AC | 163 ms
24,872 KB |
testcase_29 | AC | 159 ms
25,388 KB |
testcase_30 | AC | 161 ms
25,004 KB |
testcase_31 | AC | 157 ms
25,004 KB |
testcase_32 | AC | 1 ms
5,248 KB |
testcase_33 | AC | 2 ms
5,248 KB |
testcase_34 | AC | 1 ms
5,248 KB |
testcase_35 | AC | 2 ms
5,248 KB |
testcase_36 | AC | 1 ms
5,248 KB |
testcase_37 | AC | 2 ms
5,248 KB |
testcase_38 | AC | 2 ms
5,248 KB |
testcase_39 | AC | 2 ms
5,248 KB |
testcase_40 | AC | 1 ms
5,248 KB |
testcase_41 | AC | 2 ms
5,248 KB |
testcase_42 | AC | 1 ms
5,248 KB |
testcase_43 | AC | 141 ms
22,192 KB |
testcase_44 | AC | 129 ms
21,832 KB |
testcase_45 | AC | 118 ms
21,780 KB |
testcase_46 | AC | 82 ms
15,252 KB |
testcase_47 | AC | 118 ms
21,948 KB |
testcase_48 | AC | 88 ms
15,872 KB |
testcase_49 | AC | 140 ms
22,600 KB |
testcase_50 | AC | 112 ms
21,724 KB |
testcase_51 | AC | 88 ms
15,912 KB |
testcase_52 | AC | 131 ms
21,600 KB |
testcase_53 | AC | 158 ms
24,736 KB |
testcase_54 | AC | 159 ms
24,388 KB |
testcase_55 | AC | 158 ms
24,876 KB |
testcase_56 | AC | 161 ms
25,148 KB |
testcase_57 | AC | 168 ms
24,312 KB |
testcase_58 | AC | 159 ms
24,724 KB |
testcase_59 | AC | 159 ms
24,748 KB |
testcase_60 | AC | 163 ms
24,356 KB |
testcase_61 | AC | 158 ms
24,688 KB |
testcase_62 | AC | 159 ms
25,116 KB |
ソースコード
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <deque> #include <forward_list> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } #endif template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl #define dbgif(cond, x) ((cond) ? cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl : cerr) #else #define dbg(x) (x) #define dbgif(cond, x) 0 #endif // StarrySkyTree: segment tree for Range Minimum Query & Range Add Query // Complexity: (N)$ (construction), (\log N)$ (add / get / prod) // - RangeAddRangeMin(std::vector<Tp> data_init) : Initialize array x by data_init. // - add(int begin, int end, Tp vadd) : Update x[i] <- x[i] + vadd for all begin <= i < end. // - get(int pos) : Get x[pos]. // - prod(int begin, int end) : Get min(x[begin], ..., x[end - 1]). template <typename Tp, Tp defaultT = std::numeric_limits<Tp>::max() / 2> struct RangeAddRangeMin { int N, head; std::vector<Tp> range_min, range_add; static inline Tp f(Tp x, Tp y) noexcept { return std::min(x, y); } inline void _merge(int pos) { range_min[pos] = f(range_min[pos * 2] + range_add[pos * 2], range_min[pos * 2 + 1] + range_add[pos * 2 + 1]); } void initialize(const std::vector<Tp> &data_init) { N = data_init.size(), head = 1; while (head < N) head <<= 1; range_min.assign(head * 2, defaultT); range_add.assign(head * 2, 0); std::copy(data_init.begin(), data_init.end(), range_min.begin() + head); for (int pos = head; --pos;) _merge(pos); } RangeAddRangeMin() = default; RangeAddRangeMin(const std::vector<Tp> &data_init) { initialize(data_init); } void _add(int begin, int end, int pos, int l, int r, Tp vadd) noexcept { if (r <= begin or end <= l) return; if (begin <= l and r <= end) { range_add[pos] += vadd; return; } _add(begin, end, pos * 2, l, (l + r) / 2, vadd); _add(begin, end, pos * 2 + 1, (l + r) / 2, r, vadd); _merge(pos); } // Add `vadd` to (x_begin, ..., x_{end - 1}) void add(int begin, int end, Tp vadd) noexcept { return _add(begin, end, 1, 0, head, vadd); } Tp _get(int begin, int end, int pos, int l, int r) const noexcept { if (r <= begin or end <= l) return defaultT; if (begin <= l and r <= end) return range_min[pos] + range_add[pos]; return f(_get(begin, end, pos * 2, l, (l + r) / 2), _get(begin, end, pos * 2 + 1, (l + r) / 2, r)) + range_add[pos]; } // Return f(x_begin, ..., x_{end - 1}) Tp get(int pos) const noexcept { return prod(pos, pos + 1); } Tp prod(int begin, int end) const noexcept { return _get(begin, end, 1, 0, head); } }; struct permutation_tree { enum NodeType { JoinAsc, JoinDesc, Cut, Leaf, None, }; struct node { NodeType tp; int L, R; // i in [L, R) int mini, maxi; // A[i] in [min, max] std::vector<int> child; int sz() const { return maxi - mini + 1; } template <class OStream> friend OStream &operator<<(OStream &os, const node &n) { os << "[[" << n.L << ',' << n.R << ")(ch:"; for (auto i : n.child) os << i << ','; return os << ")(tp=" << n.tp << ")]"; } }; int root; std::vector<int> A; std::vector<node> nodes; bool is_adj(int lid, int rid) const { return nodes[lid].maxi + 1 == nodes[rid].mini; } void add_child(int parid, int chid) { nodes[parid].child.push_back(chid); nodes[parid].L = std::min(nodes[parid].L, nodes[chid].L); nodes[parid].R = std::max(nodes[parid].R, nodes[chid].R); nodes[parid].mini = std::min(nodes[parid].mini, nodes[chid].mini); nodes[parid].maxi = std::max(nodes[parid].maxi, nodes[chid].maxi); } NodeType findtype(int lid, int rid) const { const auto &l = nodes[lid].tp, &r = nodes[rid].tp; if (is_adj(lid, rid)) return NodeType::JoinAsc; if (is_adj(rid, lid)) return NodeType::JoinDesc; return NodeType::None; } permutation_tree(const std::vector<int> &A_) : root(-1), A(A_) { // A: 0-origin RangeAddRangeMin<int> segtree((std::vector<int>(A.size()))); std::vector<std::pair<int, int>> hi{{A.size() + 1, -1}}, lo{{-1, -1}}; std::vector<int> st; for (int i = 0; i < int(A.size()); ++i) { while (A[i] > hi.back().first) { segtree.add(hi[hi.size() - 2].second + 1, hi.back().second + 1, A[i] - hi.back().first); hi.pop_back(); } hi.emplace_back(A[i], i); while (A[i] < lo.back().first) { segtree.add(lo[lo.size() - 2].second + 1, lo.back().second + 1, lo.back().first - A[i]); lo.pop_back(); } lo.emplace_back(A[i], i); int h = nodes.size(); nodes.push_back({NodeType::Leaf, i, i + 1, A[i], A[i], std::vector<int>{}}); while (true) { NodeType join_tp; if (st.size() and (join_tp = findtype(st.back(), h)) != NodeType::None) { const node &vtp = nodes[st.back()]; // Insert v as the child of the top node in the stack if (join_tp == vtp.tp) { // Append child to existing Join node add_child(st.back(), h); h = st.back(); st.pop_back(); } else { // Make new join node (with exactly two children) // TODO: Refactor here nodes.push_back({join_tp, std::min(vtp.L, nodes[h].L), std::max(vtp.R, nodes[h].R), std::min(vtp.mini, nodes[h].mini), std::max(vtp.maxi, nodes[h].maxi), {st.back(), h}}); h = nodes.size() - 1; st.pop_back(); } } else if (segtree.prod(0, i + 1 - nodes[h].sz()) == 0) { // Make Cut node // TODO: Refactor here int sz = nodes[h].sz(), L = nodes[h].L, R = nodes[h].R, maxi = nodes[h].maxi, mini = nodes[h].mini; int j; std::vector<int> new_ch{h}; do { int j = st.back(); st.pop_back(); new_ch.push_back(j); node &n = nodes[j]; sz += n.sz(), L = n.L, maxi = std::max(maxi, n.maxi), mini = std::min(mini, n.mini); } while (maxi - mini + 1 != sz); std::reverse(new_ch.begin(), new_ch.end()); nodes.push_back({NodeType::Cut, L, R, mini, maxi, new_ch}); h = nodes.size() - 1; } else { break; } } st.push_back(h); segtree.add(0, i + 1, -1); } assert(st.size() == 1); root = st[0]; } void to_DOT(std::string filename = "") const { if (filename.empty()) filename = "permutation_tree_" + std::to_string(A.size()) + ".DOT"; std::ofstream ss(filename); ss << "digraph{\n"; int nleaf = 0; for (int i = 0; i < int(nodes.size()); i++) { ss << i << "[\n"; std::string lbl; if (nodes[i].tp == NodeType::Leaf) { lbl = "A[" + std::to_string(nleaf) + "] = " + std::to_string(A[nleaf]), nleaf++; } else { lbl += std::string(nodes[i].tp == NodeType::Cut ? "Cut" : "Join") + "\\n"; lbl += "[" + std::to_string(nodes[i].L) + ", " + std::to_string(nodes[i].R) + ")"; } ss << "label = \"" << lbl << "\",\n"; ss << "]\n"; for (const auto &ch : nodes[i].child) { ss << i << " -> " << ch << ";\n"; } } ss << "{rank = same;"; for (int i = 0; i < int(nodes.size()); i++) { if (nodes[i].tp == NodeType::Leaf) ss << ' ' << i << ';'; } ss << "}\n"; ss << "}\n"; ss.close(); return; } }; #include <atcoder/modint> using mint = atcoder::modint998244353; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int N, K; cin >> N >> K; vector<int> P(N); for (auto &x : P) cin >> x; for (auto &x : P) x--; permutation_tree tree(P); vector dp(K + 1, vector<mint>(N + 1)); dp[0][0] = 1; auto rec = [&](auto &&self, int now) -> void { auto &v = tree.nodes[now]; if (v.tp == permutation_tree::NodeType::Cut or v.tp == permutation_tree::NodeType::Leaf) { REP(k, K) dp[k + 1][v.R] += dp[k][v.L]; } vector<mint> sum(K); for (auto ch : v.child) { self(self, ch); if (v.tp == permutation_tree::NodeType::JoinAsc or v.tp == permutation_tree::JoinDesc) { for (int k = 0; k < K; ++k) { dp[k + 1][tree.nodes[ch].R] += sum[k]; sum[k] += dp[k][tree.nodes[ch].L]; } } } }; rec(rec, tree.root); for (int i = 1; i <= K; i++) cout << dp[i][N].val() << '\n'; }