結果
問題 | No.2759 Take Pictures, Elements? |
ユーザー | rlangevin |
提出日時 | 2024-05-17 21:43:56 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 371 ms / 2,000 ms |
コード長 | 3,959 bytes |
コンパイル時間 | 286 ms |
コンパイル使用メモリ | 82,392 KB |
実行使用メモリ | 79,780 KB |
最終ジャッジ日時 | 2024-05-17 21:44:01 |
合計ジャッジ時間 | 3,859 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 239 ms
79,352 KB |
testcase_01 | AC | 371 ms
79,780 KB |
testcase_02 | AC | 38 ms
53,452 KB |
testcase_03 | AC | 39 ms
53,052 KB |
testcase_04 | AC | 39 ms
52,944 KB |
testcase_05 | AC | 39 ms
54,364 KB |
testcase_06 | AC | 38 ms
54,676 KB |
testcase_07 | AC | 39 ms
53,792 KB |
testcase_08 | AC | 38 ms
53,540 KB |
testcase_09 | AC | 106 ms
76,744 KB |
testcase_10 | AC | 111 ms
77,208 KB |
testcase_11 | AC | 107 ms
76,756 KB |
testcase_12 | AC | 103 ms
77,208 KB |
testcase_13 | AC | 104 ms
77,144 KB |
testcase_14 | AC | 128 ms
77,744 KB |
testcase_15 | AC | 124 ms
77,352 KB |
testcase_16 | AC | 125 ms
77,236 KB |
testcase_17 | AC | 124 ms
77,112 KB |
testcase_18 | AC | 127 ms
77,092 KB |
testcase_19 | AC | 126 ms
77,108 KB |
testcase_20 | AC | 128 ms
77,092 KB |
testcase_21 | AC | 38 ms
53,416 KB |
testcase_22 | AC | 39 ms
53,604 KB |
testcase_23 | AC | 38 ms
53,084 KB |
ソースコード
class SegmentTree: def __init__(self, n, identity_e, combine_f, ): self._n = n self._size = 1 while self._size < self._n: self._size <<= 1 self._identity_e = identity_e self._combine_f = combine_f self._node = [self._identity_e] * (2 * self._size) def build(self, array): assert len(array) == self._n for index, value in enumerate(array, start=self._size): self._node[index] = value for index in range(self._size - 1, 0, -1): self._node[index] = self._combine_f( self._node[index << 1 | 0], self._node[index << 1 | 1] ) def update(self, index, value): i = self._size + index self._node[i] = value while i > 1: i >>= 1 self._node[i] = self._combine_f( self._node[i << 1 | 0], self._node[i << 1 | 1] ) def fold(self, L, R): L += self._size R += self._size value_L = self._identity_e value_R = self._identity_e while L < R: if L & 1: value_L = self._combine_f(value_L, self._node[L]) L += 1 if R & 1: R -= 1 value_R = self._combine_f(self._node[R], value_R) L >>= 1 R >>= 1 return self._combine_f(value_L, value_R) def get(self, p): return self._node[p + self._size] def max_right(self, l, f): assert 0 <= l <= self._n assert f(self._identity_e) if l == self._n: return self._n l += self._size sm = self._identity_e while True: while l % 2 == 0: l >>= 1 if not f(self._combine_f(sm, self._node[l])): while l < self._size: l <<= 1 if f(self._combine_f(sm, self._node[l])): sm = self._combine_f(sm, self._node[l]) l += 1 return l - self._size sm = self._combine_f(sm, self._node[l]) l += 1 if l & -l == l: break return self._n def min_left(self, r, f): assert 0 <= r <= self._n assert f(self._identity_e) if r == 0: return 0 r += self._size sm = self._identity_e while True: r -= 1 while r > 1 and r % 2: r >>= 1 if not f(self._combine_f(self._node[r], sm)): while r < self._size: r = 2 * r + 1 if f(self._combine_f(self._node[r], sm)): sm = self._combine_f(self._node[r], sm) r -= 1 return r + 1 - self._size sm = self._combine_f(self._node[r], sm) if r & -r == r: break return 0 def __str__(self): temp = [] for i in range(self._n): temp.append(str(self.fold(i, i + 1))) return ' '.join(temp) N, Q = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) Tp = SegmentTree(N, 10 ** 18, min) Tm = SegmentTree(N, 10 ** 18, min) Tp.update(0, 0) Tm.update(0, 0) for b in B: nTp = SegmentTree(N, 10 ** 18, min) nTm = SegmentTree(N, 10 ** 18, min) for i, a in enumerate(A): if b == a: vm = Tm.fold(0, i + 1) + i vp = Tp.fold(i, N) - i ans = min(vp, vm) nTp.update(i, min(ans + i, nTp.fold(i, i + 1))) nTm.update(i, min(ans - i, nTm.fold(i, i + 1))) Tp, nTp = nTp, Tp Tm, nTm = nTm, Tm ans = 10 ** 18 for i in range(N): ans = min(ans, Tp.fold(i, i + 1) - i) ans = min(ans, Tm.fold(i, i + 1) + i) print(ans)