← Home
For problem statement at 1000-1999/1800-1899/1850-1859/1858/problemE2.txt this is a correct solution, but verifier at 1000-1999/1800-1899/1850-1859/1858/verifierE2.go ends with oracle failed on case 4: runtime error: exit status 2
panic: runtime error: index out of range [-1]

goroutine 1 [running]:
main.main()
	/home/ubuntu/codeforces/1000-1999/1800-1899/1850-1859/1858/1858E2.go:55 +0x838

input:
30
+ 823087
?
?
- 1
+ 271532
?
?
- 1
+ 502854
!
- 1
+ 299092
?
+ 383245
+ 428792
+ 65818
+ 846157
!
- 1
+ 684478
+ 401696
!
!
+ 422002
?
!
+ 267953
!
?
- 1
exit status 1 can you fix the verifier? ```go
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

const MAXLOG = 21
const MAXX = 1000000

type Node struct {
	val   int
	depth int
	prev  *Node
	jump  [MAXLOG]*Node
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	line, _ := in.ReadString('\n')
	line = strings.TrimSpace(line)
	q, _ := strconv.Atoi(line)

	var current *Node = nil
	history := make([]*Node, 0, q)
	stamp := make([]int, MAXX+1)
	currentStamp := 0

	for i := 0; i < q; i++ {
		line, _ = in.ReadString('\n')
		line = strings.TrimSpace(line)
		if line == "?" {
			currentStamp++
			count := 0
			c := current
			for c != nil {
				val := c.val
				if stamp[val] != currentStamp {
					stamp[val] = currentStamp
					count++
				}
				c = c.prev
			}
			fmt.Fprintln(out, count)
			out.Flush()
		} else if line == "!" {
			current = history[len(history)-1]
			history = history[:len(history)-1]
		} else {
			parts := strings.Split(line, " ")
			op := parts[0]
			num, _ := strconv.Atoi(parts[1])
			if op == "+" {
				newNode := &Node{val: num}
				newNode.prev = current
				if current == nil {
					newNode.depth = 1
				} else {
					newNode.depth = current.depth + 1
				}
				newNode.jump[0] = newNode.prev
				for j := 1; j < MAXLOG; j++ {
					if newNode.jump[j-1] == nil {
						newNode.jump[j] = nil
					} else {
						newNode.jump[j] = newNode.jump[j-1].jump[j-1]
					}
				}
				history = append(history, current)
				current = newNode
			} else if op == "-" {
				history = append(history, current)
				if num == current.depth {
					current = nil
				} else {
					c := current
					remaining := num
					for j := MAXLOG - 1; j >= 0; j-- {
						pow := 1 << uint(j)
						if pow <= remaining && c.depth >= pow {
							c = c.jump[j]
							remaining -= pow
						}
					}
					current = c
				}
			}
		}
	}
}
```