結果

問題 No.962 LCPs
ユーザー PachicobuePachicobue
提出日時 2019-12-25 02:44:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 11,227 bytes
コンパイル時間 2,583 ms
コンパイル使用メモリ 218,152 KB
実行使用メモリ 12,968 KB
最終ジャッジ日時 2023-10-24 05:03:28
合計ジャッジ時間 6,706 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 107 ms
12,968 KB
testcase_18 AC 76 ms
8,224 KB
testcase_19 AC 76 ms
8,224 KB
testcase_20 AC 65 ms
6,664 KB
testcase_21 AC 64 ms
6,664 KB
testcase_22 AC 57 ms
5,584 KB
testcase_23 AC 57 ms
5,584 KB
testcase_24 AC 54 ms
5,056 KB
testcase_25 AC 54 ms
5,056 KB
testcase_26 AC 51 ms
4,792 KB
testcase_27 AC 59 ms
5,848 KB
testcase_28 AC 51 ms
5,056 KB
testcase_29 AC 47 ms
4,860 KB
testcase_30 AC 55 ms
5,584 KB
testcase_31 AC 63 ms
6,648 KB
testcase_32 AC 46 ms
4,740 KB
testcase_33 AC 71 ms
8,028 KB
testcase_34 AC 49 ms
5,056 KB
testcase_35 AC 49 ms
4,804 KB
testcase_36 AC 54 ms
5,320 KB
testcase_37 AC 32 ms
4,348 KB
testcase_38 AC 32 ms
4,348 KB
testcase_39 AC 32 ms
4,348 KB
testcase_40 AC 31 ms
4,348 KB
testcase_41 AC 32 ms
4,348 KB
testcase_42 AC 32 ms
4,348 KB
testcase_43 AC 32 ms
4,348 KB
testcase_44 AC 31 ms
4,348 KB
testcase_45 AC 32 ms
4,348 KB
testcase_46 AC 31 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
testcase_48 AC 2 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 3 ms
4,348 KB
testcase_52 AC 2 ms
4,348 KB
testcase_53 AC 2 ms
4,348 KB
testcase_54 AC 3 ms
4,348 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 2 ms
4,348 KB
testcase_57 AC 17 ms
4,348 KB
testcase_58 AC 17 ms
4,348 KB
testcase_59 AC 18 ms
4,348 KB
testcase_60 AC 19 ms
4,348 KB
testcase_61 AC 19 ms
4,348 KB
testcase_62 AC 20 ms
4,348 KB
testcase_63 AC 37 ms
9,740 KB
testcase_64 AC 3 ms
4,348 KB
testcase_65 AC 37 ms
9,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 = 500;
    constexpr int W = 500;
    for (int i = 0; i <= N - 1; i++) {
        for (int l = 1; l <= L; l++) {  // LNlogN
            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) { break; }
            const int width = inf - i + 1;
            ans += (ll)width * (width + 1) / 2 - 1;
        }
    }
    for (int i = 0; i < N - 1; i++) {
        for (int w = 1; i + w <= N - 1; w++) {  // N^2/L
            const int len = rmq.fold(i, i + w);
            if (len > L) {
                ans += (ll)len * (w + 1);
            } else {
                break;
            }
        }
    }
    outln(ans);
    return 0;
}
0