結果

問題 No.515 典型LCP
ユーザー mamekinmamekin
提出日時 2017-05-06 10:23:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 215 ms / 1,000 ms
コード長 2,294 bytes
コンパイル時間 1,626 ms
コンパイル使用メモリ 121,052 KB
実行使用メモリ 37,724 KB
最終ジャッジ日時 2023-10-12 14:57:25
合計ジャッジ時間 4,447 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 209 ms
37,668 KB
testcase_01 AC 215 ms
37,724 KB
testcase_02 AC 140 ms
27,436 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 122 ms
27,560 KB
testcase_06 AC 123 ms
27,696 KB
testcase_07 AC 124 ms
27,504 KB
testcase_08 AC 131 ms
27,696 KB
testcase_09 AC 123 ms
27,456 KB
testcase_10 AC 125 ms
27,680 KB
testcase_11 AC 125 ms
27,544 KB
testcase_12 AC 124 ms
27,436 KB
testcase_13 AC 127 ms
27,504 KB
testcase_14 AC 6 ms
4,476 KB
testcase_15 AC 122 ms
27,592 KB
testcase_16 AC 122 ms
27,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

class SparseTable
{
private:
    vector<int> logTable;
    vector<vector<int> > data;
public:
    SparseTable(const vector<int>& v){
        int n = v.size();
        logTable.assign(n+1, 0);
        for(int i=2; i<=n; ++i)
            logTable[i] = logTable[i>>1] + 1;

        data.assign(1, v);
        for(int i=1; (1<<i)<=n; ++i){
            int len = 1 << i;
            data.push_back(vector<int>(n - len + 1));
            for(int j=0; j<n-len+1; ++j)
                data[i][j] = min(data[i-1][j], data[i-1][j+len/2]);
        }
    }
    int query(int a, int b){
        int i = logTable[b-a+1];
        int len = 1 << i;
        return min(data[i][a], data[i][b-len+1]);
    }
};

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

    int n;
    cin >> n;
    vector<pair<string, int> > v(n);
    for(int i=0; i<n; ++i){
        cin >> v[i].first;
        v[i].second = i;
    }
    sort(v.begin(), v.end());
    vector<int> index(n);
    for(int i=0; i<n; ++i)
        index[v[i].second] = i;

    vector<int> len(n-1, 0);
    for(int i=0; i<n-1; ++i){
        int maxLen = min(v[i].first.size(), v[i+1].first.size());
        while(len[i] < maxLen && v[i].first[len[i]] == v[i+1].first[len[i]])
            ++ len[i];
    }
    SparseTable st(len);

    int m;
    long long x, d;
    cin >> m >> x >> d;
    vector<int> a(m), b(m);
    for(int i=0; i<m; ++i){
        a[i] = (int)(x / (n - 1));
        b[i] = (int)(x % (n - 1));
        if(a[i] > b[i])
            swap(a[i], b[i]);
        else
            ++ b[i];
        x = (x + d) % (n * (n - 1LL));
    }

    long long ans = 0;
    for(int i=0; i<m; ++i){
        int a2 = index[a[i]];
        int b2 = index[b[i]];
        if(a2 > b2)
            swap(a2, b2);
        ans += st.query(a2, b2 - 1);
    }
    cout << ans << endl;

    return 0;
}
0