結果

問題 No.2873 Kendall's Tau
ユーザー vwxyzvwxyz
提出日時 2024-09-06 21:51:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,120 ms / 4,500 ms
コード長 6,548 bytes
コンパイル時間 413 ms
コンパイル使用メモリ 82,416 KB
実行使用メモリ 253,668 KB
最終ジャッジ日時 2024-09-06 21:52:39
合計ジャッジ時間 33,499 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,620 KB
testcase_01 AC 41 ms
55,620 KB
testcase_02 AC 41 ms
54,556 KB
testcase_03 AC 41 ms
55,308 KB
testcase_04 AC 43 ms
54,812 KB
testcase_05 AC 41 ms
54,348 KB
testcase_06 AC 41 ms
54,472 KB
testcase_07 AC 2,025 ms
197,132 KB
testcase_08 AC 2,103 ms
239,876 KB
testcase_09 AC 2,055 ms
196,368 KB
testcase_10 AC 2,120 ms
253,668 KB
testcase_11 AC 1,998 ms
195,604 KB
testcase_12 AC 2,030 ms
200,576 KB
testcase_13 AC 637 ms
103,392 KB
testcase_14 AC 1,618 ms
160,208 KB
testcase_15 AC 398 ms
97,144 KB
testcase_16 AC 404 ms
96,688 KB
testcase_17 AC 1,471 ms
155,920 KB
testcase_18 AC 1,103 ms
159,764 KB
testcase_19 AC 1,396 ms
168,460 KB
testcase_20 AC 404 ms
98,760 KB
testcase_21 AC 1,202 ms
135,444 KB
testcase_22 AC 544 ms
107,224 KB
testcase_23 AC 1,202 ms
135,320 KB
testcase_24 AC 246 ms
83,456 KB
testcase_25 AC 370 ms
89,980 KB
testcase_26 AC 1,467 ms
151,088 KB
testcase_27 AC 898 ms
119,976 KB
testcase_28 AC 1,688 ms
200,076 KB
testcase_29 AC 1,812 ms
211,036 KB
testcase_30 AC 317 ms
87,796 KB
testcase_31 AC 516 ms
101,900 KB
testcase_32 AC 1,177 ms
136,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Segment_Tree:
    def __init__(self,N,f,e,lst=None,dynamic=False,bisect_search=True):
        self.f=f
        self.e=e
        self.N=N
        self.bisect_search=bisect_search
        if self.bisect_search:
            self.le=1
            while self.le<self.N:
                self.le*=2
        else:
            self.le=self.N
        if dynamic:
            self.segment_tree=defaultdict(lambda:self.e)
        else:
            if lst==None:
                self.segment_tree=[self.e]*2*self.le
            else:
                assert len(lst)<=self.N
                self.segment_tree=[self.e]*self.le+[x for x in lst]+[self.e]*(self.le-len(lst))
                for i in range(self.le-1,0,-1):
                    self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def __getitem__(self,i):
        if type(i)==int:
            if -self.le<=i<0:
                return self.segment_tree[i+self.le*2]
            elif 0<=i<self.le:
                return self.segment_tree[i+self.le]
            else:
                raise IndexError("list index out of range")
        else:
            a,b,c=i.start,i.stop,i.step
            if a==None:
                a=self.le
            else:
                a+=self.le
            if b==None:
                b=self.le*2
            else:
                b+=self.le
            return self.segment_tree[slice(a,b,c)]

    def __setitem__(self,i,x):
        if -self.le<=i<0:
            i+=self.le*2
        elif 0<=i<self.le:
            i+=self.le
        else:
            raise IndexError("list index out of range")
        self.segment_tree[i]=x
        while i>1:
            i>>= 1
            self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def Build(self,lst):
        for i,x in enumerate(lst,self.le):
            self.segment_tree[i]=x
        for i in range(self.le-1,0,-1):
            self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def Fold(self,L=None,R=None):
        if L==None:
            L=self.le
        else:
            assert 0<=L<=self.N
            L+=self.le
        if R==None:
            R=self.le*2
        else:
            assert 0<=R<=self.N
            R+=self.le
        vL=self.e
        vR=self.e
        while L<R:
            if L&1:
                vL=self.f(vL,self.segment_tree[L])
                L+=1
            if R&1:
                R-=1
                vR=self.f(self.segment_tree[R],vR)
            L>>=1
            R>>=1
        return self.f(vL,vR)

    def Fold_Index(self,L=None,R=None):
        if L==None:
            L=self.le
        else:
            assert 0<=L<=self.N
            L+=self.le
        if R==None:
            R=self.le*2
        else:
            assert 0<=R<=self.N
            R+=self.le
        if L==R:
            return None
        x=self.Fold(L-self.le,R-self.le)
        while L<R:
            if L&1:
                if self.segment_tree[L]==x:
                    i=L
                    break
                L+=1
            if R&1:
                R-=1
                if self.segment_tree[R]==x:
                    i=R
                    break
            L>>=1
            R>>=1
        while i<self.le:
            if self.segment_tree[i]==self.segment_tree[i<<1]:
                i<<=1
            else:
                i<<=1
                i|=1
        i-=self.le
        return i

    def Bisect_Right(self,L=None,f=None):
        assert self.bisect_search
        if L==self.le:
            return self.le
        if L==None:
            L=0
        assert 0<=L<=self.N
        L+=self.le
        vl=self.e
        vr=self.e
        l,r=L,self.le*2
        while l<r:
            if l&1:
                vl=self.f(vl,self.segment_tree[l])
                l+=1
            if r&1:
                r-=1
                vr=self.f(self.segment_tree[r],vr)
            l>>=1
            r>>=1
        if f(self.f(vl,vr)):
            return self.N
        v=self.e
        while True:
            while L%2==0:
                L>>=1
            vv=self.f(v,self.segment_tree[L])
            if f(vv):
                v=vv
                L+=1
            else:
                while L<self.le:
                    L<<=1
                    vv=self.f(v,self.segment_tree[L])
                    if f(vv):
                        v=vv
                        L+=1
                return L-self.le

    def Bisect_Left(self,R=None,f=None):
        assert self.bisect_search
        if R==0:
            return 0
        if R==None:
            R=self.le
        assert 0<=R<=self.N
        R+=self.le
        vl=self.e
        vr=self.e
        l,r=self.le,R
        while l<r:
            if l&1:
                vl=self.f(vl,self.segment_tree[l])
                l+=1
            if r&1:
                r-=1
                vr=self.f(self.segment_tree[r],vr)
            l>>=1
            r>>=1
        if f(self.f(vl,vr)):
            return 0
        v=self.e
        while True:
            R-=1
            while R>1 and R%2:
                R>>=1
            vv=self.f(self.segment_tree[R],v)
            if f(vv):
                v=vv
            else:
                while R<self.le:
                    R=2*R+1
                    vv=self.f(self.segment_tree[R],v)
                    if f(vv):
                        v=vv
                        R-=1
                return R+1-self.le

    def __str__(self):
        return "["+", ".join(map(str,[self.segment_tree[i] for i in range(self.le,self.le+self.N)]))+"]"

