結果

問題 No.1788 Same Set
ユーザー 👑 tute7627tute7627
提出日時 2021-12-02 22:43:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,578 ms / 4,000 ms
コード長 2,827 bytes
コンパイル時間 612 ms
コンパイル使用メモリ 87,172 KB
実行使用メモリ 162,960 KB
最終ジャッジ日時 2023-10-11 23:23:48
合計ジャッジ時間 36,498 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 177 ms
86,436 KB
testcase_01 AC 177 ms
86,384 KB
testcase_02 AC 175 ms
86,492 KB
testcase_03 AC 175 ms
86,340 KB
testcase_04 AC 181 ms
86,400 KB
testcase_05 AC 175 ms
86,324 KB
testcase_06 AC 220 ms
88,644 KB
testcase_07 AC 208 ms
88,880 KB
testcase_08 AC 207 ms
88,256 KB
testcase_09 AC 180 ms
86,340 KB
testcase_10 AC 180 ms
86,208 KB
testcase_11 AC 276 ms
160,524 KB
testcase_12 AC 683 ms
161,364 KB
testcase_13 AC 822 ms
161,264 KB
testcase_14 AC 932 ms
161,448 KB
testcase_15 AC 1,176 ms
162,228 KB
testcase_16 AC 1,376 ms
162,960 KB
testcase_17 AC 973 ms
135,704 KB
testcase_18 AC 1,353 ms
153,880 KB
testcase_19 AC 579 ms
135,716 KB
testcase_20 AC 332 ms
135,804 KB
testcase_21 AC 1,166 ms
135,716 KB
testcase_22 AC 1,238 ms
135,712 KB
testcase_23 AC 979 ms
135,752 KB
testcase_24 AC 1,102 ms
135,732 KB
testcase_25 AC 1,453 ms
135,772 KB
testcase_26 AC 1,322 ms
135,932 KB
testcase_27 AC 301 ms
135,760 KB
testcase_28 AC 1,578 ms
135,732 KB
testcase_29 AC 1,265 ms
135,748 KB
testcase_30 AC 330 ms
135,716 KB
testcase_31 AC 1,392 ms
135,804 KB
testcase_32 AC 1,132 ms
135,944 KB
testcase_33 AC 1,299 ms
135,728 KB
testcase_34 AC 1,477 ms
135,936 KB
testcase_35 AC 1,467 ms
135,748 KB
testcase_36 AC 1,396 ms
135,956 KB
testcase_37 AC 1,427 ms
135,668 KB
testcase_38 AC 1,381 ms
135,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import types
import typing

def _ceil_pow2(n: int) -> int:
    x = 0
    while (1 << x) < n:
        x += 1

    return x

class LazySegTree:
    def __init__(
            self,
            v: typing.Union[int, typing.List[typing.Any]]) -> None:

        self._n = len(v)
        self._log = _ceil_pow2(self._n)
        self._size = 1 << self._log
        self._d = [0] * (2 * self._size)
        self._lz = [0] * (2 * self._size)
        for i in range(self._n):
            self._d[self._size + i] = v[i]
        for i in range(self._size - 1, 0, -1):
            self._update(i)


    def all_prod(self) -> typing.Any:
        return self._d[1]

    def apply(self, left: int, right: typing.Optional[int] = None,
              f: typing.Optional[typing.Any] = None) -> None:
        assert f is not None

        if right is None:
            p = left
            assert 0 <= left < self._n

            p += self._size
            self._d[p] += f
            for i in range(1, self._log + 1):
                self._update(p >> i)
        else:
            assert 0 <= left <= right <= self._n
            if left == right:
                return

            left += self._size
            right += self._size

            l2 = left
            r2 = right
            while left < right:
                if left & 1:
                    self._all_apply(left, f)
                    left += 1
                if right & 1:
                    right -= 1
                    self._all_apply(right, f)
                left >>= 1
                right >>= 1
            left = l2
            right = r2

            for i in range(1, self._log + 1):
                if ((left >> i) << i) != left:
                    self._update(left >> i)
                if ((right >> i) << i) != right:
                    self._update((right - 1) >> i)

    def _all_apply(self, k: int, f: typing.Any) -> None:
        self._lz[k] += f
        if k < self._size:
          self._update(k)
        else:
          self._d[k] = (1 if self._lz[k] == 0 else 0)
    def _update(self, k: int) -> None:
        self._d[k] = self._d[2 * k] + self._d[2 * k + 1]
        if self._lz[k] > 0:
          self._d[k] = 0



n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
init = [1] * n
init.append(0)
seg = LazySegTree(init)
sz = 400005;
pre_a = [-1] * sz
pre_b = [-1] * sz;
ans = 0;
for i in range(n):
  upd = [];
  upd.append(a[i]);
  if a[i] != b[i]:
    upd.append(b[i]);
  for val in upd:
    l = min(pre_a[val], pre_b[val]);
    r = max(pre_a[val], pre_b[val]);
    seg.apply(l + 1, r + 1, -1);
  pre_a[a[i]] = i;
  pre_b[b[i]] = i;
  for val in upd:
    l = min(pre_a[val], pre_b[val]);
    r = max(pre_a[val], pre_b[val]);
    seg.apply(l + 1, r + 1, 1);
  ans += seg.all_prod() - (n - i - 1);
print(ans)
0