結果

問題 No.1170 Never Want to Walk
ユーザー Craft Boss
提出日時 2025-10-07 19:35:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,107 bytes
コンパイル時間 582 ms
コンパイル使用メモリ 82,604 KB
実行使用メモリ 104,040 KB
最終ジャッジ日時 2025-10-07 19:35:16
合計ジャッジ時間 6,647 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 17 WA * 20
権限があれば一括ダウンロードができます

ソースコード

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
            
            # ▼▼▼▼▼▼▼▼▼▼▼【最終修正点】▼▼▼▼▼▼▼▼▼▼▼
            root_j_before_union = railway.find(j)
            # unionする前に、ジャンプに必要なrightmostの値を保存しておく
            rightmost_value_before_union = railway.rightmost[root_j_before_union]
            
            railway.union(i, j)
            
            # 保存しておいた値を使って、次のジャンプ先を安全に計算する
            j = rightmost_value_before_union + 1
            # ▲▲▲▲▲▲▲▲▲▲▲【最終修正点】▲▲▲▲▲▲▲▲▲▲▲

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

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