← Home
For problem statement at 1000-1999/1800-1899/1840-1849/1840/problemF.txt this is a correct solution, but verifier at 1000-1999/1800-1899/1840-1849/1840/verifierF.go ends with All tests passed can you fix the verifier? ```go
package main

import (
	"io"
	"os"
	"sort"
	"strconv"
	"strings"
)

const INF int64 = 1 << 60

type Interval struct {
	l int64
	r int64
}

func add1(x int64) int64 {
	if x >= INF/2 {
		return INF
	}
	return x + 1
}

func sortUnique(a []int64) []int64 {
	if len(a) == 0 {
		return a
	}
	sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
	k := 1
	for i := 1; i < len(a); i++ {
		if a[i] != a[k-1] {
			a[k] = a[i]
			k++
		}
	}
	return a[:k]
}

func buildSafe(a, b []int64) []Interval {
	res := make([]Interval, 0, len(a)+len(b)+1)
	i, j := 0, 0
	prev := int64(-1)
	for i < len(a) || j < len(b) {
		var x int64
		if j == len(b) || (i < len(a) && a[i] < b[j]) {
			x = a[i]
		} else if i == len(a) || b[j] < a[i] {
			x = b[j]
		} else {
			x = a[i]
		}
		for i < len(a) && a[i] == x {
			i++
		}
		for j < len(b) && b[j] == x {
			j++
		}
		if prev+1 <= x-1 {
			res = append(res, Interval{prev + 1, x - 1})
		}
		prev = x
	}
	res = append(res, Interval{prev + 1, INF})
	return res
}

func mergeShiftUnion(a, b []Interval) []Interval {
	if len(a) == 0 && len(b) == 0 {
		return nil
	}
	res := make([]Interval, 0, len(a)+len(b))
	i, j := 0, 0
	for i < len(a) || j < len(b) {
		var x Interval
		if j == len(b) || (i < len(a) && a[i].l <= b[j].l) {
			x = Interval{add1(a[i].l), add1(a[i].r)}
			i++
		} else {
			x = Interval{add1(b[j].l), add1(b[j].r)}
			j++
		}
		if len(res) == 0 {
			res = append(res, x)
		} else {
			last := &res[len(res)-1]
			if x.l <= add1(last.r) {
				if x.r > last.r {
					last.r = x.r
				}
			} else {
				res = append(res, x)
			}
		}
	}
	return res
}

func intersectUnionSafe(u, s []Interval) []Interval {
	if len(u) == 0 || len(s) == 0 {
		return nil
	}
	res := make([]Interval, 0, len(s))
	i, j := 0, 0
	for i < len(u) && j < len(s) {
		if u[i].r < s[j].l {
			i++
			continue
		}
		if s[j].r < u[i].l {
			j++
			continue
		}
		start := s[j].l
		if u[i].l > start {
			start = u[i].l
		}
		sr := s[j].r
		res = append(res, Interval{start, sr})
		j++
		if u[i].r <= sr {
			i++
		}
	}
	return res
}

func main() {
	data, _ := io.ReadAll(os.Stdin)
	idx := 0
	nextInt := func() int64 {
		for idx < len(data) && (data[idx] < '0' || data[idx] > '9') && data[idx] != '-' {
			idx++
		}
		if idx >= len(data) {
			return 0
		}
		sign := int64(1)
		if data[idx] == '-' {
			sign = -1
			idx++
		}
		var v int64
		for idx < len(data) && data[idx] >= '0' && data[idx] <= '9' {
			v = v*10 + int64(data[idx]-'0')
			idx++
		}
		return sign * v
	}

	T := int(nextInt())
	var out strings.Builder

	for ; T > 0; T-- {
		n := int(nextInt())
		m := int(nextInt())
		r := int(nextInt())

		rowShots := make([][]int64, n+1)
		colShots := make([][]int64, m+1)

		for k := 0; k < r; k++ {
			t := nextInt()
			d := int(nextInt())
			c := int(nextInt())
			if d == 1 {
				rowShots[c] = append(rowShots[c], t)
			} else {
				colShots[c] = append(colShots[c], t)
			}
		}

		for i := 0; i <= n; i++ {
			rowShots[i] = sortUnique(rowShots[i])
		}
		for j := 0; j <= m; j++ {
			colShots[j] = sortUnique(colShots[j])
		}

		prevRow := make([][]Interval, m+1)

		for i := 0; i <= n; i++ {
			currRow := make([][]Interval, m+1)
			for j := 0; j <= m; j++ {
				safe := buildSafe(rowShots[i], colShots[j])

				if i == 0 && j == 0 {
					currRow[j] = []Interval{{0, safe[0].r}}
					continue
				}

				var top, left []Interval
				if i > 0 {
					top = prevRow[j]
				}
				if j > 0 {
					left = currRow[j-1]
				}

				if len(top) == 0 && len(left) == 0 {
					continue
				}

				union := mergeShiftUnion(top, left)
				currRow[j] = intersectUnionSafe(union, safe)
			}
			prevRow = currRow
		}

		ans := int64(-1)
		if len(prevRow[m]) > 0 {
			ans = prevRow[m][0].l
		}

		out.WriteString(strconv.FormatInt(ans, 10))
		out.WriteByte('\n')
	}

	os.Stdout.Write([]byte(out.String()))
}
```