結果
問題 | No.962 LCPs |
ユーザー | Pachicobue |
提出日時 | 2019-12-25 02:41:53 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 11,156 bytes |
コンパイル時間 | 2,824 ms |
コンパイル使用メモリ | 217,580 KB |
実行使用メモリ | 19,644 KB |
最終ジャッジ日時 | 2024-09-22 21:54:31 |
合計ジャッジ時間 | 6,489 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
13,756 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 11 ms
6,940 KB |
testcase_03 | AC | 5 ms
6,944 KB |
testcase_04 | AC | 3 ms
6,944 KB |
testcase_05 | AC | 3 ms
6,944 KB |
testcase_06 | AC | 3 ms
6,944 KB |
testcase_07 | AC | 4 ms
6,940 KB |
testcase_08 | AC | 4 ms
6,944 KB |
testcase_09 | AC | 3 ms
6,940 KB |
testcase_10 | AC | 4 ms
6,944 KB |
testcase_11 | AC | 2 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 2 ms
6,940 KB |
testcase_14 | AC | 1 ms
6,940 KB |
testcase_15 | AC | 2 ms
6,940 KB |
testcase_16 | AC | 2 ms
6,940 KB |
testcase_17 | TLE | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
testcase_47 | -- | - |
testcase_48 | -- | - |
testcase_49 | -- | - |
testcase_50 | -- | - |
testcase_51 | -- | - |
testcase_52 | -- | - |
testcase_53 | -- | - |
testcase_54 | -- | - |
testcase_55 | -- | - |
testcase_56 | -- | - |
testcase_57 | -- | - |
testcase_58 | -- | - |
testcase_59 | -- | - |
testcase_60 | -- | - |
testcase_61 | -- | - |
testcase_62 | -- | - |
testcase_63 | -- | - |
testcase_64 | -- | - |
testcase_65 | -- | - |
ソースコード
#include <bits/stdc++.h> // created [2019/12/24] 23:17:25 #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wsign-conversion" using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; using uint = unsigned int; using usize = std::size_t; using ll = long long; using ull = unsigned long long; using ld = long double; template<typename T, usize n> using arr = T (&)[n]; template<typename T, usize n> using c_arr = const T (&)[n]; template<typename T> constexpr T popcount(const T u) { return u ? static_cast<T>(__builtin_popcountll(static_cast<u64>(u))) : static_cast<T>(0); } template<typename T> constexpr T log2p1(const T u) { return u ? static_cast<T>(64 - __builtin_clzll(static_cast<u64>(u))) : static_cast<T>(0); } template<typename T> constexpr T msbp1(const T u) { return log2p1(u); } template<typename T> constexpr T lsbp1(const T u) { return __builtin_ffsll(u); } template<typename T> constexpr T clog(const T u) { return u ? log2p1(u - 1) : static_cast<T>(u); } template<typename T> constexpr bool ispow2(const T u) { return u and (static_cast<u64>(u) & static_cast<u64>(u - 1)) == 0; } template<typename T> constexpr T ceil2(const T u) { return static_cast<T>(1) << clog(u); } template<typename T> constexpr T floor2(const T u) { return u == 0 ? static_cast<T>(0) : static_cast<T>(1) << (log2p1(u) - 1); } template<typename T> constexpr bool btest(const T mask, const usize ind) { return static_cast<bool>((static_cast<u64>(mask) >> ind) & static_cast<u64>(1)); } template<typename T> void bset(T& mask, const usize ind) { mask |= (static_cast<T>(1) << ind); } template<typename T> void breset(T& mask, const usize ind) { mask &= ~(static_cast<T>(1) << ind); } template<typename T> void bflip(T& mask, const usize ind) { mask ^= (static_cast<T>(1) << ind); } template<typename T> void bset(T& mask, const usize ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); } template<typename T> constexpr T bcut(const T mask, const usize ind) { return ind == 0 ? static_cast<T>(0) : static_cast<T>((static_cast<u64>(mask) << (64 - ind)) >> (64 - ind)); } template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } constexpr unsigned int mod = 1000000007; template<typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4; template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; auto mfp = [](auto&& f) { return [=](auto&&... args) { return f(f, std::forward<decltype(args)>(args)...); }; }; template<typename T> T in() { T v; return std::cin >> v, v; } template<typename T, typename Uint, usize n, usize i> T in_v(typename std::enable_if<(i == n), c_arr<Uint, n>>::type) { return in<T>(); } template<typename T, typename Uint, usize n, usize i> auto in_v(typename std::enable_if<(i < n), c_arr<Uint, n>>::type& szs) { const usize s = (usize)szs[i]; std::vector<decltype(in_v<T, Uint, n, i + 1>(szs))> ans(s); for (usize j = 0; j < s; j++) { ans[j] = in_v<T, Uint, n, i + 1>(szs); } return ans; } template<typename T, typename Uint, usize n> auto in_v(c_arr<Uint, n> szs) { return in_v<T, Uint, n, 0>(szs); } template<typename... Types> auto in_t() { return std::tuple<std::decay_t<Types>...>{in<Types>()...}; } struct io_init { io_init() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(20); } void clear() { std::cin.tie(), std::ios::sync_with_stdio(true); } } io_setting; template<typename T> int out(const T& v) { return std::cout << v, 0; } template<typename T> int out(const std::vector<T>& v) { for (usize i = 0; i < v.size(); i++) { if (i > 0) { std::cout << ' '; } out(v[i]); } return std::cout << "\n", 0; } template<typename T1, typename T2> int out(const std::pair<T1, T2>& v) { return out(v.first), std::cout << ' ', out(v.second), 0; } template<typename T, typename... Args> int out(const T& v, const Args... args) { return out(v), std::cout << ' ', out(args...), 0; } template<typename... Args> int outln(const Args... args) { return out(args...), std::cout << '\n', 0; } template<typename... Args> void outel(const Args... args) { return out(args...), std::cout << std::endl, 0; } # define SHOW(...) static_cast<void>(0) constexpr ull TEN(const usize n) { return n == 0 ? 1ULL : TEN(n - 1) * 10ULL; } template<typename T, typename Uint, usize n, usize i> auto make_v(typename std::enable_if<(i == n), c_arr<Uint, n>>::type, const T& v = T{}) { return v; } template<typename T, typename Uint, usize n, usize i> auto make_v(typename std::enable_if<(i < n), c_arr<Uint, n>>::type szs, const T& v = T{}) { const usize s = (usize)szs[i]; return std::vector<decltype(make_v<T, Uint, n, i + 1>(szs, v))>(s, make_v<T, Uint, n, i + 1>(szs, v)); } template<typename T, typename Uint, usize n> auto make_v(c_arr<Uint, n> szs, const T& t = T{}) { return make_v<T, Uint, n, 0>(szs, t); } template<typename SemiGroup> class sparse_table { public: using semigroup_type = SemiGroup; using value_type = typename semigroup_type::value_type; template<typename InIt> sparse_table(const InIt first, const InIt last) : lg{clog(static_cast<usize>(std::distance(first, last) + 1))}, table(lg, std::vector<value_type>(1 << lg)) { const usize cap = 1 << lg; for (usize d = 0, base = 1; d < lg; d++, base <<= 1) { std::copy(first, last, table[d].begin()); for (usize b = 0; b < cap / base; b++) { if (b % 2 == 1) { for (usize i = 1; i < base; i++) { table[d][b * base + i] = semigroup_type::merge(table[d][b * base + i - 1], table[d][b * base + i]); } } else { for (usize i = 1; i < base; i++) { table[d][(b + 1) * base - i - 1] = semigroup_type::merge(table[d][(b + 1) * base - i - 1], table[d][(b + 1) * base - i]); } } } } } value_type fold(const usize l, const usize r) const { assert(l < r); if (r - l == 1) { return table[0][l]; } const usize d = log2p1(r ^ l) - 1; return semigroup_type::merge(table[d][l], table[d][r - 1]); } private: const usize lg; std::vector<std::vector<value_type>> table; }; template<typename Value, typename Comp = std::less<Value>, typename Bucket = uint64_t> class static_rmq { public: using value_type = Value; using comparator_type = Comp; template<typename InIt> static_rmq(const InIt first, const InIt last) : sz{static_cast<usize>(std::distance(first, last))}, bn{wind(sz + bs - 1)}, val{first, last}, bucket_val([&]() { std::vector<value_type> ans(bn); for (usize i = 0; i < sz; i++) { ans[wind(i)] = i % bs == 0 ? val[i] : std::min(ans[wind(i)], val[i], comparator_type{}); } return ans; }()), masks(sz, 0), st(bucket_val.begin(), bucket_val.end()) { for (usize i = 0; i < bn; i++) { std::vector<usize> g(bs, sz); std::stack<usize> stack; for (usize j = 0; j < bs and ind(i, j) < sz; j++) { for (; not stack.empty() and not comparator_type{}(val[stack.top()], val[ind(i, j)]); stack.pop()) {} g[j] = stack.empty() ? sz : stack.top(), stack.push(ind(i, j)); } for (usize j = 0; j < bs and ind(i, j) < sz; j++) { masks[ind(i, j)] = g[j] == sz ? static_cast<bucket_type>(0) : (masks[g[j]] | static_cast<bucket_type>(1) << (g[j] - i * bs)); } } } value_type fold(const usize l, const usize r) const { assert(l < r), assert(r <= sz); const usize lb = (l + bs - 1) / bs, rb = r / bs; if (lb > rb) { return brmq(l, r); } else { return lb < rb ? (l < bs * lb ? (bs * rb < r ? std::min({st.fold(lb, rb), brmq(l, bs * lb), brmq(bs * rb, r)}, comparator_type{}) : std::min(st.fold(lb, rb), brmq(l, bs * lb), comparator_type{})) : (bs * rb < r ? std::min(st.fold(lb, rb), brmq(bs * rb, r), comparator_type{}) : st.fold(lb, rb))) : (l < bs * lb ? (bs * rb < r ? std::min(brmq(l, bs * lb), brmq(bs * rb, r), comparator_type{}) : brmq(l, bs * lb)) : (bs * rb < r ? brmq(bs * rb, r) : value_type{})); } } private: using bucket_type = Bucket; static constexpr usize bs = sizeof(bucket_type) * 8; static constexpr usize bslog = log2p1(bs) - 1; static constexpr usize wind(const usize n) { return n >> (bslog); } static constexpr usize bind(const usize n) { return bcut(n, bslog); } static constexpr usize ind(const usize w, const usize b) { return (w << bslog) | b; } value_type brmq(const usize l, usize r) const { r--; const Bucket w = masks[r] >> (l % bs); return w == 0 ? val[r] : val[l + lsbp1(w) - 1]; } struct min { using value_type = Value; static value_type merge(const value_type& a, const value_type& b) { return std::min(a, b, comparator_type{}); } }; const usize sz, bn; std::vector<value_type> val, bucket_val; std::vector<Bucket> masks; sparse_table<min> st; }; int main() { const auto N = in<int>(); const auto ss = in_v<std::string>({N}); SHOW(ss); auto lcp = [](const std::string& s, const std::string& t) { int ans = 0; for (; ans < s.size() and ans < t.size() and s[ans] == t[ans]; ans++) {} return ans; }; std::vector<int> ls(N - 1); for (int i = 0; i < N - 1; i++) { ls[i] = lcp(ss[i], ss[i + 1]); } SHOW(ls); static_rmq<int> rmq(ls.begin(), ls.end()); ll ans = 0; for (const auto& s : ss) { ans += s.size(); } constexpr int L = 100; constexpr int W = 2000; for (int l = 1; l <= L; l++) { // LNlogN for (int i = 0; i <= N - 1; i++) { int inf = i, sup = N; while (sup - inf > 1) { const int mid = (inf + sup) / 2; const int len = rmq.fold(i, mid); (len >= l ? inf : sup) = mid; } if (inf == i) { continue; } const int width = inf - i + 1; ans += (ll)width * (width + 1) / 2 - 1; } } for (int w = 1; w <= W; w++) { // N^2/L for (int i = 0; i + w <= N - 1; i++) { const int len = rmq.fold(i, i + w); if (len > L) { ans += (ll)len * (w + 1); } } } outln(ans); return 0; }