結果

問題 No.515 典型LCP
ユーザー FF256grhyFF256grhy
提出日時 2017-05-06 07:25:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 828 ms / 1,000 ms
コード長 3,620 bytes
コンパイル時間 1,598 ms
コンパイル使用メモリ 154,468 KB
実行使用メモリ 9,080 KB
最終ジャッジ日時 2023-10-12 14:44:29
合計ジャッジ時間 8,399 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 816 ms
8,212 KB
testcase_01 AC 828 ms
8,284 KB
testcase_02 AC 453 ms
8,140 KB
testcase_03 AC 3 ms
7,308 KB
testcase_04 AC 4 ms
7,268 KB
testcase_05 AC 149 ms
8,332 KB
testcase_06 AC 263 ms
8,340 KB
testcase_07 AC 184 ms
8,256 KB
testcase_08 AC 318 ms
8,300 KB
testcase_09 AC 343 ms
9,080 KB
testcase_10 AC 387 ms
8,936 KB
testcase_11 AC 386 ms
8,820 KB
testcase_12 AC 385 ms
8,868 KB
testcase_13 AC 231 ms
8,236 KB
testcase_14 AC 23 ms
7,992 KB
testcase_15 AC 111 ms
8,052 KB
testcase_16 AC 112 ms
8,128 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];

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 MIN(int x, int y) { return min(x, y); }

template<typename T> class BIT {
private:
	T * a;
	int M, S;
	T (*F)(T, T);
	T Z;

public:
	// [0, size) が使用可能になる、func が演算、zero は単位元
	BIT(int size, T (*func)(T, T), T zero) {
		assert(size > 0);
		M = size;
		F = func;
		Z = zero;
		S = 1;
		while(S < size) { S *= 2; }
		a = new T[S * 2];
		inc(i, S * 2) { a[i] = Z; }
	}
	
	// テーブルの再計算
	void calc() {
		decID(i, 1, S) {
			a[i] = F(a[i * 2], a[i * 2 + 1]);
		}
	}
	
	// a[p] を v に変更し再計算
	void update(int p, T v) {
		assert(inID(p, 0, M));
		p += S;
		a[p] = v;
		while(p != 1) {
			p /= 2;
			a[p] = F(a[p * 2], a[p * 2 + 1]);
		}
	}
	
	// a[p] を v に変更(再計算無し)
	void change(int p, T v) {
		assert(inID(p, 0, M));
		p += S;
		a[p] = v;
	}
	
	T & operator[](int p) {
		assert(inID(p, 0, M));
		p += S;
		return a[p];
	}
	
	// [l, r) に f を適用した結果を返す
	T range(int l, int r) {
		assert(inID(l, 0, M));
		assert(inII(r, 0, M));
		assert(l <= r);
		l += S;
		r += S;
		
		T v = Z;
		while(l < r) {
			if(l + 1 == r) { v = F(v, a[l]); break; }
			
			if(l % 2 == 1) { v = F(v, a[l]); }
			if(r % 2 == 1) { v = F(v, a[r - 1]); }
			
			l = (l + 1) / 2;
			r = r / 2;
		}
		
		return v;
	}
	
	virtual ~BIT() {
		delete[] a;
	}
};


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; }
	
	BIT<int> bit(n, MIN, 1000000000);
	inc(i, n - 1) { bit[i] = lcp(i, i + 1); }
	bit.calc();
	
	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));
		
		if(rev[i] > rev[j]) { swap(i, j); }
		ans += bit.range(rev[i], rev[j]);
	}
	
	cout << ans << endl;
	
	return 0;
}
0