結果
問題 | No.1170 Never Want to Walk |
ユーザー |
![]() |
提出日時 | 2020-08-14 23:04:51 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 879 ms / 2,000 ms |
コード長 | 1,725 bytes |
コンパイル時間 | 240 ms |
コンパイル使用メモリ | 82,724 KB |
実行使用メモリ | 268,348 KB |
最終ジャッジ日時 | 2024-10-10 16:30:06 |
合計ジャッジ時間 | 13,595 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 37 |
ソースコード
import sys sys.setrecursionlimit(10**9) class UnionFind: def __init__(self, n): self.n = [-1]*n self.r = [0]*n self.siz = n def find_root(self, x): if self.n[x] < 0: return x else: self.n[x] = self.find_root(self.n[x]) return self.n[x] def unite(self, x, y): x = self.find_root(x) y = self.find_root(y) if x == y: return elif self.r[x] > self.r[y]: self.n[x] += self.n[y] self.n[y] = x else: self.n[y] += self.n[x] self.n[x] = y if self.r[x] == self.r[y]: self.r[y] += 1 self.siz -= 1 def root_same(self, x, y): return self.find_root(x) == self.find_root(y) def count(self, x): return -self.n[self.find_root(x)] def size(self): return self.siz n,a,b=map(int,input().split()) x=list(map(int,input().split())) from bisect import bisect_left,bisect_right num = 2**(n-1).bit_length() edge=[[]for _ in range(2*num-1)] def query(l, r, i): l += num-1 r += num-1 if l==r: edge[l].append(i) return l += 1 while r-l > 1: if ~l % 2: edge[l].append(i) if r % 2: edge[r].append(i) r -= 1 l //= 2 r = (r-1)//2 edge[l].append(i) if l == r:return edge[r].append(i) for i in range(n): if bisect_left(x,x[i]+a)==bisect_right(x,x[i]+b):continue query(bisect_left(x,x[i]+a)-1,bisect_right(x,x[i]+b)-1,i) u=UnionFind(n) ind=0 for i in range(num.bit_length()): v=num//(2**i) for j in range(0,num,v): if len(edge[ind])==0: ind+=1 continue for k in range(len(edge[ind])-1): u.unite(edge[ind][k],edge[ind][k+1]) for k in range(j,j+v): u.unite(edge[ind][0],k) ind+=1 for i in range(n): print(u.count(i))