結果

問題 No.962 LCPs
ユーザー polylogKpolylogK
提出日時 2019-12-20 23:09:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 463 ms / 2,000 ms
コード長 3,622 bytes
コンパイル時間 1,895 ms
コンパイル使用メモリ 172,868 KB
実行使用メモリ 13,112 KB
最終ジャッジ日時 2023-10-12 02:16:22
合計ジャッジ時間 10,456 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 3 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 3 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,356 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 463 ms
13,112 KB
testcase_18 AC 216 ms
8,388 KB
testcase_19 AC 214 ms
8,100 KB
testcase_20 AC 138 ms
6,820 KB
testcase_21 AC 138 ms
6,852 KB
testcase_22 AC 100 ms
5,516 KB
testcase_23 AC 101 ms
5,520 KB
testcase_24 AC 81 ms
5,264 KB
testcase_25 AC 81 ms
5,332 KB
testcase_26 AC 65 ms
4,988 KB
testcase_27 AC 221 ms
5,756 KB
testcase_28 AC 117 ms
5,068 KB
testcase_29 AC 99 ms
4,764 KB
testcase_30 AC 171 ms
5,488 KB
testcase_31 AC 248 ms
6,744 KB
testcase_32 AC 93 ms
4,428 KB
testcase_33 AC 332 ms
7,744 KB
testcase_34 AC 119 ms
5,008 KB
testcase_35 AC 151 ms
5,008 KB
testcase_36 AC 145 ms
5,220 KB
testcase_37 AC 76 ms
4,476 KB
testcase_38 AC 77 ms
4,432 KB
testcase_39 AC 78 ms
4,460 KB
testcase_40 AC 75 ms
4,432 KB
testcase_41 AC 75 ms
4,432 KB
testcase_42 AC 77 ms
4,480 KB
testcase_43 AC 79 ms
4,716 KB
testcase_44 AC 77 ms
4,436 KB
testcase_45 AC 76 ms
4,460 KB
testcase_46 AC 77 ms
4,484 KB
testcase_47 AC 4 ms
4,348 KB
testcase_48 AC 5 ms
4,352 KB
testcase_49 AC 5 ms
4,348 KB
testcase_50 AC 5 ms
4,348 KB
testcase_51 AC 5 ms
4,352 KB
testcase_52 AC 4 ms
4,352 KB
testcase_53 AC 5 ms
4,352 KB
testcase_54 AC 4 ms
4,352 KB
testcase_55 AC 4 ms
4,348 KB
testcase_56 AC 5 ms
4,348 KB
testcase_57 AC 69 ms
4,348 KB
testcase_58 AC 70 ms
4,352 KB
testcase_59 AC 68 ms
4,352 KB
testcase_60 AC 6 ms
4,352 KB
testcase_61 AC 6 ms
4,348 KB
testcase_62 AC 6 ms
4,348 KB
testcase_63 AC 301 ms
10,208 KB
testcase_64 AC 6 ms
4,352 KB
testcase_65 AC 302 ms
10,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0