def Compress(lst):
    decomp=sorted(list(set(lst)))
    comp={x:i for i,x in enumerate(decomp)}
    return comp,decomp

N=int(input())
X,Y=[],[]
for i in range(N):
    x,y=map(int,input().split())
    X.append(x)
    Y.append(y)
comp,decomp=Compress(X)
for i in range(N):
    X[i]=comp[X[i]]
comp,decomp=Compress(Y)
for i in range(N):
    Y[i]=comp[Y[i]]
def make_R(X):
    X=X[:]
    X.sort()
    bound=[0]
    for i in range(1,N):
        if X[i-1]!=X[i]:
            bound.append(i)
    bound.append(N)
    retu=N*(N-1)//2
    for l,r in zip(bound,bound[1:]):
        c=r-l
        retu-=c*(c-1)//2
    return retu
R=make_R(X)
S=make_R(Y)
def make_P(X,Y):
    idx=[i for i in range(N)]
    idx.sort(key=lambda i:(X[i],Y[i]))
    ST=Segment_Tree(N,lambda x,y:x+y,0)
    retu=0
    for i in idx:
        x,y=X[i],Y[i]
        retu+=ST.Fold(y+1,N)
        ST[y]+=1
    return retu
P=make_P(X,Y)
Q=make_P([N-1-x for x in X],Y)
ans=(Q-P)/((R*S)**.5)
print(ans)
0