結果

問題 No.515 典型LCP
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-05-05 23:30:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,197 bytes
コンパイル時間 1,900 ms
コンパイル使用メモリ 175,768 KB
実行使用メモリ 39,124 KB
最終ジャッジ日時 2023-10-12 11:08:09
合計ジャッジ時間 6,044 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 331 ms
38,720 KB
testcase_03 AC 7 ms
16,708 KB
testcase_04 AC 7 ms
16,720 KB
testcase_05 AC 107 ms
39,000 KB
testcase_06 AC 159 ms
39,032 KB
testcase_07 AC 109 ms
38,980 KB
testcase_08 AC 232 ms
38,984 KB
testcase_09 AC 253 ms
38,732 KB
testcase_10 AC 315 ms
38,780 KB
testcase_11 AC 313 ms
38,732 KB
testcase_12 AC 316 ms
38,740 KB
testcase_13 AC 202 ms
38,996 KB
testcase_14 AC 13 ms
17,744 KB
testcase_15 AC 91 ms
39,000 KB
testcase_16 AC 91 ms
39,124 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, X, D, I[3010101], J[3010101];
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;

    ll x = X, d = D;
    rep(i, 0, M) {
        I[i] = (x / (N - 1));
        J[i] = (x % (N - 1));
        if (I[i] > J[i]) swap(I[i], J[i]);
        else J[i]++;
        x = (x + d) % (1LL * N * (N - 1));
    }
}
//-----------------------------------------------------------------
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;
    rep(i, 0, M) {
        int a = m[I[i]], b = m[J[i]];
        if (a > b) swap(a, b);
        
        int c = st.get(a + 1, b);

        ans += c;

        //cout << S[a] << " + " << S[b] << " = " << c << endl;
    }
    cout << ans << endl;
}
0