結果

問題 No.515 典型LCP
ユーザー ei1333333ei1333333
提出日時 2017-05-06 02:48:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 207 ms / 1,000 ms
コード長 1,819 bytes
コンパイル時間 1,330 ms
コンパイル使用メモリ 155,116 KB
実行使用メモリ 61,072 KB
最終ジャッジ日時 2023-10-12 11:44:00
合計ジャッジ時間 4,217 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 207 ms
61,072 KB
testcase_01 AC 196 ms
60,952 KB
testcase_02 AC 130 ms
52,760 KB
testcase_03 AC 11 ms
31,396 KB
testcase_04 AC 11 ms
31,316 KB
testcase_05 AC 108 ms
53,008 KB
testcase_06 AC 113 ms
53,064 KB
testcase_07 AC 111 ms
53,056 KB
testcase_08 AC 125 ms
52,940 KB
testcase_09 AC 111 ms
53,424 KB
testcase_10 AC 113 ms
53,404 KB
testcase_11 AC 114 ms
53,404 KB
testcase_12 AC 113 ms
53,420 KB
testcase_13 AC 117 ms
52,776 KB
testcase_14 AC 27 ms
32,496 KB
testcase_15 AC 106 ms
52,556 KB
testcase_16 AC 107 ms
52,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;
const int INF = 1 << 30;

struct SparseTable
{
  int sz;
  vector< int > small[20], lookup;

  SparseTable(vector< int > &xs) : sz(xs.size())
  {
    lookup.resize(xs.size() + 1);
    lookup[0] = lookup[1] = 0;
    for(int i = 2; i <= xs.size(); i++) lookup[i] = lookup[i >> 1] + 1;
    small[0] = xs;
    for(int i = 1; i < 20; ++i) {
      small[i].resize(xs.size());
      for(int j = 0; j < xs.size(); ++j) {
        small[i][j] = min(small[i - 1][j], small[i - 1][min((int) xs.size() - 1, j + (1 << (i - 1)))]);
      }
    }
  }

  int query(int l, int r)
  {
    const int d = lookup[r - l];
    return min(small[d][l], small[d][r - (1 << d)]);
  }
};


int N, M;
int64 x, d;
string S[800000];
int latte[3000000], malta[3000000];

int main()
{
  cin >> N;

  int all = 0;
  for(int i = 0; i < N; i++) {
    cin >> S[i];
    all += S[i].size();
  }

  cin >> M >> x >> d;
  for(int k = 0; k < M; k++) {
    latte[k] = (x / (N - 1)) + 1;
    malta[k] = (x % (N - 1)) + 1;
    if(latte[k] > malta[k]) swap(latte[k], malta[k]);
    else malta[k] = malta[k] + 1;
    x = (x + d) % (1LL * N * (N - 1));
  }


  vector< int > order(N), rev(N);
  iota(begin(order), end(order), 0);
  sort(begin(order), end(order), [&](int a, int b)
  {
    return (S[a] < S[b]);
  });
  for(int i = 0; i < N; i++) rev[order[i]] = i;


  vector< int > qs(N);
  for(int i = 1; i < N; i++) {
    int tail = 0, sz = min(S[order[i - 1]].size(), S[order[i]].size());
    while(tail < sz && S[order[i - 1]][tail] == S[order[i]][tail]) ++tail;
    qs[i - 1] = tail;
  }
  SparseTable rmq(qs);
  int64 ret = 0;
  for(int i = 0; i < M; i++) {
    auto get = minmax(rev[--latte[i]], rev[--malta[i]]);
    ret += rmq.query(get.first, get.second);
  }
  cout << ret << endl;
}
0