結果
問題 | No.1720 Division Permutation |
ユーザー |
![]() |
提出日時 | 2021-10-24 11:35:02 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 179 ms / 4,000 ms |
コード長 | 15,134 bytes |
コンパイル時間 | 2,380 ms |
コンパイル使用メモリ | 197,864 KB |
実行使用メモリ | 25,512 KB |
最終ジャッジ日時 | 2024-10-01 22:44:30 |
合計ジャッジ時間 | 11,137 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 60 |
ソースコード
#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;using lint = long long;using pint = pair<int, int>;using plint = pair<lint, lint>;struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;#define ALL(x) (x).begin(), (x).end()#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, typename V>void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.maxiesize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; }int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }template <typename T> vector<T> sort_unique(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end());return vec; }template <typename T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); }template <typename T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); }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 >= 201703Ltemplate <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); returnis; }template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }#endiftemplate <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_LOCALconst 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-originRangeAddRangeMin<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 stackif (join_tp == vtp.tp) {// Append child to existing Join nodeadd_child(st.back(), h);h = st.back();st.pop_back();} else {// Make new join node (with exactly two children)// TODO: Refactor herenodes.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 hereint 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;// Utility functions for atcoder::static_modint<md>template <int md> std::istream &operator>>(std::istream &is, atcoder::static_modint<md> &x) {long long t;return is >> t, x = t, is;}template <int md> std::ostream &operator<<(std::ostream &os, const atcoder::static_modint<md> &x) {return os << x.val();}// atcoder::static_modint<P>, P: prime numbertemplate <typename modint> struct acl_fac {std::vector<modint> facs, facinvs;acl_fac(int N) {assert(-1 <= N and N < modint::mod());facs.resize(N + 1, 1);for (int i = 1; i <= N; i++) facs[i] = facs[i - 1] * i;facinvs.assign(N + 1, facs.back().inv());for (int i = N; i > 0; i--) facinvs[i - 1] = facinvs[i] * i;}modint ncr(int n, int r) const {if (n < 0 or r < 0 or n < r) return 0;return facs[n] * facinvs[r] * facinvs[n - r];}modint operator[](int i) const { return facs[i]; }modint finv(int i) const { return facinvs[i]; }};// acl_fac<mint> fac(1000000);int main() {int N, K;cin >> N >> K;vector<int> P(N);cin >> P;for (auto &x : P) x--;permutation_tree tree(P);// tree.to_DOT();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> su(K);for (auto ch : v.child) {self(self, ch);if (v.tp == permutation_tree::NodeType::JoinAsc or v.tp == permutation_tree::JoinDesc) {IREP(k, K) {dp[k + 1][tree.nodes[ch].R] += su[k];su[k] += dp[k][tree.nodes[ch].L];}}}};rec(rec, tree.root);dbg(dp);for (int i = 1; i <= K; i++) cout << dp[i][N].val() << '\n';}