結果

問題 No.515 典型LCP
ユーザー SPARKLE_mathSPARKLE_math
提出日時 2022-05-03 15:06:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 1,000 ms
コード長 2,398 bytes
コンパイル時間 1,064 ms
コンパイル使用メモリ 88,940 KB
実行使用メモリ 16,680 KB
最終ジャッジ日時 2023-09-15 09:56:50
合計ジャッジ時間 3,816 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 235 ms
16,680 KB
testcase_01 AC 234 ms
16,680 KB
testcase_02 AC 119 ms
4,556 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 76 ms
4,376 KB
testcase_06 AC 76 ms
4,376 KB
testcase_07 AC 76 ms
4,376 KB
testcase_08 AC 94 ms
4,380 KB
testcase_09 AC 77 ms
4,380 KB
testcase_10 AC 79 ms
4,376 KB
testcase_11 AC 80 ms
4,376 KB
testcase_12 AC 80 ms
4,376 KB
testcase_13 AC 78 ms
4,380 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 76 ms
4,376 KB
testcase_16 AC 75 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.cpp"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <numeric>
#line 3 "/home/kokoro601/compro_library/DataStructure/SparseTable.hpp"

template <class T> class SparseTable {
public:
    std::vector<std::vector<T> > tab;
    std::vector<int> lookup;


    void build(const std::vector<T> &v) {
        int b = 0;
        int n = v.size();
        while (1 << b <= n) b++;
        tab.assign(b, std::vector<T>(1 << b));

        for (int i = 0; i < n; i++) tab[0][i] = v[i];
        
        for (int i = 1; i < b; i++) {
            int pre_len = 1 << (i - 1);
            int ub = n - (1 << i);
            for (int j = 0; j <= ub; j++) {
                tab[i][j] = std::min(tab[i - 1][j], tab[i - 1][j + pre_len]);
            }
        }

        lookup.resize(v.size() + 1);
        for (int i = 2; i < lookup.size(); i++)
            lookup[i] = lookup[i >> 1] + 1;
    }

    // [l, r)
    inline T rmq(int l, int r) {
        // assert(l < r);
        int b = lookup[r - l];
        return std::min(tab[b][l], tab[b][r - (1 << b)]);
    }
};
#line 8 "main.cpp"

using namespace std;
using ll = long long;
using Graph = vector<vector<int> >;


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll n; cin >> n;
    vector<string> s(n);
    vector<int> lcp(n), rank(n), rank_rev(n);

    for (int i = 0; i < n; i++) cin >> s[i];

    iota(rank_rev.begin(), rank_rev.end(), 0);
    sort(rank_rev.begin(), rank_rev.end(), [&](int a, int b) { return s[a] < s[b]; });
    for (int i = 0; i < n; i++) rank[rank_rev[i]] = i; 

    for (int i = 0; i < n; i++) {
        int j = 0;
        if (rank[i] + 1 == n) continue; 
        int nxt_idx = rank_rev[rank[i] + 1];
        for (; j < min(s[i].size(), s[nxt_idx].size()); j++)
            if (s[i][j] != s[nxt_idx][j]) break;
        lcp[rank[i]] = j;
    }


    SparseTable<int> st;
    st.build(lcp);

    int m; ll x, d;
    cin >> m >> x >> d;
    ll ans = 0;
    for (int k = 0; k < m; k++) {
        ll i = (x / (n - 1)) + 1;
        ll j = (x % (n - 1)) + 1;
        if (i > j) swap(i, j);
        else j = j + 1;

        int rank1 = rank[i - 1];
        int rank2 = rank[j - 1];
        if (rank1 > rank2) swap(rank1, rank2);
        ans += st.rmq(rank1, rank2);

        x = (x + d) % (n * (n - 1));
    }
    cout << ans << endl;

    return 0;
}

0