結果

問題 No.515 典型LCP
ユーザー 37zigen37zigen
提出日時 2017-05-06 01:42:56
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 4,122 bytes
コンパイル時間 3,842 ms
コンパイル使用メモリ 78,856 KB
実行使用メモリ 64,460 KB
最終ジャッジ日時 2024-09-14 10:12:59
合計ジャッジ時間 16,864 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 AC 899 ms
42,292 KB
testcase_03 AC 56 ms
36,364 KB
testcase_04 AC 55 ms
36,460 KB
testcase_05 AC 419 ms
41,704 KB
testcase_06 AC 580 ms
41,776 KB
testcase_07 AC 477 ms
41,760 KB
testcase_08 AC 607 ms
41,772 KB
testcase_09 AC 663 ms
41,828 KB
testcase_10 AC 865 ms
42,404 KB
testcase_11 AC 856 ms
42,188 KB
testcase_12 AC 869 ms
41,996 KB
testcase_13 AC 568 ms
43,872 KB
testcase_14 AC 205 ms
43,984 KB
testcase_15 AC 308 ms
43,496 KB
testcase_16 AC 308 ms
43,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.NoSuchElementException;

class Main {
	int N, M;
	int[] X, Y;

	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner();
		long N = sc.nextLong();
		class S implements Comparable<S> {
			int idx;
			String s;

			public S(int idx, String s) {
				this.idx = idx;
				this.s = s;
			}

			public int compareTo(S o) {
				return s.compareTo(o.s);
			};
		}
		S[] s = new S[(int) N];

		Seg seg = new Seg((int) N);// seg[i]=s[i],s[i+1]のlcpの長さ
		Integer[] ord = new Integer[(int) N];
		for (int i = 0; i < N; ++i) {
			s[i] = new S(i, sc.next());
		}
		Arrays.sort(s);
		int[] rev = new int[(int) N];
		for (int i = 0; i < N; ++i) {
			rev[s[i].idx] = i;
		}
		for (int i = 0; i + 1 < N; ++i) {
			int len = 0;
			while (len < Math.min(s[i].s.length(), s[i + 1].s.length())
					&& s[i].s.charAt(len) == s[i + 1].s.charAt(len)) {
				++len;
			}
			seg.setVal(i, len);
		}
		seg.init();
		long M = sc.nextLong();
		long x = sc.nextLong();
		long d = sc.nextLong();
		long ans = 0;
		for (int k = 0; k < M; ++k) {
			int i = (int) (x / (N - 1)) + 1;
			int j = (int) (x % (N - 1)) + 1;
			if (i > j) {
				int tmp = i;
				i = j;
				j = tmp;
			} else {
				j = j + 1;
			}
			x = (x + d) % (N * (N - 1));
			--i;
			--j;
			if (rev[i] > rev[j]) {
				int tmp = i;
				i = j;
				j = tmp;
			}
			ans += seg.query(rev[i], rev[j]);
		}
		System.out.println(ans);

	}

	class Seg {
		int n;
		int[] v;

		public Seg(int n_) {
			n = 1;
			while (n < n_) {
				n *= 2;
			}
			v = new int[2 * n - 1];
			Arrays.fill(v, Integer.MAX_VALUE / 16);
		}

		void init() {
			for (int i = n - 2; i >= 0; --i) {
				v[i] = Math.min(v[2 * i + 1], v[2 * i + 2]);
			}
		}

		void setVal(int k, int val) {
			k += n - 1;
			v[k] = val;
			while (k > 0) {
				k = (k - 1) / 2;
				v[k] = Math.min(v[2 * k + 1], v[2 * k + 2]);
			}
		}

		int query(int a, int b) {
			return query(a, b, 0, n, 0);
		}

		int query(int a, int b, int l, int r, int k) {
			if (r <= a || b <= l) {
				return Integer.MAX_VALUE / 16;
			} else if (a <= l && r <= b) {
				return v[k];
			} else {
				int vl = query(a, b, l, (l + r) / 2, 2 * k + 1);
				int vr = query(a, b, (l + r) / 2, r, 2 * k + 2);
				return Math.min(vl, vr);
			}
		}
	}

	class Scanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0