結果

問題 No.515 典型LCP
ユーザー 👑 rin204rin204
提出日時 2022-10-02 20:01:18
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 693 ms / 1,000 ms
コード長 2,067 bytes
コンパイル時間 7,224 ms
コンパイル使用メモリ 310,788 KB
実行使用メモリ 23,980 KB
最終ジャッジ日時 2023-08-26 17:40:54
合計ジャッジ時間 14,104 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 693 ms
23,980 KB
testcase_01 AC 628 ms
21,576 KB
testcase_02 AC 402 ms
16,456 KB
testcase_03 AC 1 ms
4,356 KB
testcase_04 AC 1 ms
4,356 KB
testcase_05 AC 271 ms
23,172 KB
testcase_06 AC 308 ms
23,132 KB
testcase_07 AC 253 ms
23,284 KB
testcase_08 AC 378 ms
23,176 KB
testcase_09 AC 279 ms
16,580 KB
testcase_10 AC 301 ms
16,296 KB
testcase_11 AC 318 ms
16,476 KB
testcase_12 AC 307 ms
16,296 KB
testcase_13 AC 241 ms
16,284 KB
testcase_14 AC 38 ms
16,360 KB
testcase_15 AC 213 ms
23,032 KB
testcase_16 AC 210 ms
23,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include<atcoder/string>
#include<atcoder/segtree>
using namespace std;
using namespace atcoder;

using S = int;

S op(S l, S r){
    if(l <= r) return l;
    else return r;
}

S e(){
    return 1 << 30;
}

void solve(){
    int n;
    cin >> n;
    vector<int> le;
    int ind = 0;
    string S;
    vector<int> size(n);
    for(int i = 0; i < n; i++){
        le.push_back(ind);
        string s;
        cin >> s;
        S += s;
        ind += s.size();
        size[i] = s.size();
    }
    auto sa = suffix_array(S);
    auto lcp = lcp_array(S, sa);
    vector<int> inv(ind);
    for(int i = 0; i < ind; i++){
        inv[sa[i]] = i;
    }
    vector<int> pos(n);
    vector<pair<int, int>> lst;
    for(int i = 0; i < n; i++){
        int l = le[i];
        pos[i] = inv[l];
        lst.push_back({inv[l], i});
    }

    sort(lst.begin(), lst.end());
    map<int, int> mp;
    vector<int> A;
    for(int i = 0; i < n; i++){
        int l = lst[i].first;
        mp[l] = i;
        if(i != 0){
            int mi = 1 << 30;
            for(int j = lst[i - 1].first; j < l; j++){
                mi = min(mi, lcp[j]);
            }
            int ii = lst[i].second;
            int jj = lst[i - 1].second;
            mi = min(mi, min(size[ii], size[jj]));
            A.push_back(mi);
        }
    }

    for(int i = 0; i < n; i++){
        pos[i] = mp[pos[i]];
    }

    segtree<int, op, e> seg(A);

    int m;
    long long x, d;
    cin >> m >> x >> d;
    long long ans = 0;
    while(m--){
        int i = x / (n - 1);
        int j = x - i * (n - 1);        
        if(i > j) swap(i, j);
        else j++;
        
        
        int l = pos[i];
        int r = pos[j];
        if(l > r) swap(l, r);
        ans += seg.prod(l, r);
        x = (x + d) % ((long long) n * (n - 1));
    }
    cout << ans << "\n";
}


int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t;
    t = 1;
    // cin >> t;
    while(t--) solve();
}




0