結果
| 問題 |
No.515 典型LCP
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-05-06 06:17:02 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 648 ms / 1,000 ms |
| コード長 | 1,498 bytes |
| コンパイル時間 | 476 ms |
| コンパイル使用メモリ | 43,596 KB |
| 実行使用メモリ | 224,856 KB |
| 最終ジャッジ日時 | 2024-09-14 13:23:02 |
| 合計ジャッジ時間 | 5,541 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 15 |
ソースコード
#include <cstdio>
#include <algorithm>
constexpr int N = 800001;
char buf[N];
int trie[N][26];
int pos[100000];
int eulerID[N];
int now;
int rmq[21][N * 2];
int lg[N * 2];
int readint() {
int n;
scanf("%d", &n);
return n;
}
long long readlong() {
long long n;
scanf("%lld", &n);
return n;
}
void dfs(int u, int depth) {
eulerID[u] = now;
rmq[0][now++] = depth;
for (int i = 0; i < 26; i++) {
if (!trie[u][i]) {
continue;
}
dfs(trie[u][i], depth + 1);
rmq[0][now++] = depth;
}
}
int main() {
const int n = readint();
int cnt = 1;
for (int i = 0; i < n; i++) {
scanf("%s", buf);
int u = 0;
for (int j = 0; buf[j] != 0; j++) {
char c = buf[j] - 'a';
if (!trie[u][c]) {
trie[u][c] = cnt++;
}
u = trie[u][c];
}
pos[i] = u;
}
dfs(0, 0);
for (int i = 2; i <= now; i++) {
lg[i] = lg[i / 2] + 1;
}
for (int i = 0; i + 1 <= lg[now]; i++) {
for (int j = 0; j + (1 << i) < now; j++) {
rmq[i + 1][j] = std::min(rmq[i][j], rmq[i][j + (1 << i)]);
}
}
const int m = readint();
long long x = readlong();
const long long d = readlong();
long long ans = 0;
for (int foo = 0; foo < m; foo++) {
int i = x / (n - 1);
int j = x % (n - 1);
if (i > j) {
std::swap(i, j);
} else {
j++;
}
x = (x + d) % (1LL * n * (n - 1));
int l = eulerID[pos[i]];
int r = eulerID[pos[j]];
if (l > r) {
std::swap(l, r);
}
r++;
const int k = lg[r - l];
ans += std::min(rmq[k][l], rmq[k][r - (1 << k)]);
}
printf("%lld\n", ans);
}