結果

問題 No.515 典型LCP
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-05-06 02:33:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 640 ms / 1,000 ms
コード長 2,282 bytes
コンパイル時間 1,910 ms
コンパイル使用メモリ 176,432 KB
実行使用メモリ 15,748 KB
最終ジャッジ日時 2023-10-12 11:41:05
合計ジャッジ時間 7,658 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 640 ms
15,256 KB
testcase_01 AC 627 ms
15,240 KB
testcase_02 AC 351 ms
15,312 KB
testcase_03 AC 7 ms
14,408 KB
testcase_04 AC 7 ms
14,348 KB
testcase_05 AC 138 ms
15,520 KB
testcase_06 AC 189 ms
15,660 KB
testcase_07 AC 143 ms
15,516 KB
testcase_08 AC 259 ms
15,512 KB
testcase_09 AC 277 ms
15,324 KB
testcase_10 AC 336 ms
15,240 KB
testcase_11 AC 336 ms
15,320 KB
testcase_12 AC 336 ms
15,244 KB
testcase_13 AC 224 ms
15,748 KB
testcase_14 AC 14 ms
15,552 KB
testcase_15 AC 107 ms
15,612 KB
testcase_16 AC 108 ms
15,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)



typedef long long ll;
#define def INT_MAX/2
template<class V, int NV> struct SegTree {
    V comp(V l, V r) { return min(l, r); };
    vector<V> val; SegTree() { val = vector<V>(NV * 2, def); }
    V get(int l, int r) { //[l,r]
        if (l > r) return def;
        l += NV; r += NV + 1; V ret = def;
        while (l < r) { if (l & 1) ret = comp(ret, val[l++]); if (r & 1) ret = comp(ret, val[--r]); l /= 2; r /= 2; }
        return ret;
    }
    void update(int i, V v) { i += NV; val[i] = v; while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]); }
    void add(int i, V v) { update(i, val[i + NV] + v); }
};
//-----------------------------------------------------------------
int N, M;
ll X, D;
string S[101010];
//-----------------------------------------------------------------
void init() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> N;
    rep(i, 0, N) cin >> S[i];
    cin >> M >> X >> D;
}
//-----------------------------------------------------------------
int lcp(string a, string b) {
    int cnt = 0;
    rep(i, 0, min(a.length(), b.length())) {
        if (a[i] == b[i]) cnt = i + 1;
        else break;
    }

    return cnt;
}
//-----------------------------------------------------------------
int m[101010], mm[101010];
SegTree<int, 1 << 20> st;
void pre() {
    rep(i, 0, N) mm[i] = i;
    sort(mm, mm + N, [&](int a, int b) { return S[a] < S[b]; });
    rep(i, 0, N) m[mm[i]] = i;
    sort(S, S + N);

    rep(i, 1, N) st.update(i, lcp(S[i - 1], S[i]));
}
//-----------------------------------------------------------------
int main() {
    init();

    pre();

    ll ans = 0;
    ll x = X, d = D, n = N;
    rep(q, 0, M) {
        int i = (x / (n - 1)) + 1;
        int j = (x % (n - 1)) + 1;
        if (i > j) swap(i, j);
        else j++;

        //fprintf(stderr, "Finish : %d/%d\n", i, M);
        int a = m[i - 1], b = m[j - 1];
        if (a > b) swap(a, b);
        if (a == b) {
            ans += S[a].length();
            continue;
        }

        int c = st.get(a + 1, b);

        ans += c;
        //cout << S[a] << " + " << S[b] << " = " << c << endl;
        x = (x + d) % (n * (n - 1));
    }
    cout << ans << endl;
}
0