結果

問題 No.515 典型LCP
ユーザー parukiparuki
提出日時 2017-05-06 10:45:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 241 ms / 1,000 ms
コード長 3,086 bytes
コンパイル時間 2,284 ms
コンパイル使用メモリ 179,176 KB
実行使用メモリ 15,256 KB
最終ジャッジ日時 2023-10-12 14:58:36
合計ジャッジ時間 5,143 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 230 ms
15,204 KB
testcase_01 AC 241 ms
15,256 KB
testcase_02 AC 147 ms
4,436 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 86 ms
4,352 KB
testcase_06 AC 98 ms
4,368 KB
testcase_07 AC 87 ms
4,348 KB
testcase_08 AC 134 ms
4,356 KB
testcase_09 AC 88 ms
4,368 KB
testcase_10 AC 89 ms
4,348 KB
testcase_11 AC 87 ms
4,352 KB
testcase_12 AC 86 ms
4,348 KB
testcase_13 AC 88 ms
4,352 KB
testcase_14 AC 6 ms
4,352 KB
testcase_15 AC 82 ms
4,356 KB
testcase_16 AC 83 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

template<class Cmp>
class SparseTableRMQ {
public:
    SparseTableRMQ() {
    }
    SparseTableRMQ(vector<int> &ar) : n((int)ar.size()), data(n) {
        for (int i = 0; i < n; ++i) data[i] = ar[i];
        table = buildRMQ();
    }

    int queryPos(int l, int r) {
        int k = lgInt(r - l - 1);
        int lid = table[l + n*k], rid = table[r + n*k - (1 << k)];
        return (cmp(data[lid], data[rid])) ? lid : rid;
    }

    int queryValue(int l, int r) {
        return data[queryPos(l, r)];
    }
private:
    int n;
    vector<int> data, table;
    Cmp cmp;
    int lgInt(int v) {
        static const unsigned b[] = { 0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000 };
        static const unsigned S[] = { 1, 2, 4, 8, 16 };
        int res = 0;
        for (int i = 4; i >= 0; i--) {
            if (v & b[i]) {
                v >>= S[i];
                res |= S[i];
            }
        }
        return res;
    }

    vector<int> buildRMQ() {
        int logn = 1;
        for (int k = 1; k < n; k *= 2) ++logn;
        vector<int> b(n*logn);
        for (int i = 0; i < n; ++i)b[i] = i;
        int cur = 0;
        for (int k = 1; k < n; k *= 2) {
            copy(b.begin() + cur, b.begin() + cur + n, b.begin() + cur + n);
            cur += n;
            for (int i = 0; i < n - k; ++i) {
                int idl = b[cur + i], idr = b[cur + i + k];
                if (cmp(data[idl], data[idr])) {
                    b[cur + i] = idl;
                }
                else {
                    b[cur + i] = idr;
                }
            }
        }
        return b;
    }
};
typedef SparseTableRMQ<less_equal<int>> RMinQ;
typedef SparseTableRMQ<greater_equal<int>> RMaxQ;

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

    int N;
    cin >> N;
    vector<pair<string, int>> S(N);
    
    rep(i, N) {
        cin >> S[i].first;
        S[i].second = i;
    }
    sort(all(S));
    vi id(N), lcp(N - 1);
    rep(i, N)id[S[i].second] = i;
    rep(i, N - 1) {
        string &s = S[i].first, &t = S[i + 1].first;
        rep(j, min(sz(s), sz(t))) {
            if (s[j] != t[j])break;
            lcp[i] = j + 1;
        }
    }
    RMinQ rmq(lcp);

    ll M, x, d, ans = 0;
    cin >> M >> x >> d;
    while (M--) {
        ll l = (x / (N - 1)) + 1, r = (x % (N - 1)) + 1;
        if (l > r)swap(l, r);
        else r++;
        x = (x + d) % (N*(N - 1ll));
        int lb = min(id[--l], id[--r]), ub = id[l] ^ id[r] ^ lb;
        ans += rmq.queryValue(lb, ub);
    }
    cout << ans << endl;
}
0