結果

問題 No.1170 Never Want to Walk
ユーザー rei60rei60
提出日時 2020-08-22 15:52:16
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 717 ms / 2,000 ms
コード長 2,488 bytes
コンパイル時間 452 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 108,368 KB
最終ジャッジ日時 2023-08-05 12:55:21
合計ジャッジ時間 13,317 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,624 KB
testcase_01 AC 72 ms
71,184 KB
testcase_02 AC 73 ms
71,264 KB
testcase_03 AC 73 ms
71,180 KB
testcase_04 AC 71 ms
71,128 KB
testcase_05 AC 71 ms
71,268 KB
testcase_06 AC 73 ms
71,064 KB
testcase_07 AC 72 ms
71,084 KB
testcase_08 AC 72 ms
71,460 KB
testcase_09 AC 71 ms
71,204 KB
testcase_10 AC 72 ms
71,264 KB
testcase_11 AC 73 ms
71,068 KB
testcase_12 AC 97 ms
75,800 KB
testcase_13 AC 103 ms
76,940 KB
testcase_14 AC 105 ms
76,668 KB
testcase_15 AC 99 ms
76,164 KB
testcase_16 AC 97 ms
76,636 KB
testcase_17 AC 107 ms
76,564 KB
testcase_18 AC 96 ms
75,960 KB
testcase_19 AC 99 ms
76,472 KB
testcase_20 AC 104 ms
76,712 KB
testcase_21 AC 97 ms
76,568 KB
testcase_22 AC 103 ms
76,548 KB
testcase_23 AC 104 ms
76,936 KB
testcase_24 AC 100 ms
76,556 KB
testcase_25 AC 104 ms
76,640 KB
testcase_26 AC 105 ms
76,336 KB
testcase_27 AC 594 ms
105,584 KB
testcase_28 AC 548 ms
105,448 KB
testcase_29 AC 717 ms
107,984 KB
testcase_30 AC 590 ms
105,756 KB
testcase_31 AC 614 ms
105,812 KB
testcase_32 AC 404 ms
105,440 KB
testcase_33 AC 455 ms
105,352 KB
testcase_34 AC 450 ms
108,028 KB
testcase_35 AC 463 ms
108,368 KB
testcase_36 AC 456 ms
105,668 KB
testcase_37 AC 431 ms
105,428 KB
testcase_38 AC 398 ms
108,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from bisect import bisect_left, bisect_right

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.left = [0] * n
        self.right = [0] * n
        for i in range(n):
            self.left[i] = i
            self.right[i] = i
            
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x
        
        self.left[x] = min(self.left[x], self.left[y])
        self.right[x] = max(self.right[x], self.right[y])

        self.parents[x] += self.parents[y]
        self.parents[y] = x
    
    def findleft(self, x):
        x = self.find(x)
        return self.left[x]

    def findright(self, x):
        x = self.find(x)
        return self.right[x]    
    
    def pair(self, x):
        x = self.find(x)
        return (self.left[x], self.right[x])

    def union_range(self, a, b):
        if a >= b:
            return
        else:
            while self.findright(a) < b:
                self.union(self.findright(a), self.findright(a) + 1)


    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

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

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


N, A, B = map(int,input().split())
X = list(map(int, input().split()))

uf = UnionFind(N)

for idx, i in enumerate(X):
    L_left = bisect_left(X, i-B)
    L_right = bisect_right(X, i-A)
    R_left = bisect_left(X, i+A)
    R_right = bisect_right(X, i+B)
    #print(L_left, L_right, R_left, R_right)
    if L_right - L_left > 0:
        uf.union(idx, L_left)
        uf.union_range(L_left, L_right-1)
    if R_right - R_left > 0:
        uf.union(idx, R_left)
        uf.union_range(R_left, R_right-1)
    
for i in range(N):
    print(uf.size(i))

0