結果

問題 No.515 典型LCP
ユーザー chocoruskchocorusk
提出日時 2019-10-27 15:16:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 226 ms / 1,000 ms
コード長 2,319 bytes
コンパイル時間 1,493 ms
コンパイル使用メモリ 124,716 KB
実行使用メモリ 14,628 KB
最終ジャッジ日時 2023-10-12 22:52:42
合計ジャッジ時間 4,271 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 226 ms
14,324 KB
testcase_01 AC 219 ms
14,628 KB
testcase_02 AC 114 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 84 ms
4,352 KB
testcase_06 AC 83 ms
4,480 KB
testcase_07 AC 83 ms
4,472 KB
testcase_08 AC 99 ms
4,588 KB
testcase_09 AC 84 ms
4,928 KB
testcase_10 AC 86 ms
5,088 KB
testcase_11 AC 86 ms
4,924 KB
testcase_12 AC 86 ms
4,976 KB
testcase_13 AC 85 ms
4,348 KB
testcase_14 AC 17 ms
4,348 KB
testcase_15 AC 83 ms
4,352 KB
testcase_16 AC 83 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<typename T, bool ismin=true>
struct SparseTable{
    vector<vector<T>> spt;
    vector<int> vlog;
    SparseTable(const vector<T> &v){
        int b=0, n=v.size();
        while((1<<b)<=n) b++;
        spt.resize(b, vector<T>(n));
        for(int i=0; i<n; i++) spt[0][i]=v[i];
        for(int i=1; i<b; i++){
            for(int j=0; j+(1<<i)<=n; j++){
                if(ismin) spt[i][j]=min(spt[i-1][j], spt[i-1][j+(1<<(i-1))]);
                else spt[i][j]=max(spt[i-1][j], spt[i-1][j+(1<<(i-1))]);
            }
        }
        vlog.resize(n+1);
        for(int i=2; i<=n; i++) vlog[i]=vlog[i>>1]+1;
    }
    inline T rmq(int l, int r){
        int b=vlog[r-l];
        if(ismin) return min(spt[b][l], spt[b][r-(1<<b)]);
        else return max(spt[b][l], spt[b][r-(1<<b)]);
    }
};
int main()
{
	int n;
    cin>>n;
    vector<string> s(n);
    for(int i=0; i<n; i++){
        cin>>s[i];
    }
    vector<int> ind(n), rk(n);
    iota(ind.begin(), ind.end(), 0);
    sort(ind.begin(), ind.end(), [&](int i, int j){ return s[i]<s[j];});
    for(int i=0; i<n; i++) rk[ind[i]]=i;
    vector<int> lcp(n-1);
    for(int i=0; i<n-1; i++){
        int p=ind[i], q=ind[i+1];
        int mn=min(s[p].size(), s[q].size());
        for(int j=0; j<=mn; j++){
            if(j==mn){
                lcp[i]=mn; break;
            }
            if(s[p][j]!=s[q][j]){
                lcp[i]=j; break;
            }
        }
    }
    SparseTable<int> spt(lcp);
    int m;
    ll x, d;
    cin>>m>>x>>d;
    ll ans=0;
    for(int i=0; i<m; i++){
        int p=x/(n-1), q=x%(n-1);
        if(p>q) swap(p, q);
        else q++;
        x=(x+d)%((ll)n*(n-1));
        int p1=rk[p], q1=rk[q];
        if(p1>q1) swap(p1, q1);
        ans+=spt.rmq(p1, q1);
    }
    cout<<ans<<endl;
    return 0;
}
0