結果

問題 No.515 典型LCP
ユーザー kazumakazuma
提出日時 2017-07-26 18:49:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 633 ms / 1,000 ms
コード長 1,722 bytes
コンパイル時間 2,851 ms
コンパイル使用メモリ 209,976 KB
実行使用メモリ 12,032 KB
最終ジャッジ日時 2024-04-18 00:03:46
合計ジャッジ時間 8,023 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 633 ms
11,904 KB
testcase_01 AC 632 ms
12,032 KB
testcase_02 AC 350 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 96 ms
5,376 KB
testcase_06 AC 153 ms
5,376 KB
testcase_07 AC 98 ms
5,376 KB
testcase_08 AC 246 ms
5,376 KB
testcase_09 AC 245 ms
5,376 KB
testcase_10 AC 284 ms
5,376 KB
testcase_11 AC 283 ms
5,376 KB
testcase_12 AC 284 ms
5,376 KB
testcase_13 AC 171 ms
5,376 KB
testcase_14 AC 9 ms
5,376 KB
testcase_15 AC 83 ms
5,376 KB
testcase_16 AC 83 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

class SegmentTree {
	static const int id = 1e9;
	const int n;
	vector<int> data;
	int size(int n) {
		int res = 1;
		while (res < n) res <<= 1;
		return res;
	}
public:
	SegmentTree(int n_) :
		n(size(n_)), data(n * 2, id) {}
	void Init(const vector<int>& data_) {
		for (int i = 0; i < (int)data_.size(); i++)
			data[i + n] = data_[i];
		for (int i = n - 1; i >= 0; i--)
			data[i] = min(data[i * 2], data[i * 2 + 1]);
	}
	int Find(int l, int r) {
		l += n; r += n;
		int res1 = id, res2 = id;
		while (l < r) {
			if (l & 1) res1 = min(res1, data[l++]);
			if (r & 1) res2 = min(data[--r], res2);
			l >>= 1; r >>= 1;
		}
		return min(res1, res2);
	}
};

vector<int> LCP(const vector<string>& Ss) {
	int N = Ss.size();
	vector<int> res(N);
	for (int i = 1; i < N; i++) {
		auto& S = Ss[i - 1];
		auto& T = Ss[i];
		int j = 0, ss = S.size(), ts = T.size();
		while (j < ss && j < ts && S[j] == T[j]) {
			j++;
		}
		res[i - 1] = j;
	}
	return res;
}

int main()
{
	cin.sync_with_stdio(false);
	ll N, M;
	ll x, d;
	cin >> N;
	vector<pair<string, int>> s(N);
	for (int i = 0; i < N; i++) {
		cin >> s[i].first;
		s[i].second = i;
	}
	sort(s.begin(), s.end());
	vector<string> b(N);
	vector<int> trans(N);
	for (int i = 0; i < N; i++) {
		b[i] = s[i].first;
		trans[s[i].second] = i;
	}
	SegmentTree rmq(N);
	auto lcp = LCP(b);
	rmq.Init(lcp);
	cin >> M >> x >> d;
	ll res = 0;
	for (int i = 0; i < M; i++) {
		ll l = x / (N - 1), r = x % (N - 1);
		if (l > r) {
			swap(l, r);
		}
		else {
			r++;
		}
		x = (x + d) % (N * (N - 1));
		l = trans[l];
		r = trans[r];
		if (l > r) swap(l, r);
		res += rmq.Find(l, r);
	}
	cout << res << endl;
	return 0;
}
0