← Home
For problem statement at 1000-1999/1400-1499/1400-1409/1403/problemA.txt this is a correct solution, but verifier at 1000-1999/1400-1499/1400-1409/1403/verifierA.go ends with case 2 failed:
input:
5 5 3 1
5 14 15 13 3
1 0
3 0
3 1
2 3 3
expected:
-1
got:
1000000000

exit status 1 can you fix the verifier? ```go
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var (
	H         []int
	updates   [][]Update
	snaps     [][][]int
	B         = 45
	visited   []int
	status    []bool
	gen       int
	queryBufX = make([]int, 0, 1000)
	queryBufY = make([]int, 0, 1000)
	addedBuf  = make([]int, 0, 100)
)

type Update struct {
	day    int
	friend int
	isAdd  bool
}

func less(a, b int) bool {
	if H[a] != H[b] {
		return H[a] < H[b]
	}
	return a < b
}

func getFriends(x, day int, out *[]int) {
	*out = (*out)[:0]

	idx := sort.Search(len(updates[x]), func(i int) bool {
		return updates[x][i].day > day
	})
	C := idx

	snapIdx := C/B - 1
	var baseSnap []int
	if snapIdx >= 0 {
		baseSnap = snaps[x][snapIdx]
	}

	var remUpdates []Update
	if snapIdx >= 0 {
		remUpdates = updates[x][(snapIdx+1)*B : C]
	} else {
		remUpdates = updates[x][:C]
	}

	gen++
	addedBuf = addedBuf[:0]
	for i := len(remUpdates) - 1; i >= 0; i-- {
		upd := remUpdates[i]
		f := upd.friend
		if visited[f] != gen {
			visited[f] = gen
			status[f] = upd.isAdd
			if upd.isAdd {
				pos := sort.Search(len(baseSnap), func(k int) bool {
					return !less(baseSnap[k], f)
				})
				inBase := (pos < len(baseSnap) && baseSnap[pos] == f)
				if !inBase {
					addedBuf = append(addedBuf, f)
				}
			}
		}
	}

	if len(addedBuf) > 1 {
		sort.Slice(addedBuf, func(i, j int) bool {
			return less(addedBuf[i], addedBuf[j])
		})
	}

	i, j := 0, 0
	for i < len(baseSnap) || j < len(addedBuf) {
		if i < len(baseSnap) && (j == len(addedBuf) || less(baseSnap[i], addedBuf[j])) {
			f := baseSnap[i]
			if visited[f] == gen && !status[f] {
			} else {
				*out = append(*out, f)
			}
			i++
		} else {
			*out = append(*out, addedBuf[j])
			j++
		}
	}
}

var scanner = bufio.NewScanner(os.Stdin)

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1024*1024), 1024*1024)
}

func nextInt() int {
	scanner.Scan()
	v, _ := strconv.Atoi(scanner.Text())
	return v
}

func main() {
	if !scanner.Scan() {
		return
	}
	N, _ := strconv.Atoi(scanner.Text())
	_ = nextInt()
	U := nextInt()
	Q := nextInt()

	H = make([]int, N)
	for i := 0; i < N; i++ {
		H[i] = nextInt()
	}

	updates = make([][]Update, N)
	activeEdge := make(map[int64]bool)

	for i := 1; i <= U; i++ {
		u := nextInt()
		v := nextInt()
		if u > v {
			u, v = v, u
		}
		code := int64(u)*int64(N) + int64(v)
		isAdd := !activeEdge[code]
		activeEdge[code] = isAdd

		updates[u] = append(updates[u], Update{day: i, friend: v, isAdd: isAdd})
		updates[v] = append(updates[v], Update{day: i, friend: u, isAdd: isAdd})
	}

	snaps = make([][][]int, N)
	for x := 0; x < N; x++ {
		var current []int
		for i, upd := range updates[x] {
			if upd.isAdd {
				idx := sort.Search(len(current), func(k int) bool {
					return !less(current[k], upd.friend)
				})
				current = append(current, 0)
				copy(current[idx+1:], current[idx:])
				current[idx] = upd.friend
			} else {
				idx := sort.Search(len(current), func(k int) bool {
					return !less(current[k], upd.friend)
				})
				if idx < len(current) && current[idx] == upd.friend {
					copy(current[idx:], current[idx+1:])
					current = current[:len(current)-1]
				}
			}

			if (i+1)%B == 0 {
				snapCopy := make([]int, len(current))
				copy(snapCopy, current)
				snaps[x] = append(snaps[x], snapCopy)
			}
		}
	}

	visited = make([]int, N)
	status = make([]bool, N)

	out := bufio.NewWriter(os.Stdout)
	for i := 0; i < Q; i++ {
		x := nextInt()
		y := nextInt()
		v := nextInt()

		getFriends(x, v, &queryBufX)
		getFriends(y, v, &queryBufY)

		ans := 1000000000
		if len(queryBufX) > 0 && len(queryBufY) > 0 {
			pi, pj := 0, 0
			for pi < len(queryBufX) && pj < len(queryBufY) {
				fx := queryBufX[pi]
				fy := queryBufY[pj]
				dist := H[fx] - H[fy]
				if dist < 0 {
					dist = -dist
				}
				if dist < ans {
					ans = dist
				}
				if ans == 0 {
					break
				}
				if H[fx] < H[fy] {
					pi++
				} else {
					pj++
				}
			}
		}
		fmt.Fprintln(out, ans)
		out.Flush()
	}
}
```