#line 1 "/home/maspy/compro/library/my_template.hpp" #if defined(LOCAL) #include #else #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include using namespace std; using ll = long long; using u32 = unsigned int; using u64 = unsigned long long; using i128 = __int128; template constexpr T infty = 0; template <> constexpr int infty = 1'000'000'000; template <> constexpr ll infty = ll(infty) * infty * 2; template <> constexpr u32 infty = infty; template <> constexpr u64 infty = infty; template <> constexpr i128 infty = i128(infty) * infty; template <> constexpr double infty = infty; template <> constexpr long double infty = infty; using pi = pair; using vi = vector; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; template using vvvvc = vector>; template using vvvvvc = vector>; template using pq = priority_queue; template using pqg = priority_queue, greater>; #define vv(type, name, h, ...) \ vector> name(h, vector(__VA_ARGS__)) #define vvv(type, name, h, w, ...) \ vector>> name( \ h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name( \ a, vector>>( \ b, vector>(c, vector(__VA_ARGS__)))) // https://trap.jp/post/1224/ #define FOR1(a) for (ll _ = 0; _ < ll(a); ++_) #define FOR2(i, a) for (ll i = 0; i < ll(a); ++i) #define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i) #define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c)) #define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i) #define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i) #define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i) #define overload4(a, b, c, d, e, ...) e #define overload3(a, b, c, d, ...) d #define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__) #define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__) #define FOR_subset(t, s) \ for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s))) #define all(x) x.begin(), x.end() #define len(x) ll(x.size()) #define elif else if #define eb emplace_back #define mp make_pair #define mt make_tuple #define fi first #define se second #define stoi stoll int popcnt(int x) { return __builtin_popcount(x); } int popcnt(u32 x) { return __builtin_popcount(x); } int popcnt(ll x) { return __builtin_popcountll(x); } int popcnt(u64 x) { return __builtin_popcountll(x); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2) int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2) int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } template T ceil(T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); } template T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); } template pair divmod(T x, U y) { T q = floor(x, y); return {q, x - q * y}; } template T SUM(const vector &A) { T sum = 0; for (auto &&a: A) sum += a; return sum; } #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define LB(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define UB(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define UNIQUE(x) \ sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit() template T POP(deque &que) { T a = que.front(); que.pop_front(); return a; } template T POP(pq &que) { T a = que.top(); que.pop(); return a; } template T POP(pqg &que) { assert(!que.empty()); T a = que.top(); que.pop(); return a; } template T POP(vc &que) { assert(!que.empty()); T a = que.back(); que.pop_back(); return a; } template ll binary_search(F check, ll ok, ll ng, bool check_ok = true) { if (check_ok) assert(check(ok)); while (abs(ok - ng) > 1) { auto x = (ng + ok) / 2; tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x)); } return ok; } template double binary_search_real(F check, double ok, double ng, int iter = 100) { FOR(iter) { double x = (ok + ng) / 2; tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x)); } return (ok + ng) / 2; } template inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } // ? は -1 vc s_to_vi(const string &S, char first_char) { vc A(S.size()); FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); } return A; } template vector cumsum(vector &A, int off = 1) { int N = A.size(); vector B(N + 1); FOR(i, N) { B[i + 1] = B[i] + A[i]; } if (off == 0) B.erase(B.begin()); return B; } // stable sort template vector argsort(const vector &A) { vector ids(len(A)); iota(all(ids), 0); sort(all(ids), [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); }); return ids; } // A[I[0]], A[I[1]], ... template vc rearrange(const vc &A, const vc &I) { vc B(len(I)); FOR(i, len(I)) B[i] = A[I[i]]; return B; } #endif #line 1 "/home/maspy/compro/library/other/io.hpp" // based on yosupo's fastio #include namespace fastio { #define FASTIO // クラスが read(), print() を持っているかを判定するメタ関数 struct has_write_impl { template static auto check(T &&x) -> decltype(x.write(), std::true_type{}); template static auto check(...) -> std::false_type; }; template class has_write : public decltype(has_write_impl::check(std::declval())) { }; struct has_read_impl { template static auto check(T &&x) -> decltype(x.read(), std::true_type{}); template static auto check(...) -> std::false_type; }; template class has_read : public decltype(has_read_impl::check(std::declval())) {}; struct Scanner { FILE *fp; char line[(1 << 15) + 1]; size_t st = 0, ed = 0; void reread() { memmove(line, line + st, ed - st); ed -= st; st = 0; ed += fread(line + ed, 1, (1 << 15) - ed, fp); line[ed] = '\0'; } bool succ() { while (true) { if (st == ed) { reread(); if (st == ed) return false; } while (st != ed && isspace(line[st])) st++; if (st != ed) break; } if (ed - st <= 50) { bool sep = false; for (size_t i = st; i < ed; i++) { if (isspace(line[i])) { sep = true; break; } } if (!sep) reread(); } return true; } template ::value, int> = 0> bool read_single(T &ref) { if (!succ()) return false; while (true) { size_t sz = 0; while (st + sz < ed && !isspace(line[st + sz])) sz++; ref.append(line + st, sz); st += sz; if (!sz || st != ed) break; reread(); } return true; } template ::value, int> = 0> bool read_single(T &ref) { if (!succ()) return false; bool neg = false; if (line[st] == '-') { neg = true; st++; } ref = T(0); while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); } if (neg) ref = -ref; return true; } template ::value>::type * = nullptr> inline bool read_single(T &x) { x.read(); return true; } bool read_single(double &ref) { string s; if (!read_single(s)) return false; ref = std::stod(s); return true; } bool read_single(char &ref) { string s; if (!read_single(s) || s.size() != 1) return false; ref = s[0]; return true; } template bool read_single(vector &ref) { for (auto &d: ref) { if (!read_single(d)) return false; } return true; } template bool read_single(pair &p) { return (read_single(p.first) && read_single(p.second)); } template void read_single_tuple(T &t) { if constexpr (N < std::tuple_size::value) { auto &x = std::get(t); read_single(x); read_single_tuple(t); } } template bool read_single(tuple &tpl) { read_single_tuple(tpl); return true; } void read() {} template void read(H &h, T &... t) { bool f = read_single(h); assert(f); read(t...); } Scanner(FILE *fp) : fp(fp) {} }; struct Printer { Printer(FILE *_fp) : fp(_fp) {} ~Printer() { flush(); } static constexpr size_t SIZE = 1 << 15; FILE *fp; char line[SIZE], small[50]; size_t pos = 0; void flush() { fwrite(line, 1, pos, fp); pos = 0; } void write(const char val) { if (pos == SIZE) flush(); line[pos++] = val; } template ::value, int> = 0> void write(T val) { if (pos > (1 << 15) - 50) flush(); if (val == 0) { write('0'); return; } if (val < 0) { write('-'); val = -val; // todo min } size_t len = 0; while (val) { small[len++] = char(0x30 | (val % 10)); val /= 10; } for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; } pos += len; } void write(const string s) { for (char c: s) write(c); } void write(const char *s) { size_t len = strlen(s); for (size_t i = 0; i < len; i++) write(s[i]); } void write(const double x) { ostringstream oss; oss << fixed << setprecision(15) << x; string s = oss.str(); write(s); } void write(const long double x) { ostringstream oss; oss << fixed << setprecision(15) << x; string s = oss.str(); write(s); } template ::value>::type * = nullptr> inline void write(T x) { x.write(); } template void write(const vector val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) write(' '); write(val[i]); } } template void write(const pair val) { write(val.first); write(' '); write(val.second); } template void write_tuple(const T t) { if constexpr (N < std::tuple_size::value) { if constexpr (N > 0) { write(' '); } const auto x = std::get(t); write(x); write_tuple(t); } } template bool write(tuple tpl) { write_tuple(tpl); return true; } template void write(const array val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) write(' '); write(val[i]); } } void write(i128 val) { string s; bool negative = 0; if (val < 0) { negative = 1; val = -val; } while (val) { s += '0' + int(val % 10); val /= 10; } if (negative) s += "-"; reverse(all(s)); if (len(s) == 0) s = "0"; write(s); } }; Scanner scanner = Scanner(stdin); Printer printer = Printer(stdout); void flush() { printer.flush(); } void print() { printer.write('\n'); } template void print(Head &&head, Tail &&... tail) { printer.write(head); if (sizeof...(Tail)) printer.write(' '); print(forward(tail)...); } void read() {} template void read(Head &head, Tail &... tail) { scanner.read(head); read(tail...); } } // namespace fastio using fastio::print; using fastio::flush; using fastio::read; #define INT(...) \ int __VA_ARGS__; \ read(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ read(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ read(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ read(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ read(__VA_ARGS__) #define VEC(type, name, size) \ vector name(size); \ read(name) #define VV(type, name, h, w) \ vector> name(h, vector(w)); \ read(name) void YES(bool t = 1) { print(t ? "YES" : "NO"); } void NO(bool t = 1) { YES(!t); } void Yes(bool t = 1) { print(t ? "Yes" : "No"); } void No(bool t = 1) { Yes(!t); } void yes(bool t = 1) { print(t ? "yes" : "no"); } void no(bool t = 1) { yes(!t); } #line 3 "main.cpp" #line 2 "/home/maspy/compro/library/string/suffix_array.hpp" #line 2 "/home/maspy/compro/library/alg/monoid/min.hpp" template struct Monoid_Min { using X = E; using value_type = X; static constexpr X op(const X &x, const X &y) noexcept { return min(x, y); } static constexpr X unit() { return infty; } static constexpr bool commute = true; }; #line 1 "/home/maspy/compro/library/ds/sparse_table/sparse_table.hpp" // 冪等なモノイドであることを仮定。disjoint sparse table より x 倍高速 template struct Sparse_Table { using MX = Monoid; using X = typename MX::value_type; int n, log; vvc dat; Sparse_Table() {} Sparse_Table(int n) { build(n); } template Sparse_Table(int n, F f) { build(n, f); } Sparse_Table(const vc& v) { build(v); } void build(int m) { build(m, [](int i) -> X { return MX::unit(); }); } void build(const vc& v) { build(len(v), [&](int i) -> X { return v[i]; }); } template void build(int m, F f) { n = m, log = 1; while ((1 << log) < n) ++log; dat.resize(log); dat[0].resize(n); FOR(i, n) dat[0][i] = f(i); FOR(i, log - 1) { dat[i + 1].resize(len(dat[i]) - (1 << i)); FOR(j, len(dat[i]) - (1 << i)) { dat[i + 1][j] = MX::op(dat[i][j], dat[i][j + (1 << i)]); } } } X prod(int L, int R) { if (L == R) return MX::unit(); if (R == L + 1) return dat[0][L]; int k = topbit(R - L - 1); return MX::op(dat[k][L], dat[k][R - (1 << k)]); } template int max_right(const F check, int L) { assert(0 <= L && L <= n && check(MX::unit())); if (L == n) return n; int ok = L, ng = n + 1; while (ok + 1 < ng) { int k = (ok + ng) / 2; bool bl = check(prod(L, k)); if (bl) ok = k; if (!bl) ng = k; } return ok; } template int min_left(const F check, int R) { assert(0 <= R && R <= n && check(MX::unit())); if (R == 0) return 0; int ok = R, ng = -1; while (ng + 1 < ok) { int k = (ok + ng) / 2; bool bl = check(prod(k, R)); if (bl) ok = k; if (!bl) ng = k; } return ok; } }; #line 5 "/home/maspy/compro/library/string/suffix_array.hpp" // 辞書順 i 番目の suffix が j 文字目始まりであるとき、 // SA[i] = j, ISA[j] = i struct Suffix_Array { vc SA; vc ISA; vc LCP; Sparse_Table> seg; // DisjointSparse> seg; Suffix_Array(string& s, bool lcp_query = false) { char first = 127, last = 0; for (auto&& c: s) { chmin(first, c); chmax(last, c); } SA = calc_suffix_array(s, first, last); calc_LCP(s); if (lcp_query) seg.build(LCP); } Suffix_Array(vc& s, bool lcp_query = false) { SA = calc_suffix_array(s); calc_LCP(s); if (lcp_query) seg.build(LCP); } // lcp(S[i:], S[j:]) int lcp(int i, int j) { int n = len(SA); if (i == n || j == n) return 0; if (i == j) return n - i; i = ISA[i], j = ISA[j]; if (i > j) swap(i, j); return seg.prod(i, j); } private: void induced_sort(const vc& vect, int val_range, vc& SA, const vc& sl, const vc& lms_idx) { vc l(val_range, 0), r(val_range, 0); for (int c: vect) { if (c + 1 < val_range) ++l[c + 1]; ++r[c]; } partial_sum(l.begin(), l.end(), l.begin()); partial_sum(r.begin(), r.end(), r.begin()); fill(SA.begin(), SA.end(), -1); for (int i = (int)lms_idx.size() - 1; i >= 0; --i) SA[--r[vect[lms_idx[i]]]] = lms_idx[i]; for (int i: SA) if (i >= 1 && sl[i - 1]) SA[l[vect[i - 1]]++] = i - 1; fill(r.begin(), r.end(), 0); for (int c: vect) ++r[c]; partial_sum(r.begin(), r.end(), r.begin()); for (int k = (int)SA.size() - 1, i = SA[k]; k >= 1; --k, i = SA[k]) if (i >= 1 && !sl[i - 1]) { SA[--r[vect[i - 1]]] = i - 1; } } vc SA_IS(const vc& vect, int val_range) { const int n = vect.size(); vc SA(n), lms_idx; vc sl(n); sl[n - 1] = false; for (int i = n - 2; i >= 0; --i) { sl[i] = (vect[i] > vect[i + 1] || (vect[i] == vect[i + 1] && sl[i + 1])); if (sl[i] && !sl[i + 1]) lms_idx.push_back(i + 1); } reverse(lms_idx.begin(), lms_idx.end()); induced_sort(vect, val_range, SA, sl, lms_idx); vc new_lms_idx(lms_idx.size()), lms_vec(lms_idx.size()); for (int i = 0, k = 0; i < n; ++i) if (!sl[SA[i]] && SA[i] >= 1 && sl[SA[i] - 1]) { new_lms_idx[k++] = SA[i]; } int cur = 0; SA[n - 1] = cur; for (size_t k = 1; k < new_lms_idx.size(); ++k) { int i = new_lms_idx[k - 1], j = new_lms_idx[k]; if (vect[i] != vect[j]) { SA[j] = ++cur; continue; } bool flag = false; for (int a = i + 1, b = j + 1;; ++a, ++b) { if (vect[a] != vect[b]) { flag = true; break; } if ((!sl[a] && sl[a - 1]) || (!sl[b] && sl[b - 1])) { flag = !((!sl[a] && sl[a - 1]) && (!sl[b] && sl[b - 1])); break; } } SA[j] = (flag ? ++cur : cur); } for (size_t i = 0; i < lms_idx.size(); ++i) lms_vec[i] = SA[lms_idx[i]]; if (cur + 1 < (int)lms_idx.size()) { auto lms_SA = SA_IS(lms_vec, cur + 1); for (size_t i = 0; i < lms_idx.size(); ++i) { new_lms_idx[i] = lms_idx[lms_SA[i]]; } } induced_sort(vect, val_range, SA, sl, new_lms_idx); return SA; } vc calc_suffix_array(const string& s, const char first = 'a', const char last = 'z') { vc vect(s.size() + 1); copy(begin(s), end(s), begin(vect)); for (auto& x: vect) x -= (int)first - 1; vect.back() = 0; auto ret = SA_IS(vect, (int)last - (int)first + 2); ret.erase(ret.begin()); return ret; } vc calc_suffix_array(const vc& s) { vc ss = s; UNIQUE(ss); vc vect(s.size() + 1); copy(all(s), vect.begin()); for (auto& x: vect) x = LB(ss, x) + 1; vect.back() = 0; auto ret = SA_IS(vect, MAX(vect) + 2); ret.erase(ret.begin()); return ret; } template void calc_LCP(const STRING& s) { int n = s.size(), k = 0; ISA.resize(n); LCP.resize(n); for (int i = 0; i < n; i++) ISA[SA[i]] = i; for (int i = 0; i < n; i++, k ? k-- : 0) { if (ISA[i] == n - 1) { k = 0; continue; } int j = SA[ISA[i] + 1]; while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++; LCP[ISA[i]] = k; } LCP.resize(n - 1); } }; #line 1 "/home/maspy/compro/library/string/suffix_tree.hpp" #line 2 "/home/maspy/compro/library/alg/monoid/min_idx.hpp" template struct Monoid_Min_Idx { using value_type = pair; using X = value_type; static constexpr bool is_small(const X& x, const X& y) { if (x.fi < y.fi) return true; if (x.fi > y.fi) return false; return (tie_is_left ? (x.se < y.se) : (x.se >= y.se)); } static X op(X x, X y) { return (is_small(x, y) ? x : y); } static constexpr X unit() { return {infty, -1}; } static constexpr bool commute = true; }; #line 2 "/home/maspy/compro/library/ds/segtree/segtree.hpp" template struct SegTree { using MX = Monoid; using X = typename MX::value_type; using value_type = X; vc dat; int n, log, size; SegTree() {} SegTree(int n) { build(n); } template SegTree(int n, F f) { build(n, f); } SegTree(const vc& v) { build(v); } void build(int m) { build(m, [](int i) -> X { return MX::unit(); }); } void build(const vc& v) { build(len(v), [&](int i) -> X { return v[i]; }); } template void build(int m, F f) { n = m, log = 1; while ((1 << log) < n) ++log; size = 1 << log; dat.assign(size << 1, MX::unit()); FOR(i, n) dat[size + i] = f(i); FOR_R(i, 1, size) update(i); } X get(int i) { return dat[size + i]; } vc get_all() { return {dat.begin() + size, dat.begin() + size + n}; } void update(int i) { dat[i] = Monoid::op(dat[2 * i], dat[2 * i + 1]); } void set(int i, const X& x) { assert(i < n); dat[i += size] = x; while (i >>= 1) update(i); } void multiply(int i, const X& x) { assert(i < n); i += size; dat[i] = Monoid::op(dat[i], x); while (i >>= 1) update(i); } X prod(int L, int R) { assert(0 <= L && L <= R && R <= n); X vl = Monoid::unit(), vr = Monoid::unit(); L += size, R += size; while (L < R) { if (L & 1) vl = Monoid::op(vl, dat[L++]); if (R & 1) vr = Monoid::op(dat[--R], vr); L >>= 1, R >>= 1; } return Monoid::op(vl, vr); } X prod_all() { return dat[1]; } template int max_right(F check, int L) { assert(0 <= L && L <= n && check(Monoid::unit())); if (L == n) return n; L += size; X sm = Monoid::unit(); do { while (L % 2 == 0) L >>= 1; if (!check(Monoid::op(sm, dat[L]))) { while (L < size) { L = 2 * L; if (check(Monoid::op(sm, dat[L]))) { sm = Monoid::op(sm, dat[L++]); } } return L - size; } sm = Monoid::op(sm, dat[L++]); } while ((L & -L) != L); return n; } template int min_left(F check, int R) { assert(0 <= R && R <= n && check(Monoid::unit())); if (R == 0) return 0; R += size; X sm = Monoid::unit(); do { --R; while (R > 1 && (R % 2)) R >>= 1; if (!check(Monoid::op(dat[R], sm))) { while (R < size) { R = 2 * R + 1; if (check(Monoid::op(dat[R], sm))) { sm = Monoid::op(dat[R--], sm); } } return R + 1 - size; } sm = Monoid::op(dat[R], sm); } while ((R & -R) != R); return 0; } // prod_{l<=i= r) break; if (l & 1) { x = Monoid::op(x, dat[(size >> k) + ((l++) ^ xor_val)]); } if (r & 1) { x = Monoid::op(x, dat[(size >> k) + ((--r) ^ xor_val)]); } l /= 2, r /= 2, xor_val /= 2; } return x; } }; #line 2 "/home/maspy/compro/library/graph/base.hpp" template struct Edge { int frm, to; T cost; int id; }; template struct Graph { int N, M; using cost_type = T; using edge_type = Edge; vector edges; vector indptr; vector csr_edges; vc vc_deg, vc_indeg, vc_outdeg; bool prepared; class OutgoingEdges { public: OutgoingEdges(const Graph* G, int l, int r) : G(G), l(l), r(r) {} const edge_type* begin() const { if (l == r) { return 0; } return &G->csr_edges[l]; } const edge_type* end() const { if (l == r) { return 0; } return &G->csr_edges[r]; } private: const Graph* G; int l, r; }; bool is_prepared() { return prepared; } constexpr bool is_directed() { return directed; } Graph() : N(0), M(0), prepared(0) {} Graph(int N) : N(N), M(0), prepared(0) {} void build(int n) { N = n, M = 0; prepared = 0; edges.clear(); indptr.clear(); csr_edges.clear(); vc_deg.clear(); vc_indeg.clear(); vc_outdeg.clear(); } void add(int frm, int to, T cost = 1, int i = -1) { assert(!prepared); assert(0 <= frm && 0 <= to && to < N); if (i == -1) i = M; auto e = edge_type({frm, to, cost, i}); edges.eb(e); ++M; } // wt, off void read_tree(bool wt = false, int off = 1) { read_graph(N - 1, wt, off); } void read_graph(int M, bool wt = false, int off = 1) { for (int m = 0; m < M; ++m) { INT(a, b); a -= off, b -= off; if (!wt) { add(a, b); } else { T c; read(c); add(a, b, c); } } build(); } void build() { assert(!prepared); prepared = true; indptr.assign(N + 1, 0); for (auto&& e: edges) { indptr[e.frm + 1]++; if (!directed) indptr[e.to + 1]++; } for (int v = 0; v < N; ++v) { indptr[v + 1] += indptr[v]; } auto counter = indptr; csr_edges.resize(indptr.back() + 1); for (auto&& e: edges) { csr_edges[counter[e.frm]++] = e; if (!directed) csr_edges[counter[e.to]++] = edge_type({e.to, e.frm, e.cost, e.id}); } } OutgoingEdges operator[](int v) const { assert(prepared); return {this, indptr[v], indptr[v + 1]}; } vc deg_array() { if (vc_deg.empty()) calc_deg(); return vc_deg; } pair, vc> deg_array_inout() { if (vc_indeg.empty()) calc_deg_inout(); return {vc_indeg, vc_outdeg}; } int deg(int v) { if (vc_deg.empty()) calc_deg(); return vc_deg[v]; } int in_deg(int v) { if (vc_indeg.empty()) calc_deg_inout(); return vc_indeg[v]; } int out_deg(int v) { if (vc_outdeg.empty()) calc_deg_inout(); return vc_outdeg[v]; } void debug() { print("Graph"); if (!prepared) { print("frm to cost id"); for (auto&& e: edges) print(e.frm, e.to, e.cost, e.id); } else { print("indptr", indptr); print("frm to cost id"); FOR(v, N) for (auto&& e: (*this)[v]) print(e.frm, e.to, e.cost, e.id); } } vc new_idx; vc used_e; // G における頂点 V[i] が、新しいグラフで i になるようにする // {G, es} pair, vc> rearrange(vc V) { if (len(new_idx) != N) new_idx.assign(N, -1); if (len(used_e) != M) used_e.assign(M, 0); int n = len(V); FOR(i, n) new_idx[V[i]] = i; Graph G(n); vc es; FOR(i, n) { for (auto&& e: (*this)[V[i]]) { if (used_e[e.id]) continue; int a = e.frm, b = e.to; if (new_idx[a] != -1 && new_idx[b] != -1) { used_e[e.id] = 1; G.add(new_idx[a], new_idx[b], e.cost); es.eb(e.id); } } } FOR(i, n) new_idx[V[i]] = -1; for (auto&& eid: es) used_e[eid] = 0; G.build(); return {G, es}; } private: void calc_deg() { assert(vc_deg.empty()); vc_deg.resize(N); for (auto&& e: edges) vc_deg[e.frm]++, vc_deg[e.to]++; } void calc_deg_inout() { assert(vc_indeg.empty()); vc_indeg.resize(N); vc_outdeg.resize(N); for (auto&& e: edges) { vc_indeg[e.to]++, vc_outdeg[e.frm]++; } } }; #line 6 "/home/maspy/compro/library/string/suffix_tree.hpp" // https://twitter.com/maspy_stars/status/1565901414236205057?s=20&t=S2Tu6ayozHcakxai8dmh4g // 各ノードは、suffix array での長方形領域と見なして、 // グラフおよび、領域データを作る。 // sample: test/my_test/suffix_tree.test.cpp pair, vc>> suffix_tree( Suffix_Array& X) { auto SA = X.SA; auto ISA = X.ISA; auto LCP = X.LCP; int N = len(SA); using Mono = Monoid_Min_Idx; SegTree seg(N - 1, [&](int i) -> Mono::X { return {LCP[i], i}; }); using T = tuple; vc dat; dat.eb(0, N, 0, 0); vc> edges; auto dfs = [&](auto& dfs, int p, int l, int r, int h) -> void { if (r == l + 1) { int i = SA[l]; int sz = N - i; if (h == sz) return; int k = len(dat); dat.eb(l, l + 1, h, sz); edges.eb(p, k); return; } auto [lcp, i] = seg.prod(l, r - 1); if (lcp == h) { dfs(dfs, p, l, i + 1, h); dfs(dfs, p, i + 1, r, h); return; } int k = len(dat); dat.eb(l, r, h, lcp); edges.eb(p, k); dfs(dfs, k, l, r, lcp); }; dfs(dfs, 0, 0, N, 0); Graph G(len(dat)); for (auto&& [a, b]: edges) G.add(a, b); G.build(); return {G, dat}; } #line 7 "main.cpp" void solve() { LL(N, Q); STR(S); Suffix_Array X(S); VEC(pi, query, Q); for (auto&& [a, b]: query) --a; // 場所 -> 長さ, クエリ番号 vvc dat(N); FOR(q, Q) { auto [a, b] = query[q]; ll n = b - a; dat[X.ISA[a]].eb(n, q); } FOR(i, N) { sort(all(dat[i])); reverse(all(dat[i])); } SegTree> seg(N); auto upd = [&](int i) -> void { if (dat[i].empty()) seg.set(i, {infty, -1}); else seg.set(i, {dat[i].back().fi, i}); }; FOR(i, N) upd(i); vi ANS(Q); auto [G, rect] = suffix_tree(X); ll vis = 0; auto dfs = [&](auto& dfs, int v) -> void { auto [L, R, a, b] = rect[v]; ++a, ++b; // 文字列長 while (1) { auto [mi, idx] = seg.prod(L, R); if (mi > b) break; auto [sz, qid] = POP(dat[idx]); assert(sz == mi); upd(idx); ll ans = vis; ans += (mi - a) * (R - L); ANS[qid] = ans; } vis += (R - L) * (b - a); for (auto&& e: G[v]) dfs(dfs, e.to); }; dfs(dfs, 0); for (auto&& x: ANS) print(x); } signed main() { int T = 1; // INT(T); FOR(T) solve(); return 0; }