結果

問題 No.1170 Never Want to Walk
ユーザー いたいた
提出日時 2024-12-30 21:28:00
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 6,582 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 70,784 KB
最終ジャッジ日時 2024-12-30 21:28:07
合計ジャッジ時間 7,049 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
from collections import deque
class SortedSet(Generic[T]):
    BUCKET_RATIO = 50
    REBUILD_RATIO = 170
 
    def _build(self, a=None) -> None:
        "Evenly divide `a` into buckets."
        if a is None: a = list(self)
        size = self.size = len(a)
        bucket_size = int(math.ceil(math.sqrt(size / self.BUCKET_RATIO)))
        self.a = [a[size * i // bucket_size : size * (i + 1) // bucket_size] for i in range(bucket_size)]
    
    def __init__(self, a: Iterable[T] = []) -> None:
        "Make a new SortedSet from iterable. / O(N) if sorted and unique / O(N log N)"
        a = list(a)
        if not all(a[i] < a[i + 1] for i in range(len(a) - 1)):
            a = sorted(set(a))
        self._build(a)
 
    def __iter__(self) -> Iterator[T]:
        for i in self.a:
            for j in i: yield j
 
    def __reversed__(self) -> Iterator[T]:
        for i in reversed(self.a):
            for j in reversed(i): yield j
    
    def __len__(self) -> int:
        return self.size
    
    def __repr__(self) -> str:
        return "SortedSet" + str(self.a)
    
    def __str__(self) -> str:
        s = str(list(self))
        return "{" + s[1 : len(s) - 1] + "}"
 
    def _find_bucket(self, x: T) -> List[T]:
        "Find the bucket which should contain x. self must not be empty."
        for a in self.a:
            if x <= a[-1]: return a
        return a
 
    def __contains__(self, x: T) -> bool:
        if self.size == 0: return False
        a = self._find_bucket(x)
        i = bisect_left(a, x)
        return i != len(a) and a[i] == x
 
    def add(self, x: T) -> bool:
        "Add an element and return True if added. / O(√N)"
        if self.size == 0:
            self.a = [[x]]
            self.size = 1
            return True
        a = self._find_bucket(x)
        i = bisect_left(a, x)
        if i != len(a) and a[i] == x: return False
        a.insert(i, x)
        self.size += 1
        if len(a) > len(self.a) * self.REBUILD_RATIO:
            self._build()
        return True
 
    def discard(self, x: T) -> bool:
        "Remove an element and return True if removed. / O(√N)"
        if self.size == 0: return False
        a = self._find_bucket(x)
        i = bisect_left(a, x)
        if i == len(a) or a[i] != x: return False
        a.pop(i)
        self.size -= 1
        if len(a) == 0: self._build()
        return True
    
    def lt(self, x: T) -> Union[T, None]:
        "Find the largest element < x, or None if it doesn't exist."
        for a in reversed(self.a):
            if a[0] < x:
                return a[bisect_left(a, x) - 1]
 
    def le(self, x: T) -> Union[T, None]:
        "Find the largest element <= x, or None if it doesn't exist."
        for a in reversed(self.a):
            if a[0] <= x:
                return a[bisect_right(a, x) - 1]
 
    def gt(self, x: T) -> Union[T, None]:
        "Find the smallest element > x, or None if it doesn't exist."
        for a in self.a:
            if a[-1] > x:
                return a[bisect_right(a, x)]
 
    def ge(self, x: T) -> Union[T, None]:
        "Find the smallest element >= x, or None if it doesn't exist."
        for a in self.a:
            if a[-1] >= x:
                return a[bisect_left(a, x)]
    
    def __getitem__(self, x: int) -> T:
        "Return the x-th element, or IndexError if it doesn't exist."
        if x < 0: x += self.size
        if x < 0: raise IndexError
        for a in self.a:
            if x < len(a): return a[x]
            x -= len(a)
        raise IndexError
    
    def index(self, x: T) -> int:
        "Count the number of elements < x."
        ans = 0
        for a in self.a:
            if a[-1] >= x:
                return ans + bisect_left(a, x)
            ans += len(a)
        return ans
 
    def index_right(self, x: T) -> int:
        "Count the number of elements <= x."
        ans = 0
        for a in self.a:
            if a[-1] > x:
                return ans + bisect_right(a, x)
            ans += len(a)
        return ans


class UnionFind:
    def __init__(self, n):
        self._n = n
        self._parent = [-1] * n
        self._roots = set(range(n))

    def _find(self, x):
        if self._parent[x] < 0:
            return x
        self._parent[x] = self._find(self._parent[x])
        return self._parent[x]

    def union(self, x, y):
        x, y = self._find(x), self._find(y)
        if x == y:
            return
        if self._parent[y] < self._parent[x]:
            x, y = y, x
        self._parent[x] += self._parent[y]
        self._parent[y] = x
        self._roots.discard(y)

    def same(self, x, y):
        return self._find(x) == self._find(y)
    
    def size(self, x):
        return -self._parent[self._find(x)]

    def members(self, x):
        root = self._find(x)
        return [i for i in range(self._n) if self._find(i) == root]
    
    def all_group_members(self):
        group_members = dict()
        for member in range(self._n):
            root = self._find(member)
            if root not in group_members:
                group_members[root] = []
            group_members[root].append(member)
        return group_members

    def root(self, x):
        return self._find(x)

    def roots(self):
        return self._roots
    
    def group_count(self):
        return len(self.roots())
    
    def group_numbers(self):
        return [self._find(i) for i in range(self._n)]
    
    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
N,A,B=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
ne=[-1]*N
def root(N):
  if ne[N]<0:
    return N
  else:
    ne[N]=root(ne[N])
    return ne[N]
def merge(x,y):
  x,y=root(x),root(y)
  if x==y:
    return
  if ne[x]>ne[y]:
    x,y=y,x
  ne[x]+=ne[y]
  ne[y]=x
def same(x,y):
  return root(x)==root(y)
q=deque()
stp=set()
n={v:i for i,v in enumerate(x)}
x=SortedSet(x)
haiki=[]
while x:
  now=x.pop(0)
  q.append(now)
  haiki=[]
  while q:
      now=q.popleft()
      a=x.bisect_right(now-A)
      b=x.bisect_left(now-B)
      c=x.bisect_left(now+A)
      d=x.bisect_right(now+B)
      for v in range(b,a):
        if same(n[now],n[x[v]]):
          continue
        haiki.append(x[v])
        merge(n[now],n[x[v]])
        q.append(x[v])
      for v in range(c,d):
        if same(n[now],n[x[v]]):
          continue
        haiki.append(x[v])
        merge(n[now],n[x[v]])
        q.append(x[v])
  while haiki:
    x.discard(haiki.pop()) 
for i in range(N):
  print(-ne[root(i)])
0