結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー kept1994kept1994
提出日時 2022-04-29 13:56:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 261 ms / 2,000 ms
コード長 3,814 bytes
コンパイル時間 298 ms
コンパイル使用メモリ 86,984 KB
実行使用メモリ 105,872 KB
最終ジャッジ日時 2023-09-11 06:36:30
合計ジャッジ時間 8,269 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
78,340 KB
testcase_01 AC 76 ms
71,424 KB
testcase_02 AC 76 ms
71,388 KB
testcase_03 AC 232 ms
100,124 KB
testcase_04 AC 250 ms
105,860 KB
testcase_05 AC 231 ms
100,228 KB
testcase_06 AC 214 ms
97,328 KB
testcase_07 AC 255 ms
105,872 KB
testcase_08 AC 110 ms
78,288 KB
testcase_09 AC 163 ms
101,940 KB
testcase_10 AC 201 ms
105,796 KB
testcase_11 AC 77 ms
71,124 KB
testcase_12 AC 255 ms
105,724 KB
testcase_13 AC 259 ms
105,740 KB
testcase_14 AC 261 ms
105,756 KB
testcase_15 AC 77 ms
71,260 KB
testcase_16 AC 78 ms
71,476 KB
testcase_17 AC 78 ms
71,260 KB
testcase_18 AC 78 ms
71,300 KB
testcase_19 AC 79 ms
71,416 KB
testcase_20 AC 76 ms
71,124 KB
testcase_21 AC 78 ms
71,132 KB
testcase_22 AC 77 ms
71,564 KB
testcase_23 AC 115 ms
78,184 KB
testcase_24 AC 148 ms
88,464 KB
testcase_25 AC 216 ms
96,956 KB
testcase_26 AC 125 ms
80,932 KB
testcase_27 AC 164 ms
93,904 KB
testcase_28 AC 189 ms
102,228 KB
testcase_29 AC 219 ms
97,244 KB
testcase_30 AC 249 ms
105,568 KB
testcase_31 AC 137 ms
83,700 KB
testcase_32 AC 125 ms
80,200 KB
testcase_33 AC 193 ms
100,228 KB
testcase_34 AC 76 ms
71,412 KB
testcase_35 AC 77 ms
71,108 KB
testcase_36 AC 78 ms
71,268 KB
testcase_37 AC 77 ms
71,192 KB
testcase_38 AC 78 ms
71,460 KB
testcase_39 AC 77 ms
71,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys

MOD = 998244353

class SegTree:
    def __init__(self, monoid, N, func):
        self.monoid = monoid
        self.func = func
        self.bottomLen = N
        self.offset = self.bottomLen        # セグ木の最下層の最初のインデックスに合わせるためのオフセット
        self.segLen = self.bottomLen * 2
        self.tree = [monoid] * self.segLen

    """
    初期化
    O(self.segLen)
    """
    def build(self, seq):
        # 最下段の初期化
        for i, x in enumerate(seq, self.offset):
            self.tree[i] = x
        # ビルド
        for i in range(self.offset - 1, 0, -1):
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1])
            print(self.tree)

    """
    一点加算 他演算
    O(log(self.bottomLen))
    """
    def pointAdd(self, i: int, val: int):
        i += self.offset
        self.tree[i] += val
        # self.tree[i] = self.func(self.tree[i], val) <- こっちの方が都度の修正は発生しない。再帰が遅くないか次第。
        while i > 1:
            i >>= 1 # 2で割って頂点に達するまで下層から遡上
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1]) # 必ず末尾0と1がペアになるのでor演算子

    """
    一点更新
    O(log(self.bottomLen))
    """
    def pointUpdate(self, i: int, val: int):
        i += self.offset
        self.tree[i] = val
        while i > 1:
            i >>= 1 # 2で割って頂点に達するまで下層から遡上
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1]) # 必ず末尾0と1がペアになるのでor演算子
    
    """
    区間更新
    O(log(self.bottomLen))
    """
    def rangeAdd(self, l: int, r: int, val: int):
        l += self.offset
        r += self.offset
        while l < r:
            if l & 1:
                self.tree[l] = self.func(self.tree[l], val) 
                l += 1
            if r & 1:
                r -= 1
                self.tree[r] = self.func(self.tree[r], val) 
            l >>= 1
            r >>= 1
        return

    """ 一点取得
    O(log(self.bottomLen))
    """
    def pointUpdate(self, i: int, val: int):
        i += self.offset
        self.tree[i] = val
        while i > 1:
            i >>= 1 # 2で割って頂点に達するまで下層から遡上
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1]) # 必ず末尾0と1がペアになるのでor演算子

    """ 区間取得
    O(log(self.bottomLen))
    """
    def getRange(self, l: int, r: int):
        l += self.offset
        r += self.offset
        vL = self.monoid
        vR = self.monoid
        while l < r:
            if l & 1:
                vL = self.func(vL, self.tree[l])
                l += 1
            if r & 1:
                r -= 1
                vR = self.func(self.tree[r], vR)
            l >>= 1
            r >>= 1
        return self.func(vL, vR)

    """ 一点取得
    O(log(self.bottomLen))
    """
    def getPoint(self, i: int):
        i += self.offset
        res = self.tree[i]
        while i > 1:
            i >>= 1
            res = self.func(res, self.tree[i])
        return res


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    d = dict()
    for i, aa in enumerate(A):
        d[aa] = i
    # print(d)

    monoid = 0
    def add(x: int, y: int):
        return x + y
    seg = SegTree(monoid, N, add)
    
    ans = 0
    for i, bb in enumerate(B):
        seg.pointAdd(d[bb], 1)
        # print(seg.tree, i - seg.getRange(0, d[bb]))
        ans += i - seg.getRange(0, d[bb])
    print(ans)
    return

    

if __name__ == '__main__':
    main()
0