import sequtils,queues proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "" .} proc scan(): int32 = while true: let k = getchar_unlocked() if k < '0': break result = 10.int32 * result + k.ord.int32 - '0'.ord.int32 type UnionFind[T] = object parent : seq[T] # rank : seq[int16] proc root[T](self:var UnionFind[T],x:T): T = if self.parent[x] == x: return x self.parent[x] = self.root(self.parent[x]) return self.parent[x] proc initUnionFind[T](size:int) : UnionFind[T] = result.parent = newSeqUninitialized[T](size) # result.rank = newSeq[int16](size) for i in 0.int32.. 0 and uf.parent[i-1] != 0: if uf.unite(i,i-1): ans -= 1 if y > 0 and uf.parent[i-w] != 0: if uf.unite(i,i-w): ans -= 1 else: uf.parent[i] = 0 ans -= 1 discard getchar_unlocked() i += 1 echo ans