← Home
For problem statement at 2000-2999/2000-2099/2060-2069/2064/problemE.txt this is a correct solution, but verifier at 2000-2999/2000-2099/2060-2069/2064/verifierE.go ends with reference runtime error on test 1 (sample): exit status 2
input:
4
1
1
1
1
5
3 4 1 2
1 1 1 1 1
5
4 2 3 1 5
2 1 4 1 5
40
29 15 20 35 37 31 27 1 32 36 38 25 22 8 16 7 3 28 11 12 23 4 14 9 39 13 10 30 6 2 24 17 19 5 34 18 33 26 40 21
3 1 2 2 1 2 3 1 1 1 1 2 1 3 1 1 3 1 1 1 2 2 1 3 3 3 2 3 2 2 2 2 1 3 2 1 1 2 2 2
output:
1
panic: runtime error: index out of range [5] with length 2

goroutine 1 [running]:
main.solveCase(0x1, {0x4000098050?, 0x40000a0f10?, 0x1?}, {0x4000098058, 0x1, 0x400003a668?})
	/home/ubuntu/codeforces/2000-2999/2000-2099/2060-2069/2064/2064E.go:140 +0x2e0
main.main()
	/home/ubuntu/codeforces/2000-2999/2000-2099/2060-2069/2064/2064E.go:183 +0x1d0
exit status 1 can you fix the verifier? package main

import (
	"io"
	"os"
)

func main() {
	buf := make([]byte, 8192)
	var pos, max int
	readNext := func() byte {
		if pos == max {
			n, err := os.Stdin.Read(buf)
			if err != nil && err != io.EOF {
				panic(err)
			}
			if n == 0 {
				return 0
			}
			max = n
			pos = 0
		}
		b := buf[pos]
		pos++
		return b
	}

	readInt := func() int {
		b := readNext()
		for b != 0 && (b < '0' || b > '9') {
			b = readNext()
		}
		if b == 0 {
			return 0
		}
		res := 0
		for b >= '0' && b <= '9' {
			res = res*10 + int(b-'0')
			b = readNext()
		}
		return res
	}

	out := make([]byte, 0, 8192)
	writeInt := func(x int64) {
		if x == 0 {
			out = append(out, '0')
			out = append(out, '\n')
			if len(out) > 4096 {
				os.Stdout.Write(out)
				out = out[:0]
			}
			return
		}
		var buf [20]byte
		i := 19
		for x > 0 {
			buf[i] = byte(x%10 + '0')
			x /= 10
			i--
		}
		out = append(out, buf[i+1:]...)
		out = append(out, '\n')
		if len(out) > 4096 {
			os.Stdout.Write(out)
			out = out[:0]
		}
	}

	t := readInt()
	if t == 0 {
		return
	}

	maxN := 200005
	parent := make([]int, maxN)
	size := make([]int, maxN)
	color := make([]int, maxN)
	prev := make([]int, maxN)
	nxt := make([]int, maxN)
	x := make([]int, maxN)
	p := make([]int, maxN)
	c := make([]int, maxN)

	for t > 0 {
		t--
		n := readInt()
		for i := 1; i <= n; i++ {
			p[i] = readInt()
		}
		for i := 1; i <= n; i++ {
			c[i] = readInt()
		}

		for i := 1; i <= n; i++ {
			x[p[i]] = i
		}

		for i := 1; i <= n; i++ {
			parent[i] = i
			size[i] = 1
			color[i] = c[i]
			prev[i] = i - 1
			nxt[i] = i + 1
		}
		prev[1] = 0
		nxt[n] = 0

		find := func(i int) int {
			root := i
			for parent[root] != root {
				root = parent[root]
			}
			curr := i
			for curr != root {
				n_node := parent[curr]
				parent[curr] = root
				curr = n_node
			}
			return root
		}

		merge := func(u, v int) {
			parent[v] = u
			size[u] += size[v]
			nxt[u] = nxt[v]
			if nxt[v] != 0 {
				prev[nxt[v]] = u
			}
		}

		curr := 1
		for nxt[curr] != 0 {
			nx := nxt[curr]
			if color[curr] == color[nx] {
				merge(curr, nx)
			} else {
				curr = nx
			}
		}

		ans := int64(1)
		mod := int64(998244353)

		for k := 1; k <= n; k++ {
			idx := x[k]
			b := find(idx)

			ans = (ans * int64(size[b])) % mod
			size[b]--

			if size[b] == 0 {
				p_blk := prev[b]
				n_blk := nxt[b]

				if p_blk != 0 {
					nxt[p_blk] = n_blk
				}
				if n_blk != 0 {
					prev[n_blk] = p_blk
				}

				if p_blk != 0 && n_blk != 0 && color[p_blk] == color[n_blk] {
					merge(p_blk, n_blk)
				}
			}
		}

		writeInt(ans)
	}
	if len(out) > 0 {
		os.Stdout.Write(out)
	}
}