結果

問題 No.1170 Never Want to Walk
ユーザー 👑 もぐら 3.0もぐら 3.0
提出日時 2023-11-21 23:31:32
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,185 bytes
コンパイル時間 257 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 221,472 KB
最終ジャッジ日時 2023-11-21 23:31:46
合計ジャッジ時間 14,331 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
62,420 KB
testcase_01 AC 48 ms
55,616 KB
testcase_02 AC 43 ms
55,616 KB
testcase_03 AC 42 ms
55,616 KB
testcase_04 AC 42 ms
55,616 KB
testcase_05 AC 43 ms
55,616 KB
testcase_06 AC 42 ms
55,616 KB
testcase_07 AC 42 ms
55,616 KB
testcase_08 AC 42 ms
55,616 KB
testcase_09 AC 42 ms
55,616 KB
testcase_10 AC 44 ms
55,616 KB
testcase_11 AC 42 ms
55,616 KB
testcase_12 AC 73 ms
72,564 KB
testcase_13 AC 98 ms
76,572 KB
testcase_14 AC 97 ms
76,312 KB
testcase_15 AC 74 ms
72,568 KB
testcase_16 AC 118 ms
76,436 KB
testcase_17 AC 105 ms
76,884 KB
testcase_18 AC 75 ms
72,564 KB
testcase_19 AC 86 ms
76,432 KB
testcase_20 AC 105 ms
76,608 KB
testcase_21 AC 74 ms
72,576 KB
testcase_22 AC 182 ms
76,760 KB
testcase_23 AC 125 ms
76,884 KB
testcase_24 AC 196 ms
76,616 KB
testcase_25 AC 210 ms
76,892 KB
testcase_26 AC 155 ms
76,748 KB
testcase_27 AC 1,038 ms
221,472 KB
testcase_28 AC 947 ms
219,172 KB
testcase_29 AC 983 ms
221,304 KB
testcase_30 AC 994 ms
218,564 KB
testcase_31 AC 980 ms
218,716 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections
class UnionFind():
    def __init__(self):
        self.parents = dict()                                      
        self.members_set = collections.defaultdict(lambda : set()) 
        self.roots_set = set()                                     
        self.key_ID = dict()                                      
        self.ID_key = dict()                                      
        self.cnt = 0                                              

    def dictf(self,x):
        if x in self.key_ID:
            return self.key_ID[x]
        else:
            self.cnt += 1
            self.key_ID[x] = self.cnt
            self.parents[x] = self.cnt
            self.ID_key[self.cnt] = x
            self.members_set[x].add(x)
            self.roots_set.add(x)
            return self.key_ID[x]

    def find(self, x):
        ID_x = self.dictf(x)
        if self.parents[x] == ID_x:
            return x
        else:
            self.parents[x] = self.key_ID[self.find(self.ID_key[self.parents[x]])]
            return self.ID_key[self.parents[x]]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        if x == y:
            return
        for i in self.members_set[y]:
            self.members_set[x].add(i)
        self.members_set[y] = set()
        self.roots_set.remove(y)
        self.parents[y] = self.key_ID[x]

    def size(self, x):
        return len(self.members_set[self.find(x)])

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        return self.members_set[self.find(x)]

    def roots(self):
        return self.roots_set

    def group_count(self):
        return len(self.roots_set)

    def all_group_members(self):
        return {r: self.members_set[r] for r in self.roots_set}

import bisect
N,A,B = map(int,input().split())
x = list(map(int,input().split()))
uf_s = UnionFind()

for i in range(N-1):
    left=bisect.bisect_left(x,x[i]+A)
    right=bisect.bisect_right(x,x[i]+B)
    for j in range(left,right):
        uf_s.union(x[i], x[j])

for n in x:
    print(len(uf_s.members(n)))
0