結果

問題 No.743 Segments on a Polygon
ユーザー brthyyjpbrthyyjp
提出日時 2022-03-08 15:25:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 1,714 bytes
コンパイル時間 377 ms
コンパイル使用メモリ 86,968 KB
実行使用メモリ 116,772 KB
最終ジャッジ日時 2023-09-30 21:32:11
合計ジャッジ時間 5,412 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 300 ms
115,736 KB
testcase_01 AC 292 ms
116,108 KB
testcase_02 AC 300 ms
115,908 KB
testcase_03 AC 294 ms
115,512 KB
testcase_04 AC 294 ms
115,944 KB
testcase_05 AC 294 ms
115,684 KB
testcase_06 AC 293 ms
116,064 KB
testcase_07 AC 293 ms
115,832 KB
testcase_08 AC 292 ms
115,896 KB
testcase_09 AC 225 ms
116,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0]*(self.n+1) # 1-indexed

    def init(self, init_val):
        for i, v in enumerate(init_val):
            self.add(i, v)

    def add(self, i, x):
        # i: 0-indexed
        i += 1 # to 1-indexed
        while i <= self.n:
            self.bit[i] += x
            i += (i & -i)

    def sum(self, i, j):
        # return sum of [i, j)
        # i, j: 0-indexed
        return self._sum(j) - self._sum(i)

    def _sum(self, i):
        # return sum of [0, i)
        # i: 0-indexed
        res = 0
        while i > 0:
            res += self.bit[i]
            i -= i & (-i)
        return res

    def lower_bound(self, x):
        s = 0
        pos = 0
        depth = self.n.bit_length()
        v = 1 << depth
        for i in range(depth, -1, -1):
            k = pos + v
            if k <= self.n and s + self.bit[k] < x:
                    s += self.bit[k]
                    pos += v
            v >>= 1
        return pos

    def __str__(self): # for debug
        arr = [self.sum(i,i+1) for i in range(self.n)]
        return str(arr)

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

from itertools import accumulate

n, m = map(int, input().split())
Q = [[] for i in range(m)]
CR = [0]*(m+1)
bit = BIT(m+1)
L = []
for i in range(n):
    l, r = map(int, input().split())
    if l > r:
        l, r = r, l
    Q[r].append(l)
    bit.add(l, 1)
    L.append(l)
    CR[r+1] += 1

ans = n*(n-1)//2
CR = list(accumulate(CR))
for l in L:
    ans -= CR[l]
Q = [sorted(x) for x in Q]
for r in range(m):
    for l in Q[r]:
        ans -= bit.sum(0, l)
        bit.add(l, -1)
print(ans)
0