結果

問題 No.1170 Never Want to Walk
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-03-29 23:54:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 501 ms / 2,000 ms
コード長 2,844 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 81,900 KB
実行使用メモリ 118,712 KB
最終ジャッジ日時 2023-10-21 13:22:11
合計ジャッジ時間 9,962 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
68,324 KB
testcase_01 AC 55 ms
68,324 KB
testcase_02 AC 62 ms
68,324 KB
testcase_03 AC 56 ms
68,324 KB
testcase_04 AC 57 ms
68,324 KB
testcase_05 AC 56 ms
68,324 KB
testcase_06 AC 58 ms
68,324 KB
testcase_07 AC 57 ms
68,324 KB
testcase_08 AC 57 ms
68,324 KB
testcase_09 AC 54 ms
68,324 KB
testcase_10 AC 54 ms
68,324 KB
testcase_11 AC 54 ms
68,324 KB
testcase_12 AC 78 ms
77,504 KB
testcase_13 AC 81 ms
77,536 KB
testcase_14 AC 83 ms
77,532 KB
testcase_15 AC 80 ms
77,508 KB
testcase_16 AC 80 ms
77,536 KB
testcase_17 AC 81 ms
77,520 KB
testcase_18 AC 78 ms
77,504 KB
testcase_19 AC 78 ms
77,508 KB
testcase_20 AC 79 ms
77,532 KB
testcase_21 AC 80 ms
77,524 KB
testcase_22 AC 83 ms
77,608 KB
testcase_23 AC 85 ms
77,652 KB
testcase_24 AC 85 ms
77,620 KB
testcase_25 AC 83 ms
77,580 KB
testcase_26 AC 84 ms
77,612 KB
testcase_27 AC 451 ms
118,508 KB
testcase_28 AC 458 ms
118,428 KB
testcase_29 AC 501 ms
118,712 KB
testcase_30 AC 453 ms
118,608 KB
testcase_31 AC 464 ms
118,484 KB
testcase_32 AC 317 ms
118,400 KB
testcase_33 AC 379 ms
117,864 KB
testcase_34 AC 396 ms
118,548 KB
testcase_35 AC 370 ms
118,536 KB
testcase_36 AC 320 ms
118,316 KB
testcase_37 AC 362 ms
118,344 KB
testcase_38 AC 332 ms
118,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import Callable, DefaultDict, Optional, List
from bisect import bisect_left, bisect_right
from collections import defaultdict


class UnionFindRange:
    __slots__ = "part", "_n", "_parent", "_rank"

    def __init__(self, n: int):
        self.part = n
        self._n = n
        self._parent = list(range(n))
        self._rank = [1] * n

    def find(self, x: int) -> int:
        while x != self._parent[x]:
            self._parent[x] = self._parent[self._parent[x]]
            x = self._parent[x]
        return x

    def union(self, x: int, y: int, f: Optional[Callable[[int, int], None]] = None) -> bool:
        """union后, 大的编号所在的组的指向小的编号所在的组."""
        if x < y:
            x, y = y, x
        rootX = self.find(x)
        rootY = self.find(y)
        if rootX == rootY:
            return False
        self._parent[rootX] = rootY
        self._rank[rootY] += self._rank[rootX]
        self.part -= 1
        if f is not None:
            f(rootY, rootX)
        return True

    def unionRange(
        self, left: int, right: int, f: Optional[Callable[[int, int], None]] = None
    ) -> int:
        """合并[left,right]区间, 返回合并次数."""
        if left > right:
            left, right = right, left
        leftRoot = self.find(left)
        rightRoot = self.find(right)
        unionCount = 0
        while rightRoot != leftRoot:
            unionCount += 1
            self.union(rightRoot, rightRoot - 1, f)
            rightRoot = self.find(rightRoot - 1)
        return unionCount

    def isConnected(self, x: int, y: int) -> bool:
        return self.find(x) == self.find(y)

    def getSize(self, x: int) -> int:
        return self._rank[self.find(x)]

    def getGroup(self) -> DefaultDict[int, List[int]]:
        group = defaultdict(list)
        for i in range(self._n):
            group[self.find(i)].append(i)
        return group


if __name__ == "__main__":

    # No.1170 Never Want to Walk
    # https://yukicoder.me/problems/no/1170
    # 数轴上有n个车站,第i个位置在xi
    # 如果两个车站之间的距离di与dj满足 A<=|di-dj|<=B,则这两个车站可以相互到达,否则不能相互到达
    # 对每个车站,求出从该车站出发,可以到达的车站的数量
    # 1<=n<=2e5 0<=A<=B<=1e9 0<=x1<=x2<...<=xn<=1e9

    # !每个车站向右合并可以到达的车站,把合并分解为单点合并+区间合并
    n, A, B = map(int, input().split())
    pos = list(map(int, input().split()))
    uf = UnionFindRange(n)
    for i, p in enumerate(pos):
        left = bisect_left(pos, p + A)
        right = bisect_right(pos, p + B) - 1
        if right >= left:  # 有可以到达的车站
            uf.union(i, left)
            uf.unionRange(left, right)
    for i in range(n):
        print(uf.getSize(i))
0