結果

問題 No.515 典型LCP
ユーザー tossytossy
提出日時 2017-11-17 22:41:29
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 1,000 ms
コード長 2,441 bytes
コンパイル時間 1,905 ms
コンパイル使用メモリ 175,068 KB
実行使用メモリ 20,700 KB
最終ジャッジ日時 2023-08-16 19:12:22
合計ジャッジ時間 4,497 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 209 ms
20,660 KB
testcase_01 AC 199 ms
20,700 KB
testcase_02 AC 119 ms
4,388 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 87 ms
4,380 KB
testcase_06 AC 95 ms
4,376 KB
testcase_07 AC 89 ms
4,380 KB
testcase_08 AC 113 ms
4,380 KB
testcase_09 AC 87 ms
4,380 KB
testcase_10 AC 92 ms
4,376 KB
testcase_11 AC 90 ms
4,376 KB
testcase_12 AC 92 ms
4,376 KB
testcase_13 AC 89 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 82 ms
4,380 KB
testcase_16 AC 79 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

template<typename T>
class SparseTable {
public:
  vector<T> tbl[20];
  vector<T> tbr[20];
  int n;
  T (*op)(T, T);
  SparseTable(const vector<T> &v, T (*_op)(T, T)) : op(_op){
    // build table O(NlogN)
    n = v.size();
    tbl[0].resize(n+1);
    tbr[0].resize(n+1);
    rep(i,n) tbl[0][i] = tbr[0][i] = v[i];
    for(int k=1; (1<<k)<=n; k++){
      tbl[k].resize(n+1); tbr[k].resize(n+1);
      int mask = (1<<k)-1;
      rep(i,n){
        if((i&mask) == 0) tbr[k][i] = v[i];
        else tbr[k][i] = op(tbr[k][i-1], v[i]);
      }
      for(int i=n-1; i>=0; i--){
        if(((i+1)&mask) == 0) tbl[k][i] = v[i];
        else tbl[k][i] = op(tbl[k][i+1], v[i]);
      }
    }
  }
  T query(int l, int r){ // [l,r)
    if(l+1 == r) return tbl[0][l];
    r--; // [l,r]
    int k = 31 - __builtin_clz(l^r); // 2^k <= l^r < 2^{k+1}
    return op(tbl[k][l], tbr[k][r]);
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  long n;
  cin>>n;
  vector<string> v(n);
  rep(i,n) cin>>v[i];
  vector<int> id(n);
  rep(i,n) id[i] = i;
  sort(all(id), [&](const int l, const int r){
    return v[l] < v[r];
  });

  auto lcp = [&](int p, int q){
    rep(i,min(v[p].size(), v[q].size())) if(v[p][i]!=v[q][i]) return i;
    return (int)min(v[p].size(), v[q].size());
  };

  // lcp btw i,i+1
  vector<int> lcp_arr(n,0);
  rep(i,n-1){
    lcp_arr[i] = lcp(id[i], id[i+1]);
  }
// dbg(lcp_arr);
  SparseTable<int> st(lcp_arr, [](const int l, const int r){return min(l,r);});
  vector<int> pos(n);
  rep(i,n) pos[id[i]] = i;

  long m,x,d;
  cin>>m>>x>>d;

  long ans = 0;
  rep(_,m){
    long i = (x/(n-1));
    long j = (x%(n-1));
    if(i<=j) j++;

    int l = min(pos[i], pos[j]), r = max(pos[i], pos[j]);

    ans += st.query(l, r);
    // dbg(st.query(l,r));
    x = (x+d)%(n*(n-1));
  }

  cout << ans << endl;

  return 0;
}
0