結果

問題 No.1170 Never Want to Walk
ユーザー NoneNone
提出日時 2021-02-25 11:53:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 653 ms / 2,000 ms
コード長 3,346 bytes
コンパイル時間 445 ms
コンパイル使用メモリ 81,756 KB
実行使用メモリ 107,460 KB
最終ジャッジ日時 2023-10-26 02:26:43
合計ジャッジ時間 11,231 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
53,576 KB
testcase_01 AC 40 ms
53,576 KB
testcase_02 AC 40 ms
53,576 KB
testcase_03 AC 40 ms
53,576 KB
testcase_04 AC 39 ms
53,576 KB
testcase_05 AC 40 ms
53,576 KB
testcase_06 AC 40 ms
53,576 KB
testcase_07 AC 40 ms
53,576 KB
testcase_08 AC 41 ms
53,576 KB
testcase_09 AC 41 ms
53,576 KB
testcase_10 AC 40 ms
53,576 KB
testcase_11 AC 40 ms
53,576 KB
testcase_12 AC 63 ms
68,100 KB
testcase_13 AC 74 ms
73,408 KB
testcase_14 AC 80 ms
75,388 KB
testcase_15 AC 66 ms
70,160 KB
testcase_16 AC 68 ms
70,168 KB
testcase_17 AC 79 ms
73,908 KB
testcase_18 AC 68 ms
68,104 KB
testcase_19 AC 76 ms
72,300 KB
testcase_20 AC 84 ms
75,368 KB
testcase_21 AC 69 ms
70,156 KB
testcase_22 AC 81 ms
75,468 KB
testcase_23 AC 82 ms
75,452 KB
testcase_24 AC 79 ms
75,484 KB
testcase_25 AC 77 ms
75,472 KB
testcase_26 AC 82 ms
75,460 KB
testcase_27 AC 634 ms
104,356 KB
testcase_28 AC 623 ms
105,368 KB
testcase_29 AC 653 ms
107,460 KB
testcase_30 AC 634 ms
104,952 KB
testcase_31 AC 623 ms
105,396 KB
testcase_32 AC 452 ms
104,720 KB
testcase_33 AC 540 ms
103,764 KB
testcase_34 AC 462 ms
106,800 KB
testcase_35 AC 493 ms
105,376 KB
testcase_36 AC 510 ms
104,260 KB
testcase_37 AC 411 ms
103,836 KB
testcase_38 AC 407 ms
105,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self._left = [i for i in range(n)]
        self._right = [i for i in range(n)]

    def find(self, x):
        """ 根を見つける関数を定義(同時にxを直接根にくっつける操作も行う)"""
        tmp = []
        parents = self.parents
        while parents[x] >= 0:
            tmp.append(x)
            x = parents[x]
        for y in tmp:
            parents[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.parents[x] += self.parents[y]
        self.parents[y] = x
        self._left[x] = min(self._left[x],self._left[y])
        self._right[x] = max(self._right[x],self._right[y])

    def union_range(self, l, r):
        """ 閉区間[l,r]の要素をくっつける """
        if l>r: return
        while self.right(l)<r:
            self.union(self.right(l),self.right(l)+1)

    def left(self,x):
        x = self.find(x)
        return self._left[x]

    def right(self,x):
        x = self.find(x)
        return self._right[x]

    def same(self, x, y):
        """ xとyが同じ根の子かを判定 """
        return self.find(x) == self.find(y)

    def size(self, x):
        """ xの根のparent(= 要素数)を返す """
        return -self.parents[self.find(x)]

    def members(self, x):
        """ xが属するグループの要素をリストとして返す O(N)"""
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        """ 全ての根の要素をリストとして返す O(N)"""
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        """ グループの数を返す O(N)"""
        return len(self.roots())

    def size_list(self):
        """ 各グループの要素数のリストを返す(根の番号は返さない) O(N)"""
        return [-x for x in self.parents if x < 0]

    def all_group_members(self):
        """ {根:[根の子(根を含む)のリスト],...}を辞書で返す O(N)"""
        res = [[] for _ in range(self.n)]
        for i in range(self.n):
            x = self.find(i)
            res[x].append(i)
        return {r: res[r] for r in self.roots()}

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

#################################################################
def example():
    global input
    example = iter(
        """
10 2 5
1 8 9 12 13 15 16 24 27 29
        """
            .strip().split("\n"))
    input = lambda: next(example)
#################################################################

import sys
input = sys.stdin.readline
from bisect import *

# example()

N, A, B = map(int, input().split())
U = UnionFind(N)
X=list(map(int, input().split()))
for i in range(N):
    x=X[i]
    l,r=bisect_left(X,x+A),bisect_right(X,x+B)
    if l>=N: continue
    U.union_range(l,r-1)
    if r-l>0:
        U.union(i,l)
for i in range(N):
    print(U.size(i))
0