← Home
For problem statement at 1000-1999/1700-1799/1790-1799/1793/problemD.txt this is a correct solution, but verifier at 1000-1999/1700-1799/1790-1799/1793/verifierD.go ends with All tests passed can you fix the verifier? package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

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
	}

	n := nextInt()
	ppos := make([]int, n+1)
	qpos := make([]int, n+1)

	for i := 1; i <= n; i++ {
		ppos[nextInt()] = i
	}
	for i := 1; i <= n; i++ {
		qpos[nextInt()] = i
	}

	tri := func(length int) int64 {
		if length <= 0 {
			return 0
		}
		x := int64(length)
		return x * (x + 1) / 2
	}

	a, b := ppos[1], qpos[1]
	x, y := a, b
	if x > y {
		x, y = y, x
	}

	var ans int64
	ans += tri(x-1) + tri(y-x-1) + tri(n-y)

	L, R := a, b
	if L > R {
		L, R = R, L
	}

	for m := 2; m <= n; m++ {
		u, v := ppos[m], qpos[m]

		if !((u >= L && u <= R) || (v >= L && v <= R)) {
			maxLeft := 0
			minRight := n + 1

			if u < L {
				if u > maxLeft {
					maxLeft = u
				}
			} else {
				if u < minRight {
					minRight = u
				}
			}

			if v < L {
				if v > maxLeft {
					maxLeft = v
				}
			} else {
				if v < minRight {
					minRight = v
				}
			}

			ans += int64(L-maxLeft) * int64(minRight-R)
		}

		if u < L {
			L = u
		}
		if v < L {
			L = v
		}
		if u > R {
			R = u
		}
		if v > R {
			R = v
		}
	}

	ans++

	out := bufio.NewWriter(os.Stdout)
	fmt.Fprintln(out, ans)
	out.Flush()
}