結果

問題 No.871 かえるのうた
ユーザー kawacchu
提出日時 2019-08-30 22:11:40
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 1,506 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 38,060 KB
最終ジャッジ日時 2024-11-21 23:55:07
合計ジャッジ時間 18,680 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15 WA * 30 TLE * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

from bisect import *
from collections import deque

class UnionFind:
    def __init__(self, num):
        self.rank = [0] * num
        self.par = [i for i in range(num)]
        self.n = num
 
    def find_root(self, node):
        if self.par[node] == node:
            return node
        else:
            self.par[node] = self.find_root(self.par[node])
            return self.par[node]
 
    def same_root(self, x, y):
        return self.find_root(x) == self.find_root(y)
 
    def union(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        if self.rank[x] > self.rank[y]:
            self.par[y] = x
        else:
            self.par[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

N,K = map(int,input().split())
X = list(map(int,input().split()))
A = list(map(int,input().split()))

UF = UnionFind(N)
q = deque([K-1])
while q :
    indn = q.popleft()
    x = X[indn]
    indl = bisect_left(X,x-A[indn])
    for j in range(indl,indn) :
        if UF.same_root(j,indn) :
            continue
        UF.union(j,indn)
        q.append(j)
q = deque([K-1])
while q :
    indn = q.popleft()
    indr = bisect_right(X,x+A[indn])-1
    for j in range(indn+1,indr+1) :
        if UF.same_root(indn,j) :
            continue
        UF.union(indn,j)
        q.append(j)
    


ans = 0
for i in range(N) :
    if UF.same_root(i,K-1) :
        ans += 1
print(ans)
0