結果
| 問題 | 
                            No.515 典型LCP
                             | 
                    
| コンテスト | |
| ユーザー | 
                             chocorusk
                         | 
                    
| 提出日時 | 2019-10-27 15:16:33 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 230 ms / 1,000 ms | 
| コード長 | 2,319 bytes | 
| コンパイル時間 | 1,562 ms | 
| コンパイル使用メモリ | 125,804 KB | 
| 実行使用メモリ | 14,628 KB | 
| 最終ジャッジ日時 | 2024-09-14 21:03:26 | 
| 合計ジャッジ時間 | 4,048 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 15 | 
ソースコード
#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;
}
            
            
            
        
            
chocorusk