結果
| 問題 | No.962 LCPs |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-12-20 23:09:56 |
| 言語 | C++14 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 437 ms / 2,000 ms |
| コード長 | 3,622 bytes |
| 記録 | |
| コンパイル時間 | 6,290 ms |
| コンパイル使用メモリ | 175,512 KB |
| 実行使用メモリ | 13,180 KB |
| 最終ジャッジ日時 | 2024-09-14 02:04:07 |
| 合計ジャッジ時間 | 10,766 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 64 |
ソースコード
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;
template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
#define NDEBUG
template<typename Monoid>
class segment_tree {
using value_type = Monoid;
using size_type = size_t;
using binary_function = std::function<value_type(value_type, value_type)>;
using checker = std::function<bool(value_type)>;
const size_type size_;
size_type height_;
const value_type id;
const binary_function bi_func;
std::vector<value_type> data;
private:
const size_type get_height(const size_type& size) const {
size_type height = 1;
while(1 << height <= size) height++;
return height;
}
const size_type base_size() const {
return 1 << height_;
}
void meld(const size_type& index) {
data[index] = bi_func(data[index << 1 ^ 0], data[index << 1 ^ 1]);
}
public:
segment_tree(const size_type& size, const binary_function& bi_func, const value_type& id)
: size_(size),
bi_func(bi_func),
id(id) {
height_ = get_height(size);
data.assign(base_size() << 1, id);
}
value_type fold(size_type left, size_type right) {
value_type l_value = id,
r_value = id;
for(left += base_size(), right += base_size();
left < right;
left >>= 1, right >>= 1) {
if(left & 1) l_value = bi_func(l_value, data[left++]);
if(right & 1) r_value = bi_func(data[--right], r_value);
}
return bi_func(std::move(l_value), std::move(r_value));
}
void update(size_type index, const value_type& value) {
index += base_size();
data[index] = bi_func(data[index], value);
while(index >>= 1) meld(index);
}
void change(size_type index, const value_type& value) {
index += base_size();
data[index] = value;
while(index >>= 1) meld(index);
}
const size_type search(const size_type & left, const checker & check) {
value_type val = id;
size_type k = left + base_size();
while(true) {
if(check(bi_func(val, data[k]))) {
val = bi_func(val, data[k]);
if(k & 1) {
if((k + 1) & k) k = (k + 1) >> 1;
else return size();
} else {
k = k + 1;
}
} else {
if(k < base_size()) {
k = k << 1 ^ 0;
} else {
return k - base_size();
}
}
}
}
value_type operator[](const size_type& index) const {
return data[index + base_size()];
}
const size_type size() const {
return size_;
}
const bool empty() const {
return data.empty();
}
};
int main() {
int n; scanf("%d", &n); std::vector<std::string> s(n);
for(int i = 0; i < n; i++) cin >> s[i];
std::vector<i64> a(n - 1);
for(int i = 0; i < n - 1; i++) {
for(int j = 0; j < std::min(s[i + 1].size(), s[i].size()); j++) {
if(s[i][j] != s[i + 1][j]) break;
a[i] = j + 1;
}
}
i64 ans = 0;
for(auto v: s) ans += v.size();
auto f = [](int a, int b) { return std::min(a, b); };
segment_tree<int> seg(a.size() + 1, f, 1 << 30);
for(int i = 0; i < a.size(); i++) seg.change(i, a[i]);
for(int i = 0; i < a.size(); i++) {
int now = i;
while(now < a.size()) {
int ok = a.size() + 1, ng = now + 1;
while(std::abs(ok - ng) > 1) {
int mid = (ok + ng) >> 1;
if(seg.fold(i, mid) < a[now]) ok = mid;
else ng = mid;
}
int nxt = ok - 1;
i64 S = now - i + 2, T = nxt - i + 1;
i64 len = (S + T) * (T - S + 1) >> 1;
ans += len * a[now];
now = nxt;
}
}
printf("%lld\n", ans);
return 0;
}