結果

問題 No.1170 Never Want to Walk
ユーザー Craft Boss
提出日時 2025-10-07 19:32:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,031 bytes
コンパイル時間 3,244 ms
コンパイル使用メモリ 81,912 KB
実行使用メモリ 103,888 KB
最終ジャッジ日時 2025-10-07 19:32:54
合計ジャッジ時間 6,612 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 15 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import bisect

# 再帰の制限を緩和(Nが大きい場合に対応)
sys.setrecursionlimit(10**6)

class StationConnectivity:
    def __init__(self, num_station):
        self.parent = [-1] * num_station
        self.rightmost = list(range(num_station))

    def find(self, i):
        if self.parent[i] < 0:
            return i
        else:
            self.parent[i] = self.find(self.parent[i])
            return self.parent[i]

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

        if x_root != y_root:
            if self.parent[x_root] > self.parent[y_root]:
                x_root, y_root = y_root, x_root
            
            self.parent[x_root] += self.parent[y_root]
            self.parent[y_root] = x_root
            self.rightmost[x_root] = max(self.rightmost[x_root], self.rightmost[y_root])

    def count_reachable(self, i):
        return -self.parent[self.find(i)]

def main():
    N, A, B = map(int, input().split())
    xi = list(map(int, input().split()))

    railway = StationConnectivity(N)

    for i in range(N):
        left_coord = xi[i] - B
        right_coord = xi[i] - A
        
        start_index = bisect.bisect_left(xi, left_coord, hi=i)
        
        j = start_index
        while j < i:
            if xi[j] > right_coord:
                break
            
            # ▼▼▼▼▼▼▼▼▼▼▼【修正点】▼▼▼▼▼▼▼▼▼▼▼
            # unionする「前」に、jが属するグループの情報を取得しておく
            root_j_before_union = railway.find(j)
            
            # iとjを連結
            railway.union(i, j)
            
            # union前の情報を使って次のジャンプ先を決める
            j = railway.rightmost[root_j_before_union] + 1
            # ▲▲▲▲▲▲▲▲▲▲▲【修正点】▲▲▲▲▲▲▲▲▲▲▲

    for i in range(N):
        print(railway.count_reachable(i))

if __name__ == "__main__":
    main()
0