結果

問題 No.743 Segments on a Polygon
ユーザー H3PO4H3PO4
提出日時 2021-01-02 19:20:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,592 ms / 2,000 ms
コード長 821 bytes
コンパイル時間 281 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 33,976 KB
最終ジャッジ日時 2024-04-20 13:49:15
合計ジャッジ時間 12,334 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,521 ms
33,720 KB
testcase_01 AC 1,539 ms
33,592 KB
testcase_02 AC 1,497 ms
33,644 KB
testcase_03 AC 1,485 ms
33,644 KB
testcase_04 AC 1,563 ms
33,644 KB
testcase_05 AC 1,592 ms
33,724 KB
testcase_06 AC 1,523 ms
33,648 KB
testcase_07 AC 1,569 ms
33,976 KB
testcase_08 AC 1,540 ms
33,768 KB
testcase_09 AC 1,185 ms
33,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Bit:
    """1-indexed"""

    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def _sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def sum(self, i, j):
        """閉区間[i, j]"""
        return self._sum(j) - self._sum(i - 1)

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


N, M = map(int, input().split())
B = Bit(M)
queries = []
for i in range(N):
    a, b = sorted([int(x) + 1 for x in input().split()])
    queries.append((a, 1))
    queries.append((b, 2, a))
queries.sort()

ans = 0
for q in queries:
    if q[1] == 1:
        B.add(q[0], 1)
    elif q[1] == 2:
        B.add(q[2], -1)
        ans += B.sum(q[2], q[0])
print(ans)
0