← Home
For problem statement at 1000-1999/1600-1699/1680-1689/1684/problemF.txt this is a correct solution, but verifier at 1000-1999/1600-1699/1680-1689/1684/verifierF.go ends with wrong answer on test 48
input:
1
6 3
2 6 2 6 2 6
5 6
2 4
1 5
expected:
2

got:
3

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

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

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		solve()
	}
}

func solve() {
	var n, m int
	fmt.Fscan(in, &n, &m)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}

	// minReq[i] stores the minimum l for all queries with r >= i
	// Effectively, for a position v, minReq[v] tells us the starting point 
	// of the "longest" query (left-most start) that ends at or after v.
	minReq := make([]int, n+2)
	for i := 0; i <= n+1; i++ {
		minReq[i] = n + 1
	}

	for i := 0; i < m; i++ {
		var l, r int
		fmt.Fscan(in, &l, &r)
		if l < minReq[r] {
			minReq[r] = l
		}
	}

	// Propagate minReq backwards: if a query covers r+1, it covers r (for our purposes of intersection)
	// Wait, actually: if we have a query [l, r], it imposes constraints on pairs (u, v) inside it.
	// Pair (u, v) is active if there is some query [l, r] such that l <= u < v <= r.
	// This is equivalent to u >= min_l { queries with r_j >= v }.
	// So we need suffix minimum for minReq.
	for i := n; i >= 1; i-- {
		if minReq[i+1] < minReq[i] {
			minReq[i] = minReq[i+1]
		}
	}

	type pair struct {
		val, idx int
	}
	pairs := make([]pair, n)
	for i := 0; i < n; i++ {
		pairs[i] = pair{a[i], i + 1}
	}

	sort.Slice(pairs, func(i, j int) bool {
		if pairs[i].val != pairs[j].val {
			return pairs[i].val < pairs[j].val
		}
		return pairs[i].idx < pairs[j].idx
	})

	minV := n + 2
	maxU := -1
	events := make([]int, n+2)
	hasActive := false

	// Group by value
	for i := 0; i < n; {
		j := i
		for j < n && pairs[j].val == pairs[i].val {
			j++
		}

		group := pairs[i:j]
		k := len(group)
		indices := make([]int, k)
		for idx := 0; idx < k; idx++ {
			indices[idx] = group[idx].idx
		}

		// Check adjacent pairs for global bounds
		for idx := 0; idx < k-1; idx++ {
			u := indices[idx]
			v := indices[idx+1]
			// Pair (u, v) is active if u >= minReq[v]
			if u >= minReq[v] {
				hasActive = true
				if v < minV {
					minV = v
				}
				if u > maxU {
					maxU = u
				}
			}
		}

		// For P array: find for each v, the smallest u that activates it.
		// We want to prevent cases where u < L and v > R.
		// So if u < L, we must have R >= v.
		// We compute P[x] = max v such that exists active (u, v) with u <= x.
		for idx := 1; idx < k; idx++ {
			v := indices[idx]
			req := minReq[v]
			if req > n {
				continue
			}

			// Find smallest pos in indices[0...idx-1] such that indices[pos] >= req
			pos := sort.Search(idx, func(x int) bool {
				return indices[x] >= req
			})

			if pos < idx {
				u := indices[pos]
				// active pair (u, v)
				if v > events[u] {
					events[u] = v
				}
				hasActive = true
			}
		}
		i = j
	}

	if !hasActive {
		fmt.Fprintln(out, 0)
		return
	}

	// Build prefix max array P
	P := make([]int, n+2)
	curr := 0
	for i := 1; i <= n; i++ {
		if events[i] > curr {
			curr = events[i]
		}
		P[i] = curr
	}

	ans := n + 1
	// L can range from 1 to minV. If L > minV, then there is a pair (u, minV)
	// such that minV < L -> pair is completely to the left of [L, R], which is invalid.
	for L := 1; L <= minV; L++ {
		// We need R >= maxU (to cover u of pairs with u >= L)
		// We need R >= P[L-1] (to cover v of pairs with u < L)
		reqR := maxU
		if P[L-1] > reqR {
			reqR = P[L-1]
		}

		length := reqR - L + 1
		if length < ans {
			ans = length
		}
	}
	fmt.Fprintln(out, ans)
}
```