← Home
For problem statement at 0-999/200-299/220-229/220/problemB.txt this is a correct solution, but verifier at 0-999/200-299/220-229/220/verifierB.go ends with All 100 tests passed can you fix the verifier? package main

import (
	"io"
	"os"
	"sort"
)

type Query struct {
	L, R, ID, Block int
}

func main() {
	data, _ := io.ReadAll(os.Stdin)
	at := 0
	var n, m int
	n, at = readInt(data, at)
	m, at = readInt(data, at)

	a := make([]int, n+1)
	for i := 1; i <= n; i++ {
		a[i], at = readInt(data, at)
	}

	B := 316
	queries := make([]Query, m)
	for i := 0; i < m; i++ {
		var l, r int
		l, at = readInt(data, at)
		r, at = readInt(data, at)
		queries[i] = Query{L: l, R: r, ID: i, Block: l / B}
	}

	sort.Slice(queries, func(i, j int) bool {
		if queries[i].Block != queries[j].Block {
			return queries[i].Block < queries[j].Block
		}
		if queries[i].Block%2 == 1 {
			return queries[i].R < queries[j].R
		}
		return queries[i].R > queries[j].R
	})

	ans := make([]int, m)
	count := make([]int, n+1)
	currentAns := 0

	L, R := 1, 0
	for _, q := range queries {
		for L > q.L {
			L--
			x := a[L]
			if x <= n {
				if count[x] == x {
					currentAns--
				} else if count[x] == x-1 {
					currentAns++
				}
				count[x]++
			}
		}
		for R < q.R {
			R++
			x := a[R]
			if x <= n {
				if count[x] == x {
					currentAns--
				} else if count[x] == x-1 {
					currentAns++
				}
				count[x]++
			}
		}
		for L < q.L {
			x := a[L]
			if x <= n {
				if count[x] == x {
					currentAns--
				} else if count[x] == x+1 {
					currentAns++
				}
				count[x]--
			}
			L++
		}
		for R > q.R {
			x := a[R]
			if x <= n {
				if count[x] == x {
					currentAns--
				} else if count[x] == x+1 {
					currentAns++
				}
				count[x]--
			}
			R--
		}
		ans[q.ID] = currentAns
	}

	out := make([]byte, 0, m*10)
	for i := 0; i < m; i++ {
		out = appendInt(out, ans[i])
		out = append(out, '\n')
	}
	os.Stdout.Write(out)
}

func readInt(data []byte, at int) (int, int) {
	for at < len(data) && (data[at] < '0' || data[at] > '9') {
		at++
	}
	if at == len(data) {
		return 0, at
	}
	res := 0
	for at < len(data) && data[at] >= '0' && data[at] <= '9' {
		res = res*10 + int(data[at]-'0')
		at++
	}
	return res, at
}

func appendInt(b []byte, n int) []byte {
	if n == 0 {
		return append(b, '0')
	}
	var buf [20]byte
	i := 20
	for n > 0 {
		i--
		buf[i] = '0' + byte(n%10)
		n /= 10
	}
	return append(b, buf[i:]...)
}