For problem statement at 2000-2999/2100-2199/2130-2139/2137/problemF.txt this is a correct solution, but verifier at 2000-2999/2100-2199/2130-2139/2137/verifierF.go ends with candidate runtime error: exit status 2
panic: runtime error: index out of range [-3]
goroutine 1 [running]:
main.(*BIT).Update(...)
/tmp/cf-worker-2188750638/main.go:17
main.main()
/tmp/cf-worker-2188750638/main.go:81 +0x4f4 can you fix the verifier? package main
import (
"io"
"os"
"strconv"
"strings"
)
type BIT struct {
n int
bit []int
}
func (b *BIT) Update(pos, val int) {
for pos <= b.n {
if val > b.bit[pos] {
b.bit[pos] = val
}
pos += pos & -pos
}
}
func (b *BIT) Query(pos int) int {
res := 0
for pos > 0 {
if b.bit[pos] > res {
res = b.bit[pos]
}
pos -= pos & -pos
}
return res
}
func main() {
data, _ := io.ReadAll(os.Stdin)
idx := 0
nextInt := func() int {
for idx < len(data) && data[idx] <= ' ' {
idx++
}
val := 0
for idx < len(data) && data[idx] > ' ' {
val = val*10 + int(data[idx]-'0')
idx++
}
return val
}
t := nextInt()
var out strings.Builder
for ; t > 0; t-- {
n := nextInt()
a := make([]int, n+1)
b := make([]int, n+1)
for i := 1; i <= n; i++ {
a[i] = nextInt()
}
for i := 1; i <= n; i++ {
b[i] = nextInt()
}
v := 2 * n
bit := BIT{n: v, bit: make([]int, v+2)}
var ans int64
for i := 1; i <= n; i++ {
h := 0
if a[i] == b[i] {
h = i
} else {
th := a[i]
if b[i] > th {
th = b[i]
}
h = bit.Query(v - th + 1)
}
ans += int64(h) * int64(n-i+1)
bit.Update(v-a[i]+1, i)
}
out.WriteString(strconv.FormatInt(ans, 10))
if t > 1 {
out.WriteByte('\n')
}
}
os.Stdout.Write([]byte(out.String()))
}