This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/number_of_substrings"
#include "my_template.hpp"
#include "other/io.hpp"
#include "string/suffix_array.hpp"
void solve() {
STR(S);
ll N = len(S);
Suffix_Array sa(S);
ll ANS = N * (N + 1) / 2;
ANS -= SUM<ll>(sa.LCP);
print(ANS);
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << setprecision(15);
solve();
return 0;
}#line 1 "test/2_library_checker/string/number_of_substrings.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/number_of_substrings"
#line 1 "my_template.hpp"
#if defined(USE_PCH)
#include <my_template_compiled.hpp>
#else
#if defined(__GNUC__)
#include <bits/allocator.h>
#pragma GCC optimize("Ofast,unroll-loops")
// 環境によってはコンパイル成功かつ実行時エラー
#pragma GCC target("avx2,popcnt")
#endif
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
using ll = long long;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;
template <class>
constexpr bool dependent_false = false;
template <class T>
constexpr T infty = [] {
static_assert(dependent_false<T>, "infty<T> is not defined");
return T{};
}();
template <>
constexpr int infty<int> = 1'010'000'000;
template <>
constexpr ll infty<ll> = 2'020'000'000'000'000'000;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000;
template <>
constexpr double infty<double> = infty<i128>;
template <>
constexpr long double infty<long double> = infty<i128>;
using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using pq_max = priority_queue<T>;
template <class T>
using pq_min = priority_queue<T, vector<T>, greater<T>>;
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) \
vector<vector<vector<type>>> name( \
h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...) \
vector<vector<vector<vector<type>>>> name( \
a, vector<vector<vector<type>>>( \
b, vector<vector<type>>(c, vector<type>(__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 = ll(a) - 1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = ll(a) - 1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = ll(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 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
// require y > 0
template <typename T>
T floor(T x, T y) {
return x / y - (x % y < 0);
}
// require y > 0
template <typename T>
T ceil(T x, T y) {
return (x / y) + (x % y > 0);
}
// require y > 0
template <typename T>
T bmod(T x, T y) {
T r = x % y;
return (r < 0 ? r + y : r);
}
// require y > 0
template <typename T>
pair<T, T> divmod(T x, T y) {
T q = x / y, r = x % y;
if (r < 0) --q, r += y;
return {q, r};
}
constexpr auto TEN = [] {
array<u64, 20> A{};
A[0] = 1;
for (int i = 1; i < 20; ++i) A[i] = 10 * A[i - 1];
return A;
}();
template <typename T, typename U>
T SUM(const U &A) {
return std::accumulate(A.begin(), A.end(), T{});
}
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
template <class C, class T>
inline long long LB(const C &c, const T &x) {
return lower_bound(c.begin(), c.end(), x) - c.begin();
}
template <class C, class T>
inline long long UB(const C &c, const T &x) {
return upper_bound(c.begin(), c.end(), x) - c.begin();
}
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
template <typename T>
T POP(deque<T> &que) {
T a = que.front();
que.pop_front();
return a;
}
template <class T, class Container, class Compare>
T POP(priority_queue<T, Container, Compare> &que) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(vc<T> &que) {
T a = que.back();
que.pop_back();
return a;
}
template <typename F>
i128 binary_search(F check, i128 ok, i128 ng, bool check_ok = true) {
if (check_ok) assert(check(ok));
while (1) {
i128 x = (ok + ng) / 2;
if (x == ok || x == ng) break;
(check(x) ? ok : ng) = x;
}
return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
FOR(iter) {
double x = (ok + ng) / 2;
(check(x) ? ok : ng) = x;
}
return (ok + ng) / 2;
}
template <class T, class S>
inline bool chmax(T &a, const S &b) {
T c = max<T>(a, b);
bool changed = (c != a);
a = c;
return changed;
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
T c = min<T>(a, b);
bool changed = (c != a);
a = c;
return changed;
}
// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
vc<int> A(S.size());
FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
return A;
}
template <typename T, typename U>
vc<T> cumsum(const vc<U> &A, int off = 1) {
int N = A.size();
vc<T> 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 <typename T>
vc<int> argsort(const vc<T> &A) {
vc<int> 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 <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
vc<T> B(len(I));
FOR(i, len(I)) B[i] = A[I[i]];
return B;
}
template <typename T, typename... Vectors>
void concat(vc<T> &first, const Vectors &...others) {
first.reserve(first.size() + (others.size() + ... + 0));
(first.insert(first.end(), others.begin(), others.end()), ...);
}
// i128
template <class T, enable_if_t<is_same_v<T, i128>, int> = 0>
constexpr i128 abs(T x) {
return x < 0 ? -x : x;
}
constexpr i128 gcd(i128 a, i128 b) {
while (b != 0) {
i128 c = a % b;
a = b, b = c;
}
return abs(a);
}
#endif
#line 1 "other/io.hpp"
#define FASTIO
// https://judge.yosupo.jp/submission/21623
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf
uint32_t pil = 0, pir = 0, por = 0;
bool input_eof = false;
template <class T>
constexpr bool is_signed_integer_v = is_signed_v<T> || is_same_v<T, i128>;
template <class T>
struct unsigned_integer {
using type = make_unsigned_t<T>;
};
template <>
struct unsigned_integer<i128> {
using type = u128;
};
template <>
struct unsigned_integer<u128> {
using type = u128;
};
template <class T>
using unsigned_integer_t = typename unsigned_integer<T>::type;
[[noreturn]] inline void input_error(const char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(EXIT_FAILURE);
}
struct Pre {
char num[10000][4];
constexpr Pre() : num() {
for (int i = 0; i < 10000; i++) {
int n = i;
for (int j = 3; j >= 0; j--) {
num[i][j] = n % 10 | '0';
n /= 10;
}
}
}
} constexpr pre;
inline void load() {
uint32_t n = pir - pil;
memmove(ibuf, ibuf + pil, n);
pil = 0;
pir = n;
if (input_eof) return;
pir += fread(ibuf + pir, 1, SZ - pir, stdin);
if (ferror(stdin)) input_error("fastio: input error");
if (feof(stdin)) {
input_eof = true;
// Allows the last token to end exactly at EOF without a trailing
// whitespace.
if (pir < SZ) ibuf[pir++] = '\n';
}
}
inline char get_char() {
if (pil == pir) {
load();
if (pil == pir) input_error("fastio: unexpected EOF");
}
return ibuf[pil++];
}
inline void flush() {
fwrite(obuf, 1, por, stdout);
por = 0;
}
void rd(char &c) {
do c = get_char();
while (isspace(static_cast<unsigned char>(c)));
}
void rd(string &x) {
x.clear();
char c;
do c = get_char();
while (isspace(static_cast<unsigned char>(c)));
do {
x += c;
c = get_char();
} while (!isspace(static_cast<unsigned char>(c)));
}
template <typename T>
void rd_real(T &x) {
string s;
rd(s);
x = stod(s);
}
template <typename T>
void rd_integer_slow(T &x) {
char c;
do c = get_char();
while (c < '-');
bool minus = 0;
if constexpr (is_signed_integer_v<T>) {
if (c == '-') {
minus = 1, c = get_char();
}
}
x = 0;
assert('0' <= c && c <= '9');
while ('0' <= c && c <= '9') {
x = x * 10 + (c & 15), c = get_char();
}
assert(isspace(static_cast<unsigned char>(c)));
if constexpr (is_signed_integer_v<T>) {
if (minus) x = -x;
}
}
template <typename T>
void rd_integer(T &x) {
if (pil + 100 > pir) {
load();
if (pil + 100 > pir) {
rd_integer_slow(x);
return;
}
}
char c;
do c = ibuf[pil++];
while (c < '-');
bool minus = 0;
if constexpr (is_signed_integer_v<T>) {
if (c == '-') {
minus = 1, c = ibuf[pil++];
}
}
x = 0;
assert('0' <= c && c <= '9');
while ('0' <= c && c <= '9') {
x = x * 10 + (c & 15), c = ibuf[pil++];
}
assert(isspace(static_cast<unsigned char>(c)));
if constexpr (is_signed_integer_v<T>) {
if (minus) x = -x;
}
}
template <class T>
enable_if_t<is_integral_v<T> || is_same_v<T, i128> || is_same_v<T, u128>> rd(
T &x) {
rd_integer(x);
}
template <class T>
enable_if_t<is_floating_point_v<T> || is_same_v<T, f128>> rd(T &x) {
rd_real(x);
}
template <class T, class U>
void rd(pair<T, U> &p) {
rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T>
void rd_tuple(T &t) {
if constexpr (N < tuple_size<T>::value) {
auto &x = get<N>(t);
rd(x);
rd_tuple<N + 1>(t);
}
}
template <class... T>
void rd(tuple<T...> &tpl) {
rd_tuple(tpl);
}
template <class T, size_t N>
void rd(array<T, N> &x) {
for (auto &d : x) rd(d);
}
template <class T>
void rd(vc<T> &x) {
for (auto &d : x) rd(d);
}
template <class... T>
void read(T &...x) {
(rd(x), ...);
}
inline void wt_range(const char *s, size_t n) {
size_t i = 0;
while (i < n) {
if (por == SZ) flush();
size_t chunk = min(n - i, (size_t)(SZ - por));
memcpy(obuf + por, s + i, chunk);
por += chunk;
i += chunk;
}
}
void wt(const char c) {
if (por == SZ) flush();
obuf[por++] = c;
}
void wt(const char *s) { wt_range(s, strlen(s)); }
void wt(const string &s) { wt_range(s.data(), s.size()); }
template <typename T>
void wt_integer(T x) {
if (por > SZ - 100) flush();
using U = unsigned_integer_t<T>;
U y = static_cast<U>(x);
if constexpr (is_signed_integer_v<T>) {
if (x < 0) {
obuf[por++] = '-';
y = U(0) - y;
}
}
int outi;
for (outi = 96; y >= 10000; outi -= 4) {
memcpy(out + outi, pre.num[y % 10000], 4);
y /= 10000;
}
if (y >= 1000) {
memcpy(obuf + por, pre.num[y], 4);
por += 4;
} else if (y >= 100) {
memcpy(obuf + por, pre.num[y] + 1, 3);
por += 3;
} else if (y >= 10) {
int q = (y * 103) >> 10;
obuf[por] = q | '0';
obuf[por + 1] = (y - q * 10) | '0';
por += 2;
} else
obuf[por++] = y | '0';
memcpy(obuf + por, out + outi + 4, 96 - outi);
por += 96 - outi;
}
template <typename T>
inline void wt_real(T x) {
static char buf[1000];
int n = std::snprintf(buf, sizeof(buf), "%.15f", (double)x);
wt_range(buf, (size_t)n);
}
template <class T>
enable_if_t<is_integral_v<T> || is_same_v<T, i128> || is_same_v<T, u128>> wt(
T x) {
wt_integer(x);
}
template <class T>
enable_if_t<is_floating_point_v<T> || is_same_v<T, f128>> wt(T x) {
wt_real(x);
}
inline void wt(bool b) { wt(static_cast<char>('0' + (b ? 1 : 0))); }
template <class T, class U>
void wt(const pair<T, U> &val) {
wt(val.first);
wt(' ');
wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T &t) {
if constexpr (N < tuple_size<T>::value) {
if constexpr (N > 0) wt(' ');
wt(get<N>(t));
wt_tuple<N + 1>(t);
}
}
template <class... T>
void wt(const tuple<T...> &tpl) {
wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> &val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
template <class T>
void wt(const vector<T> &val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&...tail) {
wt(forward<Head>(head));
((wt(' '), wt(forward<Tail>(tail))), ...);
wt('\n');
}
// gcc expansion. called automaticall after main.
void __attribute__((destructor)) _d() { flush(); }
} // namespace fastio
using fastio::flush;
using fastio::print;
using fastio::read;
#if defined(LOCAL)
#define HDR "[DEBUG:", __func__, __LINE__, "]"
#define SHOW(...) \
SHOW_IMPL(__VA_ARGS__, SHOW8, SHOW7, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, \
SHOW1) \
(__VA_ARGS__)
#define SHOW_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME
#define SHOW1(x) print(HDR, #x, "=", (x)), flush()
#define SHOW2(x, y) print(HDR, #x, "=", (x), #y, "=", (y)), flush()
#define SHOW3(x, y, z) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z)), flush()
#define SHOW4(x, y, z, w) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush()
#define SHOW5(x, y, z, w, v) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \
(v)), \
flush()
#define SHOW6(x, y, z, w, v, u) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \
(v), #u, "=", (u)), \
flush()
#define SHOW7(x, y, z, w, v, u, t) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \
(v), #u, "=", (u), #t, "=", (t)), \
flush()
#define SHOW8(x, y, z, w, v, u, t, s) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \
(v), #u, "=", (u), #t, "=", (t), #s, "=", (s)), \
flush()
#else
#define SHOW(...)
#endif
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
read(__VA_ARGS__)
#define U32(...) \
u32 __VA_ARGS__; \
read(__VA_ARGS__)
#define U64(...) \
u64 __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<type> name(size); \
read(name)
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(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); }
void YA(bool t = 1) { print(t ? "YA" : "TIDAK"); }
void TIDAK(bool t = 1) { YA(!t); }
void Alice(bool t = 1) { print(t ? "Alice" : "Bob"); }
void Bob(bool t = 1) { Alice(!t); }
#line 4 "test/2_library_checker/string/number_of_substrings.test.cpp"
#line 1 "ds/index_compression.hpp"
template <typename T>
struct Index_Compression_DISTINCT_SMALL {
int mi, ma;
vc<T> dat;
vc<T> build(vc<int> X) {
mi = 0, ma = -1;
if (!X.empty()) mi = MIN(X), ma = MAX(X);
dat.assign(ma - mi + 2, 0);
for (auto& x : X) dat[x - mi + 1]++;
FOR(i, len(dat) - 1) dat[i + 1] += dat[i];
for (auto& x : X) {
x = dat[x - mi]++;
}
FOR_R(i, 1, len(dat)) dat[i] = dat[i - 1];
dat[0] = 0;
return X;
}
int size() const { return len(dat); }
int val_to_idx(T x) const { return dat[clamp<ll>(x - mi, 0, ma - mi + 1)]; }
int idx_to_val(int i) const { return dat[i]; }
};
template <typename T>
struct Index_Compression_SAME_SMALL {
int mi, ma;
vc<T> dat;
vc<T> build(vc<T> X) {
mi = 0, ma = -1;
if (!X.empty()) mi = MIN(X), ma = MAX(X);
dat.assign(ma - mi + 2, 0);
for (auto& x : X) dat[x - mi + 1] = 1;
FOR(i, len(dat) - 1) dat[i + 1] += dat[i];
for (auto& x : X) {
x = dat[x - mi];
}
return X;
}
int size() const { return len(dat); }
int val_to_idx(T x) const { return dat[clamp<ll>(x - mi, 0, ma - mi + 1)]; }
int idx_to_val(int i) const { return dat[i]; }
};
template <typename T>
struct Index_Compression_SAME_LARGE {
vc<T> dat;
vc<int> build(const vc<T>& X) {
dat.reserve(len(X));
vc<pair<T, int>> tmp(len(X));
FOR(i, len(X)) tmp[i] = {X[i], i};
sort(all(tmp));
vc<int> ANS(len(X));
for (auto [x, j] : tmp) {
if (dat.empty() || dat.back() != x) dat.eb(x);
ANS[j] = len(dat) - 1;
}
return ANS;
}
int size() const { return len(dat); }
int val_to_idx(T x) const { return LB(dat, x); }
int idx_to_val(int i) const { return dat[i]; }
};
template <typename T>
struct Index_Compression_DISTINCT_LARGE {
vc<T> dat;
vc<int> build(vc<T> X) {
dat.reserve(len(X));
vc<pair<T, int>> tmp(len(X));
FOR(i, len(X)) tmp[i] = {X[i], i};
sort(all(tmp));
vc<int> ANS(len(X));
for (auto [x, j] : tmp) {
dat.eb(x);
ANS[j] = len(dat) - 1;
}
return ANS;
}
int size() const { return len(dat); }
int val_to_idx(T x) const { return LB(dat, x); }
int idx_to_val(int i) const { return dat[i]; }
};
template <typename T, bool SMALL>
using Index_Compression_DISTINCT =
typename std::conditional<SMALL, Index_Compression_DISTINCT_SMALL<T>,
Index_Compression_DISTINCT_LARGE<T>>::type;
template <typename T, bool SMALL>
using Index_Compression_SAME =
typename std::conditional<SMALL, Index_Compression_SAME_SMALL<T>,
Index_Compression_SAME_LARGE<T>>::type;
// SAME: [2,3,2] -> [0,1,0]
// DISTINCT: [2,2,3] -> [0,2,1]
// build で列を圧縮してくれる. そのあと
// (x): lower_bound(X,x) をかえす
template <typename T, bool SAME, bool SMALL>
using Index_Compression =
typename std::conditional<SAME, Index_Compression_SAME<T, SMALL>,
Index_Compression_DISTINCT<T, SMALL>>::type;
#line 1 "alg/monoid/min.hpp"
// require: all values x satisfy x <= infty<E>
template <typename E>
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 id() { return infty<E>; }
static constexpr bool commute = true;
};
#line 1 "other/bit.hpp"
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); }
int popcnt_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
// (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 <typename T>
T kth_bit(int k) {
assert(0 <= k && k < int(8 * sizeof(T)));
return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
assert(0 <= k && k < int(8 * sizeof(T)));
return x >> k & 1;
}
template <typename UINT>
struct all_bit {
static_assert(is_unsigned<UINT>::value);
UINT s;
all_bit(UINT s) : s(s) {}
struct iter {
UINT s;
int operator*() const { return lowbit(s); }
void operator++() { s &= s - 1; }
bool operator!=(nullptr_t) const { return s; }
};
iter begin() const { return {s}; }
nullptr_t end() const { return nullptr; }
};
template <typename UINT>
struct all_subset {
static_assert(is_unsigned<UINT>::value);
UINT s;
all_subset(UINT s) : s(s) {}
struct iter {
UINT s, t;
bool done = false;
UINT operator*() const { return t; }
void operator++() {
done = (t == 0);
t = (t - 1) & s;
}
bool operator!=(nullptr_t) const { return !done; }
};
iter begin() const { return {s, s}; }
nullptr_t end() const { return nullptr; }
};
constexpr u64 full_mask(int n) {
assert(0 <= n && n <= 64);
return n == 64 ? -1ULL : (1ULL << n) - 1;
}
u64 bit_reverse(u64 x) {
x = ((x & 0x5555555555555555ULL) << 1) | ((x >> 1) & 0x5555555555555555ULL);
x = ((x & 0x3333333333333333ULL) << 2) | ((x >> 2) & 0x3333333333333333ULL);
x = ((x & 0x0f0f0f0f0f0f0f0fULL) << 4) | ((x >> 4) & 0x0f0f0f0f0f0f0f0fULL);
x = ((x & 0x00ff00ff00ff00ffULL) << 8) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
x = ((x & 0x0000ffff0000ffffULL) << 16) | ((x >> 16) & 0x0000ffff0000ffffULL);
x = (x << 32) | (x >> 32);
return x;
}
#line 2 "ds/sparse_table/sparse_table.hpp"
// 冪等なモノイドであることを仮定。disjoint sparse table より x 倍高速
template <class Monoid>
struct Sparse_Table {
using MX = Monoid;
using X = typename MX::value_type;
int n, log;
vvc<X> dat;
Sparse_Table() {}
Sparse_Table(int n) { build(n); }
template <typename F>
Sparse_Table(int n, F f) {
build(n, f);
}
Sparse_Table(const vc<X>& v) { build(v); }
void build(int m) {
build(m, [](int i) -> X { return MX::id(); });
}
void build(const vc<X>& v) {
build(len(v), [&](int i) -> X { return v[i]; });
}
template <typename F>
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) const {
if (L == R) return MX::id();
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 <class F>
int max_right(const F check, int L) const {
assert(0 <= L && L <= n && check(MX::id()));
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 <class F>
int min_left(const F check, int R) const {
assert(0 <= R && R <= n && check(MX::id()));
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 1 "ds/segtree/segtree.hpp"
template <class Monoid>
struct SegTree {
using MX = Monoid;
using X = typename MX::value_type;
using value_type = X;
vc<X> dat;
int n, log, size;
SegTree() {}
SegTree(int n) { build(n); }
template <typename F>
SegTree(int n, F f) {
build(n, f);
}
SegTree(const vc<X>& v) { build(v); }
void build(int m) {
build(m, [](int i) -> X { return MX::id(); });
}
void build(const vc<X>& v) {
build(len(v), [&](int i) -> X { return v[i]; });
}
template <typename F>
void build(int m, F f) {
n = m, log = 1;
while ((1 << log) < n) ++log;
size = 1 << log;
dat.assign(size << 1, MX::id());
FOR(i, n) dat[size + i] = f(i);
FOR_R(i, 1, size) update(i);
}
X get(int i) const { return dat[size + i]; }
vc<X> get_all() const { 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) const {
assert(0 <= L && L <= R && R <= n);
X vl = Monoid::id(), vr = Monoid::id();
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);
}
vc<int> prod_ids(int L, int R) const {
assert(0 <= L && L <= R && R <= n);
vc<int> I, J;
L += size, R += size;
while (L < R) {
if (L & 1) I.eb(L++);
if (R & 1) J.eb(--R);
L >>= 1, R >>= 1;
}
reverse(all(J));
concat(I, J);
return I;
}
X prod_all() const { return dat[1]; }
template <class F>
int max_right(F check, int L) const {
assert(0 <= L && L <= n && check(Monoid::id()));
if (L == n) return n;
L += size;
X sm = Monoid::id();
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 <class F>
int min_left(F check, int R) const {
assert(0 <= R && R <= n && check(Monoid::id()));
if (R == 0) return 0;
R += size;
X sm = Monoid::id();
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} A[i xor x]
X xor_prod(int l, int r, int xor_val) const {
static_assert(Monoid::commute);
X x = Monoid::id();
for (int k = 0; k < log + 1; ++k) {
if (l >= 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 1 "other/bit.hpp"
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); }
int popcnt_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
// (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 <typename T>
T kth_bit(int k) {
assert(0 <= k && k < int(8 * sizeof(T)));
return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
assert(0 <= k && k < int(8 * sizeof(T)));
return x >> k & 1;
}
template <typename UINT>
struct all_bit {
static_assert(is_unsigned<UINT>::value);
UINT s;
all_bit(UINT s) : s(s) {}
struct iter {
UINT s;
int operator*() const { return lowbit(s); }
void operator++() { s &= s - 1; }
bool operator!=(nullptr_t) const { return s; }
};
iter begin() const { return {s}; }
nullptr_t end() const { return nullptr; }
};
template <typename UINT>
struct all_subset {
static_assert(is_unsigned<UINT>::value);
UINT s;
all_subset(UINT s) : s(s) {}
struct iter {
UINT s, t;
bool done = false;
UINT operator*() const { return t; }
void operator++() {
done = (t == 0);
t = (t - 1) & s;
}
bool operator!=(nullptr_t) const { return !done; }
};
iter begin() const { return {s, s}; }
nullptr_t end() const { return nullptr; }
};
constexpr u64 full_mask(int n) {
assert(0 <= n && n <= 64);
return n == 64 ? -1ULL : (1ULL << n) - 1;
}
u64 bit_reverse(u64 x) {
x = ((x & 0x5555555555555555ULL) << 1) | ((x >> 1) & 0x5555555555555555ULL);
x = ((x & 0x3333333333333333ULL) << 2) | ((x >> 2) & 0x3333333333333333ULL);
x = ((x & 0x0f0f0f0f0f0f0f0fULL) << 4) | ((x >> 4) & 0x0f0f0f0f0f0f0f0fULL);
x = ((x & 0x00ff00ff00ff00ffULL) << 8) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
x = ((x & 0x0000ffff0000ffffULL) << 16) | ((x >> 16) & 0x0000ffff0000ffffULL);
x = (x << 32) | (x >> 32);
return x;
}
#line 2 "ds/sparse_table/sparse_table.hpp"
// 冪等なモノイドであることを仮定。disjoint sparse table より x 倍高速
template <class Monoid>
struct Sparse_Table {
using MX = Monoid;
using X = typename MX::value_type;
int n, log;
vvc<X> dat;
Sparse_Table() {}
Sparse_Table(int n) { build(n); }
template <typename F>
Sparse_Table(int n, F f) {
build(n, f);
}
Sparse_Table(const vc<X>& v) { build(v); }
void build(int m) {
build(m, [](int i) -> X { return MX::id(); });
}
void build(const vc<X>& v) {
build(len(v), [&](int i) -> X { return v[i]; });
}
template <typename F>
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) const {
if (L == R) return MX::id();
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 <class F>
int max_right(const F check, int L) const {
assert(0 <= L && L <= n && check(MX::id()));
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 <class F>
int min_left(const F check, int R) const {
assert(0 <= R && R <= n && check(MX::id()));
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 1 "other/bit.hpp"
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); }
int popcnt_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
// (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 <typename T>
T kth_bit(int k) {
assert(0 <= k && k < int(8 * sizeof(T)));
return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
assert(0 <= k && k < int(8 * sizeof(T)));
return x >> k & 1;
}
template <typename UINT>
struct all_bit {
static_assert(is_unsigned<UINT>::value);
UINT s;
all_bit(UINT s) : s(s) {}
struct iter {
UINT s;
int operator*() const { return lowbit(s); }
void operator++() { s &= s - 1; }
bool operator!=(nullptr_t) const { return s; }
};
iter begin() const { return {s}; }
nullptr_t end() const { return nullptr; }
};
template <typename UINT>
struct all_subset {
static_assert(is_unsigned<UINT>::value);
UINT s;
all_subset(UINT s) : s(s) {}
struct iter {
UINT s, t;
bool done = false;
UINT operator*() const { return t; }
void operator++() {
done = (t == 0);
t = (t - 1) & s;
}
bool operator!=(nullptr_t) const { return !done; }
};
iter begin() const { return {s, s}; }
nullptr_t end() const { return nullptr; }
};
constexpr u64 full_mask(int n) {
assert(0 <= n && n <= 64);
return n == 64 ? -1ULL : (1ULL << n) - 1;
}
u64 bit_reverse(u64 x) {
x = ((x & 0x5555555555555555ULL) << 1) | ((x >> 1) & 0x5555555555555555ULL);
x = ((x & 0x3333333333333333ULL) << 2) | ((x >> 2) & 0x3333333333333333ULL);
x = ((x & 0x0f0f0f0f0f0f0f0fULL) << 4) | ((x >> 4) & 0x0f0f0f0f0f0f0f0fULL);
x = ((x & 0x00ff00ff00ff00ffULL) << 8) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
x = ((x & 0x0000ffff0000ffffULL) << 16) | ((x >> 16) & 0x0000ffff0000ffffULL);
x = (x << 32) | (x >> 32);
return x;
}
#line 2 "ds/sparse_table/disjoint_sparse_table.hpp"
template <class Monoid>
struct Disjoint_Sparse_Table {
using MX = Monoid;
using X = typename MX::value_type;
int n, log;
vvc<X> dat;
Disjoint_Sparse_Table() {}
Disjoint_Sparse_Table(int n) { build(n); }
template <typename F>
Disjoint_Sparse_Table(int n, F f) {
build(n, f);
}
Disjoint_Sparse_Table(const vc<X>& v) { build(v); }
void build(int m) {
build(m, [](int i) -> X { return MX::id(); });
}
void build(const vc<X>& v) {
build(len(v), [&](int i) -> X { return v[i]; });
}
template <typename F>
void build(int m, F f) {
n = m, log = 1;
while ((1 << log) < n) ++log;
dat.resize(log);
dat[0].reserve(n);
FOR(i, n) dat[0].eb(f(i));
FOR(i, 1, log) {
auto& v = dat[i];
v = dat[0];
int b = 1 << i;
for (int m = b; m <= n; m += 2 * b) {
int L = m - b, R = min(n, m + b);
FOR_R(j, L + 1, m) v[j - 1] = MX::op(v[j - 1], v[j]);
FOR(j, m, R - 1) v[j + 1] = MX::op(v[j], v[j + 1]);
}
}
}
X prod(int L, int R) const {
if (L == R) return MX::id();
--R;
if (L == R) return dat[0][L];
int k = topbit(L ^ R);
return MX::op(dat[k][L], dat[k][R]);
}
template <class F>
int max_right(const F check, int L) const {
assert(0 <= L && L <= n && check(MX::id()));
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 <class F>
int min_left(const F check, int R) const {
assert(0 <= R && R <= n && check(MX::id()));
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 3 "ds/static_range_product.hpp"
/*
参考:https://judge.yosupo.jp/submission/106668
長さ 2^LOG のブロックに分ける.ブロック内の prefix, suffix を持つ.
ブロック積の列を ST(DST) で持つ.ブロックをまたぐ積は O(1).
短いものは O(1) を諦めて愚直ということにする.
前計算:O(Nlog(N)/2^LOG)
クエリ:O(1) / worst O(2^LOG)
*/
template <typename Monoid, typename SPARSE_TABLE, int LOG = 4>
struct Static_Range_Product {
using MX = Monoid;
using X = typename MX::value_type;
int N, b_num;
vc<X> A, pre, suf; // inclusive
SPARSE_TABLE ST;
Static_Range_Product() {}
template <typename F>
Static_Range_Product(int n, F f) {
build(n, f);
}
Static_Range_Product(const vc<X>& v) { build(v); }
void build(const vc<X>& v) {
build(len(v), [&](int i) -> X { return v[i]; });
}
template <typename F>
void build(int m, F f) {
N = m;
b_num = N >> LOG;
A.resize(N);
FOR(i, N) A[i] = f(i);
pre = A, suf = A;
constexpr int mask = (1 << LOG) - 1;
FOR(i, 1, N) {
if (i & mask) pre[i] = MX::op(pre[i - 1], A[i]);
}
FOR_R(i, 1, N) {
if (i & mask) suf[i - 1] = MX::op(A[i - 1], suf[i]);
}
ST.build(b_num, [&](int i) -> X { return suf[i << LOG]; });
}
// O(1) or O(R-L)
X prod(int L, int R) const {
if (L == R) return MX::id();
R -= 1;
int a = L >> LOG, b = R >> LOG;
if (a < b) {
X x = ST.prod(a + 1, b);
x = MX::op(suf[L], x);
x = MX::op(x, pre[R]);
return x;
}
X x = A[L];
FOR(i, L + 1, R + 1) x = MX::op(x, A[i]);
return x;
}
template <class F>
int max_right(const F check, int L) const {
assert(0 <= L && L <= N && check(MX::id()));
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 <class F>
int min_left(const F check, int R) const {
assert(0 <= R && R <= N && check(MX::id()));
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 6 "string/suffix_array.hpp"
// 辞書順 i 番目の suffix が j 文字目始まりであるとき、
// SA[i] = j, ISA[j] = i
// |S|>0 を前提(そうでない場合 dummy 文字を追加して利用せよ)
// SEG_TYPE=0: SegTree, 1: SparseTable, 2: StaticRangeProduct
template <int SEG_TYPE = 0>
struct Suffix_Array {
vc<int> SA;
vc<int> ISA;
vc<int> LCP;
using Mono = Monoid_Min<int>;
using SEG0 = SegTree<Mono>;
using SEG1 = Sparse_Table<Mono>;
using SEG2 = Static_Range_Product<Mono, Sparse_Table<Mono>, 4>;
static_assert(SEG_TYPE == 0 || SEG_TYPE == 1 || SEG_TYPE == 2);
using SegType = conditional_t<SEG_TYPE == 0, SEG0,
conditional_t<SEG_TYPE == 1, SEG1, SEG2> >;
SegType seg;
bool build_seg;
Suffix_Array() {}
template <typename STRING>
Suffix_Array(const STRING& s) {
build_seg = 0;
auto a = to_compressed_vector(s);
SA = SA_IS(a);
calc_LCP(a);
}
vc<int> to_compressed_vector(const string& s) {
vc<int> a(len(s));
FOR(i, len(s)) a[i] = (unsigned char)s[i];
Index_Compression<int, 1, 1> I;
return I.build(a);
}
vc<int> to_compressed_vector(const vc<int>& s) {
Index_Compression<int, 1, 0> I;
return I.build(s);
}
// lcp(S[i:], S[j:])
int lcp(int i, int j) {
if (!build_seg) {
build_seg = true;
seg.build(LCP);
}
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);
}
// S[i:] との lcp が n 以上であるような半開区間
pair<int, int> lcp_range(int i, int n) {
if (!build_seg) {
build_seg = true;
seg.build(LCP);
}
i = ISA[i];
int a = seg.min_left([&](auto e) -> bool { return e >= n; }, i);
int b = seg.max_right([&](auto e) -> bool { return e >= n; }, i);
return {a, b + 1};
}
// -1: S[L1:R1) < S[L2, R2)
// 0: S[L1:R1) = S[L2, R2)
// +1: S[L1:R1) > S[L2, R2)
int compare(int L1, int R1, int L2, int R2) {
int n1 = R1 - L1, n2 = R2 - L2;
int n = lcp(L1, L2);
chmin(n, n1);
chmin(n, n2);
if (n == n1 && n == n2) return 0;
if (n == n1) return -1;
if (n == n2) return 1;
return (ISA[L1 + n] > ISA[L2 + n] ? 1 : -1);
}
private:
void induced_sort(const vc<int>& vect, int val_range, vc<int>& SA,
const vc<bool>& sl, const vc<int>& lms_idx) {
vc<int> 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<int> SA_IS(vc<int> vect) {
for (auto& x : vect) ++x;
vect.eb(0);
int val_range = MAX(vect) + 1;
const int n = vect.size();
vc<int> SA(n), lms_idx;
vc<bool> 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<int> 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);
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);
SA.erase(SA.begin());
return SA;
}
void calc_LCP(const vc<int>& s) {
int n = s.size(), k = 0;
ISA.resize(n);
LCP.resize(n);
if (n == 0) return;
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 6 "test/2_library_checker/string/number_of_substrings.test.cpp"
void solve() {
STR(S);
ll N = len(S);
Suffix_Array sa(S);
ll ANS = N * (N + 1) / 2;
ANS -= SUM<ll>(sa.LCP);
print(ANS);
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << setprecision(15);
solve();
return 0;
}