結果

問題 No.1170 Never Want to Walk
ユーザー rei60rei60
提出日時 2020-08-29 13:01:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 844 ms / 2,000 ms
コード長 2,640 bytes
コンパイル時間 298 ms
コンパイル使用メモリ 82,040 KB
実行使用メモリ 105,552 KB
最終ジャッジ日時 2024-04-26 16:22:51
合計ジャッジ時間 13,644 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,736 KB
testcase_01 AC 40 ms
52,480 KB
testcase_02 AC 39 ms
52,992 KB
testcase_03 AC 40 ms
53,176 KB
testcase_04 AC 40 ms
52,736 KB
testcase_05 AC 39 ms
52,736 KB
testcase_06 AC 39 ms
52,864 KB
testcase_07 AC 39 ms
52,736 KB
testcase_08 AC 40 ms
52,736 KB
testcase_09 AC 40 ms
52,864 KB
testcase_10 AC 39 ms
52,480 KB
testcase_11 AC 39 ms
52,992 KB
testcase_12 AC 70 ms
72,320 KB
testcase_13 AC 82 ms
75,904 KB
testcase_14 AC 82 ms
75,776 KB
testcase_15 AC 69 ms
71,808 KB
testcase_16 AC 70 ms
72,960 KB
testcase_17 AC 81 ms
75,520 KB
testcase_18 AC 68 ms
71,424 KB
testcase_19 AC 72 ms
73,344 KB
testcase_20 AC 83 ms
75,520 KB
testcase_21 AC 70 ms
72,320 KB
testcase_22 AC 82 ms
75,624 KB
testcase_23 AC 82 ms
75,904 KB
testcase_24 AC 83 ms
75,708 KB
testcase_25 AC 81 ms
75,564 KB
testcase_26 AC 82 ms
75,520 KB
testcase_27 AC 836 ms
104,324 KB
testcase_28 AC 813 ms
104,156 KB
testcase_29 AC 759 ms
104,600 KB
testcase_30 AC 731 ms
104,656 KB
testcase_31 AC 844 ms
104,460 KB
testcase_32 AC 576 ms
104,696 KB
testcase_33 AC 725 ms
103,948 KB
testcase_34 AC 661 ms
104,924 KB
testcase_35 AC 683 ms
105,552 KB
testcase_36 AC 731 ms
104,628 KB
testcase_37 AC 632 ms
104,136 KB
testcase_38 AC 636 ms
105,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
"""
Created on Sat Aug 22 11:41:03 2020

"""

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:
            stack = []
            prt = self.parents
            while prt[x] >= 0:
                stack.append(x)
                x = prt[x]
            for y in stack:
                prt[y] = x
            return 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)

    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