import sequtils 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 template useUnionFind = type UnionFind[T] = object parent : seq[T] 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) 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 discard getchar_unlocked() i += 1 echo ans