結果

問題 No.1282 Display Elements
ユーザー paruf4paruf4
提出日時 2020-11-07 00:12:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 363 ms / 2,000 ms
コード長 2,271 bytes
コンパイル時間 606 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 173,848 KB
最終ジャッジ日時 2023-09-29 19:48:54
合計ジャッジ時間 5,151 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,284 KB
testcase_01 AC 73 ms
71,388 KB
testcase_02 AC 72 ms
71,284 KB
testcase_03 AC 73 ms
70,984 KB
testcase_04 AC 72 ms
71,464 KB
testcase_05 AC 73 ms
71,248 KB
testcase_06 AC 73 ms
71,252 KB
testcase_07 AC 73 ms
71,108 KB
testcase_08 AC 71 ms
71,164 KB
testcase_09 AC 72 ms
71,348 KB
testcase_10 AC 109 ms
76,532 KB
testcase_11 AC 86 ms
76,268 KB
testcase_12 AC 76 ms
74,504 KB
testcase_13 AC 86 ms
75,964 KB
testcase_14 AC 92 ms
76,664 KB
testcase_15 AC 349 ms
165,956 KB
testcase_16 AC 197 ms
106,512 KB
testcase_17 AC 295 ms
148,196 KB
testcase_18 AC 220 ms
106,508 KB
testcase_19 AC 174 ms
103,512 KB
testcase_20 AC 186 ms
103,260 KB
testcase_21 AC 362 ms
173,676 KB
testcase_22 AC 226 ms
103,776 KB
testcase_23 AC 363 ms
173,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
a = list(map(int, input().split()))
a.sort()
b = list(map(int, input().split()))

c = list(set(a+b))
c.sort()
dic = {}

for i in range(len(c)):
    dic[c[i]] = i

for i in range(n):
    a[i] = dic[a[i]]
    b[i] = dic[b[i]]

#####segfunc#####
def segfunc(x, y):
    return x+y
#################


#####ide_ele#####
ide_ele = 0
#################


class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """

    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res


dp = [0]*(2*n+10)
seg = SegTree(dp, segfunc, 0)
res = 0
for i in range(n):
    seg.update(b[i], seg.query(b[i], b[i]+1)+1)
    res += seg.query(0, a[i])

print(res)
0