結果
| 問題 |
No.515 典型LCP
|
| コンテスト | |
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2017-05-06 02:29:16 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,331 bytes |
| コンパイル時間 | 2,214 ms |
| コンパイル使用メモリ | 178,640 KB |
| 実行使用メモリ | 39,168 KB |
| 最終ジャッジ日時 | 2024-09-14 10:26:57 |
| 合計ジャッジ時間 | 6,531 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 13 WA * 2 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
typedef long long ll;
#define def INT_MAX/2
template<class V, int NV> struct SegTree {
V comp(V l, V r) { return min(l, r); };
vector<V> val; SegTree() { val = vector<V>(NV * 2, def); }
V get(int l, int r) { //[l,r]
if (l > r) return def;
l += NV; r += NV + 1; V ret = def;
while (l < r) { if (l & 1) ret = comp(ret, val[l++]); if (r & 1) ret = comp(ret, val[--r]); l /= 2; r /= 2; }
return ret;
}
void update(int i, V v) { i += NV; val[i] = v; while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]); }
void add(int i, V v) { update(i, val[i + NV] + v); }
};
//-----------------------------------------------------------------
int N, M, X, D, I[3010101], J[3010101];
string S[101010];
//-----------------------------------------------------------------
void init() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
rep(i, 0, N) cin >> S[i];
cin >> M >> X >> D;
ll x = X, d = D, n = N;
rep(i, 0, M) {
I[i] = (x / (n - 1));
J[i] = (x % (n - 1));
if (I[i] > J[i]) swap(I[i], J[i]);
else J[i]++;
x = (x + d) % (n * (n - 1));
}
}
//-----------------------------------------------------------------
int lcp(string a, string b) {
int cnt = 0;
rep(i, 0, min(a.length(), b.length())) {
if (a[i] == b[i]) cnt = i + 1;
else break;
}
return cnt;
}
//-----------------------------------------------------------------
int m[101010], mm[101010];
SegTree<int, 1 << 20> st;
void pre() {
rep(i, 0, N) mm[i] = i;
sort(mm, mm + N, [&](int a, int b) { return S[a] < S[b]; });
rep(i, 0, N) m[mm[i]] = i;
sort(S, S + N);
rep(i, 1, N) st.update(i, lcp(S[i - 1], S[i]));
}
//-----------------------------------------------------------------
int main() {
init();
pre();
ll ans = 0;
rep(i, 0, M) {
//fprintf(stderr, "Finish : %d/%d\n", i, M);
int a = m[I[i]], b = m[J[i]];
if (a > b) swap(a, b);
if (a == b) {
ans += S[a].length();
continue;
}
int c = st.get(a + 1, b);
ans += c;
//cout << S[a] << " + " << S[b] << " = " << c << endl;
}
cout << ans << endl;
}
はまやんはまやん