結果

問題 No.515 典型LCP
ユーザー kimiyukikimiyuki
提出日時 2017-05-09 23:27:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 437 ms / 1,000 ms
コード長 2,473 bytes
コンパイル時間 1,464 ms
コンパイル使用メモリ 89,760 KB
実行使用メモリ 155,268 KB
最終ジャッジ日時 2023-10-12 21:52:51
合計ジャッジ時間 6,119 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 437 ms
155,108 KB
testcase_01 AC 427 ms
155,268 KB
testcase_02 AC 202 ms
27,460 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 188 ms
27,760 KB
testcase_06 AC 191 ms
27,736 KB
testcase_07 AC 190 ms
27,800 KB
testcase_08 AC 197 ms
27,972 KB
testcase_09 AC 193 ms
28,092 KB
testcase_10 AC 194 ms
28,220 KB
testcase_11 AC 193 ms
28,264 KB
testcase_12 AC 193 ms
28,228 KB
testcase_13 AC 192 ms
27,744 KB
testcase_14 AC 18 ms
5,068 KB
testcase_15 AC 176 ms
27,500 KB
testcase_16 AC 177 ms
27,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
using ll = long long;
using namespace std;

template <class Monoid>
struct sparse_table {
    typedef typename Monoid::type T;
    vector<vector<T> > table;
    Monoid mon;
    sparse_table(vector<T> const & init, Monoid const & a_mon = Monoid())
            : mon(a_mon) {
        int n = init.size();
        int log_n = sqrt(n) + 1;
        table.resize(log_n, vector<T>(n, mon.unit));
        table[0] = init;
        for (int k = 0; k < log_n-1; ++ k) {
            for (int i = 0; i < n; ++ i) {
                table[k+1][i] = mon.append(table[k][i], i + (1ll<<k) < n ? table[k][i + (1ll<<k)] : mon.unit);
            }
        }
    }
    T operator () (int l, int r) {
        assert (0 <= l and l <= r and r <= table[0].size());
        if (l == r) return mon.unit;
        int k = log2(r - l);
        return mon.append(table[k][l], table[k][r - (1ll<<k)]);
    }
};
struct min_t {
    typedef int type;
    const int unit = 1e9+7;
    int append(int a, int b) { return min(a, b); }
};

int compute_lcp(string const & a, string const & b) {
    int i = 0;
    while (i < a.length() and i < b.length() and a[i] == b[i]) ++ i;
    return i;
}

int main() {
    // input
    int n; cin >> n;
    vector<string> s(n); repeat (i,n) cin >> s[i];
    int m; ll x, d; cin >> m >> x >> d;
    // generate queries
    vector<int> qi(m);
    vector<int> qj(m);
    repeat (k,m) {
        qi[k] = x / (n - 1);
        qj[k] = x % (n - 1);
        if (qi[k] > qj[k]) {
            swap(qi[k], qj[k]);
        } else {
            qj[k] += 1;
        }
        x = (x + d) % (n *(ll) (n - 1));
    }
    // construct a sparse table
    vector<int> rank(n);
    whole(iota, rank, 0);
    whole(sort, rank, [&](int i, int j) { return s[i] < s[j]; });
    vector<int> lcp_0(n-1);
    repeat (i,n-1) lcp_0[i] = compute_lcp(s[rank[i]], s[rank[i+1]]);
    sparse_table<min_t> table(lcp_0);
    vector<int> rank_of(n);
    repeat (i,n) rank_of[rank[i]] = i;
    // compute
    ll result = 0;
    repeat (q,m) {
        int l = rank_of[qi[q]];
        int r = rank_of[qj[q]];
        if (l > r) swap(l, r);
        result += table(l, r);
    }
    // output
    cout << result << endl;
    return 0;
}
0