結果

問題 No.743 Segments on a Polygon
ユーザー maspymaspy
提出日時 2020-03-20 03:07:49
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 727 ms / 2,000 ms
コード長 1,582 bytes
コンパイル時間 102 ms
コンパイル使用メモリ 10,884 KB
実行使用メモリ 32,540 KB
最終ジャッジ日時 2023-08-20 21:23:50
合計ジャッジ時間 8,539 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 628 ms
32,540 KB
testcase_01 AC 632 ms
32,444 KB
testcase_02 AC 639 ms
32,368 KB
testcase_03 AC 638 ms
32,444 KB
testcase_04 AC 636 ms
32,524 KB
testcase_05 AC 641 ms
32,452 KB
testcase_06 AC 644 ms
32,496 KB
testcase_07 AC 727 ms
32,460 KB
testcase_08 AC 641 ms
32,448 KB
testcase_09 AC 491 ms
32,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


class BinaryIndexedTree():
    def __init__(self, seq):
        self.size = len(seq)
        self.depth = self.size.bit_length()
        self.build(seq)

    def build(self, seq):
        data = seq
        size = self.size
        for i, x in enumerate(data):
            j = i + (i & (-i))
            if j < size:
                data[j] += data[i]
        self.data = data

    def __repr__(self):
        return self.data.__repr__()

    def get_sum(self, i):
        data = self.data
        s = 0
        while i:
            s += data[i]
            i -= i & -i
        return s

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

    def find_kth_element(self, k):
        data = self.data; size = self.size
        x, sx = 0, 0
        dx = 1 << (self.depth)
        for i in range(self.depth - 1, -1, -1):
            dx = (1 << i)
            if x + dx >= size:
                continue
            y = x + dx
            sy = sx + data[y]
            if sy < k:
                x, sx = y, sy
        return x + 1


N, M = map(int, readline().split())
m = map(int, read().split())
LR = []
for L, R in zip(m, m):
    if L > R:
        L, R = R, L
    LR.append((L + 1, R + 1))
LR.sort()

bit = BinaryIndexedTree([0] * (M + 10))
answer = 0
for L, R in LR:
    answer += bit.get_sum(R) - bit.get_sum(L)
    bit.add(R, 1)
print(answer)
0