結果
| 問題 |
No.3188 K-th Lexmin
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-06-24 20:35:50 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 271 ms / 3,500 ms |
| コード長 | 12,344 bytes |
| コンパイル時間 | 4,891 ms |
| コンパイル使用メモリ | 268,812 KB |
| 実行使用メモリ | 46,508 KB |
| 最終ジャッジ日時 | 2025-06-24 20:36:13 |
| 合計ジャッジ時間 | 22,614 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 47 |
ソースコード
#ifndef HIDDEN_IN_VS // 折りたたみ用
// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS
// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;
// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9e18(int は -2^31 ~ 2^31 = 2e9)
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
// 定数の定義
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定
// 汎用関数の定義
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod
// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif // 折りたたみ用
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
using mint = modint998244353;
//using mint = static_modint<(int)1e9 + 7>;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
int mute_dump = 0;
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_math(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE の代わりに MLE を出す
#endif
//【suffix tree】
/*
* Suffix_tree<STR>(STR s) : O(n)
* 文字列 s[0..n) で初期化する.
*
* ll count_unique() : O(n)
* s に含まれる非空な部分文字列の種類数を返す.
*
* pii search(STR p) : O(m log σ) ?
* s[sa[i]..sa[i]+m) = p[0..m) なる i の範囲 i∈[il..ir) を返す(存在しなければ {-1, -1})
*
* dfs_all(FUNC f) : O(n β)
* 辞書順で昇順に全ブロック [x1..x2)×[y1..y2) に対し f(x1, x2, y1, y2) を呼び出す.
* ブロック [x1..x2)×[y1..y2) は sa[x1..x2) の [y1..y2) 文字目に対応する.
*
* build_dictionary() : O(n)
* 辞書機能を有効にする.
*
* pii get(ll k) : O(log n)
* 空文字列を除き辞書順で k 番目の部分文字列が s[l..r) であるとき {l, r} を返す.
* なければ {-1, -1} を返す.build_dictionary() を先に呼び出すこと.
*
* pii get_unique(ll k) : O(log n)
* 空文字列を除き辞書順で k 番目のユニークな部分文字列が s[l..r) であるとき {l, r} を返す.
* なければ {-1, -1} を返す.build_dictionary() を先に呼び出すこと.
*/
template <class STR>
class Suffix_tree {
struct Node {
int x1, x2; // sa[x1..x2) に対応するブロックであることを表す
int y1, y2; // [y1..y2) 文字目に対応するブロックであることを表す
vi ch; // 子の昇順リスト
Node(int x1, int x2, int y1, int y2) : x1(x1), x2(x2), y1(y1), y2(y2) {}
Node() : x1(-1), x2(-1), y1(-1), y2(-1) {};
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const Node& v) {
os << "x:[" << v.x1 << "," << v.x2 << ") "
<< "y:[" << v.y1 << "," << v.y2 << ") "
<< "ch:" << v.ch;
return os;
}
#endif
};
// n : 文字列の長さ
int n;
// s : 元の文字列
STR s;
// g : s の接尾辞木(0 はダミーの根)
vector<Node> g;
// id[i] : 辞書順で昇順 i 番目(ダミー含む)の頂点
vi id;
// acc[i] : 頂点 id[0..i) に対応する部分文字列の個数
vl acc;
// acc_uniq[i] : 頂点 id[0..i) に対応する部分文字列の種類数
vl acc_uniq;
// 部分木 i から辞書データを構築する.
void build_dictionary_dfs(int i) {
id.emplace_back(i);
ll dx = g[i].x2 - g[i].x1;
ll dy = g[i].y2 - g[i].y1;
acc.emplace_back(acc.back() + dx * dy);
acc_uniq.emplace_back(acc_uniq.back() + dy);
repe(ni, g[i].ch) build_dictionary_dfs(ni);
}
public:
// sa[x] : s の接尾辞のうち辞書順 x 番目のものの先頭位置
vi sa;
// 文字列 s[0..n) で初期化する.
Suffix_tree(const STR& s) : n(sz(s)), s(s) {
// verify : https://atcoder.jp/contests/abc362/tasks/abc362_g
// 自前で書くのは大変そうなので,ACL の SA と LCP を利用する.
sa = suffix_array(s);
auto lcp = lcp_array(s, sa);
lcp.insert(lcp.begin(), 0);
lcp.push_back(0);
// ダミーの根
g.emplace_back(0, n, 0, 0);
// stk : 未完成のノードを溜めておくスタック
stack<int> stk;
stk.push(0);
// デカルト木を組み上げるような感じで suffix tree を構築する.
repi(x, 0, n) {
int i = -1;
while (!stk.empty()) {
i = stk.top();
if (g[i].y2 <= lcp[x]) break;
if (g[i].y1 < lcp[x]) {
g.emplace_back(g[i].x1, x, lcp[x], g[i].y2);
g.back().ch = move(g[i].ch);
g[i].ch.push_back(sz(g) - 1);
g[i].y2 = lcp[x];
}
else {
stk.pop();
g[i].x2 = x;
}
}
if (x < n) {
g[i].ch.push_back(sz(g));
stk.push(sz(g));
g.emplace_back(x, -1, lcp[x], n - sa[x]);
}
}
}
// s に含まれる非空な部分文字列の種類数を返す.
ll count_unique() const {
// verify : https://judge.yosupo.jp/problem/number_of_substrings
ll res = 0;
repe(v, g) res += v.y2 - v.y1;
return res;
}
// s[sa[i]..sa[i]+m) = p[0..m) なる i の範囲 i∈[il..ir) を返す(存在しなければ {-1, -1})
pii search(const STR& p) {
// verify : https://codeforces.com/contest/1202/problem/E
int m = sz(p);
Assert(m > 0);
int i = 0; int j = 0;
// どの子に進めばいいかを毎回二分探索する.
while (true) {
int ok = 0, ng = sz(g[i].ch);
if (ng == 0) return { -1, -1 };
while (ng - ok > 1) {
int mid = (ok + ng) / 2;
int ic = g[i].ch[mid];
if (p[j] >= s[sa[g[ic].x1] + g[ic].y1]) ok = mid;
else ng = mid;
}
i = g[i].ch[ok];
int ws = g[i].y2 - g[i].y1;
int wp = m - j;
rep(d, min(ws, wp)) if (s[sa[g[i].x1] + g[i].y1 + d] != p[j + d]) return { -1, -1 };
if (wp <= ws) return { g[i].x1, g[i].x2 };
j += ws;
}
}
// 辞書順で昇順に全ブロック [x1..x2)×[y1..y2) に対し f(x1, x2, y1, y2) を呼び出す.
template <class FUNC>
void dfs_all(const FUNC& f, int i = 0) const {
// verify : https://atcoder.jp/contests/abc280/tasks/abc280_h
if (i != 0) f(g[i].x1, g[i].x2, g[i].y1, g[i].y2);
repe(ni, g[i].ch) dfs_all(f, ni);
}
// 辞書機能を有効にする.
void build_dictionary() {
// verify : https://yukicoder.me/problems/no/2361
id.reserve(sz(g));
acc.reserve(sz(g) + 1);
acc.emplace_back(0);
acc_uniq.reserve(sz(g) + 1);
acc_uniq.emplace_back(0);
build_dictionary_dfs(0);
}
// 空文字列を除き辞書順で k 番目の部分文字列が s[l..r) であるとき {l, r} を返す(なければ {-1, -1} を返す)
pii get(ll k) const {
// verify : https://yukicoder.me/problems/no/2361
Assert(!acc.empty());
if (k < 0 || k >= acc.back()) return { -1, -1 };
int i = ubpos(acc, k) - 1;
int v = id[i];
k -= acc[i];
int dx = g[v].x2 - g[v].x1;
return { sa[g[v].x1], sa[g[v].x1] + g[v].y1 + (int)k / dx + 1 };
}
// 空文字列を除き辞書順で k 番目のユニークな部分文字列が s[l..r) であるとき {l, r} を返す.(なければ {-1, -1} を返す)
pii get_unique(ll k) const {
// verify : https://atcoder.jp/contests/arc097/tasks/arc097_a
Assert(!acc_uniq.empty());
if (k < 0 || k >= acc_uniq.back()) return { -1, -1 };
int i = ubpos(acc_uniq, k) - 1;
int v = id[i];
k -= acc_uniq[i];
int dx = g[v].x2 - g[v].x1;
return { sa[g[v].x1], sa[g[v].x1] + g[v].y1 + (int)k / dx + 1 };
}
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const Suffix_tree& S) {
os << "[suffixes]:" << endl;
rep(i, S.n) {
os << right << setw(2) << S.sa[i] << ": ";
repi(j, S.sa[i], sz(S.s) - 1) os << S.s[j];
os << endl;
}
os << "[blocks]:" << endl;
auto f = [&](int x1, int x2, int y1, int y2) {
os << "x:[" << x1 << "," << x2 << ") ";
os << "y:[" << y1 << "," << y2 << ") ";
repi(j, S.sa[x1] + y1, S.sa[x1] + y2 - 1) os << S.s[j];
os << endl;
};
S.dfs_all(f);
return os;
}
#endif
/* f の定義の雛形
auto f = [&](int x1, int x2, int y1, int y2) {
};
S.dfs_all(f);
*/
};
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
int t;
cin >> t;
rep(hoge, t) {
int n; ll k;
cin >> n >> k;
vi a(n);
cin >> a;
Suffix_tree A(a);
A.build_dictionary();
auto [l, r] = A.get(k - 1);
repi(i, l, r - 1) cout << a[i] << " \n"[i == r - 1];
}
}