結果

問題 No.1282 Display Elements
ユーザー tcltktcltk
提出日時 2021-02-02 00:56:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 393 ms / 2,000 ms
コード長 3,081 bytes
コンパイル時間 1,049 ms
コンパイル使用メモリ 86,712 KB
実行使用メモリ 117,320 KB
最終ジャッジ日時 2023-09-12 10:55:53
合計ジャッジ時間 9,063 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 205 ms
80,708 KB
testcase_01 AC 202 ms
80,472 KB
testcase_02 AC 202 ms
80,632 KB
testcase_03 AC 203 ms
80,580 KB
testcase_04 AC 204 ms
80,660 KB
testcase_05 AC 203 ms
80,596 KB
testcase_06 AC 203 ms
80,600 KB
testcase_07 AC 202 ms
80,560 KB
testcase_08 AC 204 ms
80,476 KB
testcase_09 AC 203 ms
80,600 KB
testcase_10 AC 219 ms
83,704 KB
testcase_11 AC 218 ms
83,748 KB
testcase_12 AC 203 ms
80,604 KB
testcase_13 AC 210 ms
83,356 KB
testcase_14 AC 216 ms
83,876 KB
testcase_15 AC 368 ms
116,836 KB
testcase_16 AC 296 ms
94,752 KB
testcase_17 AC 357 ms
109,648 KB
testcase_18 AC 328 ms
99,376 KB
testcase_19 AC 299 ms
117,240 KB
testcase_20 AC 301 ms
116,672 KB
testcase_21 AC 393 ms
117,320 KB
testcase_22 AC 374 ms
117,280 KB
testcase_23 AC 390 ms
117,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#region Header
#!/usr/bin/env python3
# from typing import *

import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq

def input():
    return sys.stdin.readline()[:-1]

sys.setrecursionlimit(1000000)
#endregion

# _INPUT = """3
# 2 2 2
# 1 5 3
# """
# sys.stdin = io.StringIO(_INPUT)


# Range Add Query に対応
class BIT_RAQ:
    """
    Binary Indexed Tree (Fenwick Tree), 1-indexed
    """

    def __init__(self, n):
        """
        Parameters
        ----------
        n : int
            要素数。index は 0..n になる。
        """
        self.size = n
        self.data = [[0]*(n+1) for _ in range(2)]
        self.depth = n.bit_length()

    def _add(self, p, i, x):
        assert p in [0, 1]
        assert 1 <= i <= self.size+1, 'i: out of range'
        while i <= self.size:
            self.data[p][i] += x
            i += i & -i

    def add(self, i, x):
        """
        index i に add
        """
        assert 1 <= i <= self.size+1, 'i: out of range'
        self._add(0, i, x)

    def radd(self, l, r, x):
        """
        [l, r) に add
        """
        assert 1 <= l <= self.size, 'l: out of range'
        assert l <= r <= self.size+1, 'r: out of range'
        self._add(0, l, -x * (l-1))
        self._add(0, min(r+1, self.size+1), x * r)
        self._add(1, l, x)
        self._add(1, min(r+1, self.size+1), -x)

    def _get_sum(self, p, i):
        assert p in [0, 1]
        assert 0 <= i <= self.size+1, 'i: out of range'
        s = 0
        while i > 0:
            s += self.data[p][i]
            i -= i & -i
        return s

    def get_sum(self, i):
        """
        [1,i) の sum
        """
        assert 0 <= i <= self.size+1, 'i: out of range'
        return self._get_sum(0, i) + self._get_sum(1, i) * i

    def get_rsum(self, l, r):
        """
        [l, r) の sum
        """
        assert 1 <= l <= self.size, 'l: out of range'
        assert l <= r <= self.size+1, 'r: out of range'
        return self.get_sum(r) - self.get_sum(l-1)

    def get_value(self, i):
        """
        index i の値
        """
        assert 1 <= i <= self.size+1, 'i: out of range'
        return self.get_sum(i) - self.get_sum(i-1)

    def lower_bound(self, x):
        """
        累積和が x 以上になる最小の index
        """
        sum_ = 0
        pos = 0
        for i in range(self.depth, -1, -1):
            k = pos + (1 << i)
            if k <= self.size and sum_ + self.data[k] < x:
                sum_ += self.data[k]
                pos += 1 << i
        return pos + 1


def main():
    N = int(input())
    A = sorted(list(map(int, input().split())))
    B = list(map(int, input().split()))

    bit = BIT_RAQ(N+1)
    for i in range(N):
        j = max(bisect.bisect_right(A, B[i]), i)
        bit.radd(j+1, N+1, 1)
    ans = bit.get_sum(N)
    print(ans)


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