結果

問題 No.515 典型LCP
ユーザー FF256grhyFF256grhy
提出日時 2017-05-06 05:40:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 738 ms / 1,000 ms
コード長 2,837 bytes
コンパイル時間 2,348 ms
コンパイル使用メモリ 153,924 KB
実行使用メモリ 9,988 KB
最終ジャッジ日時 2023-10-12 14:09:20
合計ジャッジ時間 8,567 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 733 ms
8,716 KB
testcase_01 AC 738 ms
8,832 KB
testcase_02 AC 404 ms
9,196 KB
testcase_03 AC 5 ms
8,276 KB
testcase_04 AC 4 ms
8,304 KB
testcase_05 AC 134 ms
9,244 KB
testcase_06 AC 217 ms
9,284 KB
testcase_07 AC 152 ms
9,088 KB
testcase_08 AC 283 ms
9,276 KB
testcase_09 AC 294 ms
9,920 KB
testcase_10 AC 338 ms
9,856 KB
testcase_11 AC 333 ms
9,988 KB
testcase_12 AC 335 ms
9,844 KB
testcase_13 AC 205 ms
9,216 KB
testcase_14 AC 23 ms
9,024 KB
testcase_15 AC 106 ms
9,300 KB
testcase_16 AC 107 ms
9,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define UB upper_bound
#define LB lower_bound
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

LL n, m, x, d;
pair<string, int> p[100000];
int rev[100000];

const int BS = 1 << 17;
int a[2 * BS];

LL lcp(int i, int j) {
	int c = 0;
	inc(k, min(p[i].FI.size(), p[j].FI.size())) {
		if(p[i].FI[k] == p[j].FI[k]) { c++; } else { break; }
	}
	return c;
}

int rmq(int i, int j) {
	int v = BS;
	
	if(i >= j) { return v; }
	if(i + 1 == j) { return a[i]; }
	
	if(i % 2 == 1) { setmin(v, a[i]); }
	if(j % 2 == 1) { setmin(v, a[j - 1]); }
	
	return min(v, rmq((i + 1) / 2, j / 2));
}

int main() {
	cin >> n;
	inc(i, n) { cin >> p[i].FI; p[i].SE = i; }
	cin >> m >> x >> d;
	
	sort(p, p + n);
	inc(i, n) { rev[ p[i].SE ] = i; }
	
	// inc(i, n) { cout << p[i].FI << endl; }
	
	inc(i, 2 * BS) { a[i] = BS; }
	inc(i, n - 1) { a[BS + i] = lcp(i, i + 1); }
	dec(i, BS) { a[i] = min(a[i * 2], a[i * 2 + 1]); }
	
	LL i, j, ans = 0;
	inc(k, m) {
		i = (x / (n - 1));
		j = (x % (n - 1));
		if(i > j) { swap(i, j); } else { j++; }
		x = (x + d) % (n * (n - 1));
		
		LL v = rmq(BS + min(rev[i], rev[j]), BS + max(rev[i], rev[j]));
		ans += v;
		
		/*
		cout << i << ": " << p[ rev[i] ].FI << endl;
		cout << j << ": " << p[ rev[j] ].FI << endl;
		cout << v << "  ";
		assert(v <= 10);
		inc(abc, v) { cout << "="; } cout << "\n\n";
		*/
	}
	
	cout << ans << endl;
	
	return 0;
}
0