← Home
For problem statement at 1000-1999/1700-1799/1740-1749/1744/problemF.txt this is a correct solution, but verifier at 1000-1999/1700-1799/1740-1749/1744/verifierF.go ends with All tests passed can you fix the verifier? package main

import (
	"bytes"
	"io"
	"os"
	"strconv"
)

func countPairs(A, B, K int64) int64 {
	if A < 0 || B < 0 || K < 0 {
		return 0
	}
	if A > K {
		A = K
	}
	if B > K {
		B = K
	}
	t := K - B
	if t >= A {
		return (A + 1) * (B + 1)
	}
	first := (t + 1) * (B + 1)
	rem := A - t
	sum := (t + 1 + A) * rem / 2
	second := rem*(K+1) - sum
	return first + second
}

func main() {
	data, _ := io.ReadAll(os.Stdin)
	idx := 0
	nextInt := func() int {
		for idx < len(data) && (data[idx] < '0' || data[idx] > '9') {
			idx++
		}
		val := 0
		for idx < len(data) && data[idx] >= '0' && data[idx] <= '9' {
			val = val*10 + int(data[idx]-'0')
			idx++
		}
		return val
	}

	t := nextInt()
	var out bytes.Buffer

	for ; t > 0; t-- {
		n := nextInt()
		pos := make([]int, n)
		for i := 1; i <= n; i++ {
			v := nextInt()
			pos[v] = i
		}

		var ans int64 = 1
		L, R := pos[0], pos[0]

		for x := 1; x < n; x++ {
			s := R - L + 1
			K := 2*x - s
			if K >= 0 {
				p := pos[x]
				if p < L {
					ans += countPairs(int64(L-p-1), int64(n-R), int64(K))
				} else if p > R {
					ans += countPairs(int64(L-1), int64(p-R-1), int64(K))
				}
			}
			if pos[x] < L {
				L = pos[x]
			}
			if pos[x] > R {
				R = pos[x]
			}
		}

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

	os.Stdout.Write(out.Bytes())
}