結果

問題 No.515 典型LCP
ユーザー tokusakuraitokusakurai
提出日時 2020-07-14 17:43:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 477 ms / 1,000 ms
コード長 2,379 bytes
コンパイル時間 2,141 ms
コンパイル使用メモリ 185,140 KB
実行使用メモリ 28,012 KB
最終ジャッジ日時 2024-04-28 22:43:48
合計ジャッジ時間 5,916 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 469 ms
28,012 KB
testcase_01 AC 477 ms
27,928 KB
testcase_02 AC 132 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 97 ms
6,940 KB
testcase_06 AC 98 ms
6,940 KB
testcase_07 AC 98 ms
6,940 KB
testcase_08 AC 119 ms
6,944 KB
testcase_09 AC 102 ms
6,948 KB
testcase_10 AC 109 ms
6,944 KB
testcase_11 AC 109 ms
6,940 KB
testcase_12 AC 109 ms
6,944 KB
testcase_13 AC 100 ms
6,944 KB
testcase_14 AC 21 ms
6,944 KB
testcase_15 AC 99 ms
6,944 KB
testcase_16 AC 100 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

template<typename Semigroup>
struct Sparse_Table{
    Semigroup ope(Semigroup a, Semigroup b){
        return min(a, b);
    }
    const int n;
    vector<vector<Semigroup>> st;
    vector<int> lookup;

    Sparse_Table(const vector<Semigroup> &table) : n(sz(table)){
        st.resize(n);
        rep(i, n) st[i].pb(table[i]);
        for(int k = 0; (1<<k) < n; k++){
            rep(i, n){
                if(i+(1<<k) < n) st[i].pb(ope(st[i][k], st[i+(1<<k)][k]));
                else st[i].pb(st[i][k]);
            }
        }
        lookup.resize(n+1);
        lookup[0] = -1;
        rep2(i, 1, n) lookup[i] = lookup[i/2]+1;
    }

    Semigroup query(int l, int r){
        int k = lookup[r-l];
        return ope(st[l][k], st[r-(1<<k)][k]);
    }

    Semigroup operator [] (int i) {return st[i][0];}
};

int main(){
    ll N;
    cin >> N;
    string s[N];
    rep(i, N) cin >> s[i];

    pair<string, int> p[N];
    rep(i, N) p[i] = make_pair(s[i], i);
    sort(p, p+N);
    int ord[N];
    rep(i, N) ord[p[i].second] = i;
    vector<int> lcp(N-1);
    rep(i, N-1){
        string a = p[i].first, b = p[i+1].first;
        lcp[i] = 0;
        while(min(sz(a), sz(b)) > lcp[i] && a[lcp[i]] == b[lcp[i]]) lcp[i]++;
    }
    Sparse_Table<int> st(lcp);

    int M; ll x, d;
    cin >> M >> x >> d;
    ll ans = 0;
    rep(i, M){
        int a = x/(N-1), b = x%(N-1);
        if(a <= b) b++;
        a = ord[a], b = ord[b];
        if(a > b) swap(a, b);
        ans += st.query(a, b);
        x += d, x %= N*(N-1);
    }
    cout << ans << endl;
}
0