結果

問題 No.2873 Kendall's Tau
ユーザー 寝癖
提出日時 2024-08-24 13:53:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,232 ms / 4,500 ms
コード長 1,490 bytes
コンパイル時間 401 ms
コンパイル使用メモリ 82,516 KB
実行使用メモリ 165,012 KB
最終ジャッジ日時 2024-08-24 13:53:58
合計ジャッジ時間 22,556 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

from bisect import bisect_left

import typing

class FenwickTree:
    '''Reference: https://en.wikipedia.org/wiki/Fenwick_tree'''

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.data = [0] * n

    def add(self, p: int, x: typing.Any) -> None:
        assert 0 <= p < self._n

        p += 1
        while p <= self._n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, left: int, right: int) -> typing.Any:
        assert 0 <= left <= right <= self._n

        return self._sum(right) - self._sum(left)

    def _sum(self, r: int) -> typing.Any:
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r

        return s


N = int(input())
pos = [list(map(int, input().split())) for _ in range(N)]

# 座標圧縮
xs, ys = sorted(set([x for x, y in pos])), sorted(set([y for x, y in pos]))
for i in range(N):
    x, y = pos[i]
    pos[i] = (bisect_left(xs, x), bisect_left(ys, y))

# x座標の昇順
pos.sort()

# x座標ごとにy座標を管理
y_pos = [[] for _ in range(len(xs))]
for x, y in pos:
    y_pos[x].append(y)

# fenwick treeでP,Q,R,Sを求める
ft = FenwickTree(len(ys))
P, Q, R, S = 0, 0, N*(N-1)//2, N*(N-1)//2
for yp in y_pos:
    for y in yp:
        P += ft.sum(0, y)
        Q += ft.sum(y+1, len(ys))
    for y in yp:
        ft.add(y, 1)
    R -= len(yp)*(len(yp)-1)//2
for i in range(len(ys)):
    cnt = ft.sum(i, i+1)
    S -= cnt*(cnt-1)//2

print((P-Q)*(R*S)**(-0.5))
0