Golang | Treemap + Fenwick
\npackage main\n\nimport (\n\t"github.com/emirpasic/gods/v2/maps/treemap"\n)\n\nfunc numberOfAlternatingGroups(colors []int, queries [][]int) []int {\n\tn := le
```\npackage main\n\nimport (\n\t"github.com/emirpasic/gods/v2/maps/treemap"\n)\n\nfunc numberOfAlternatingGroups(colors []int, queries [][]int) []int {\n\tn := len(colors)\n\ttmp := make([]int, n+n)\n\tcopy(tmp, colors)\n\tcopy(tmp[n:], colors)\n\tcolors = tmp\n\n\tbit := NewBit[int](n*2 + 2)\n\tans := make([]int, 0, len(queries))\n\ttree := treemap.New[int, int]()\n\n\tl := 0\n\tfor r := 1; r < len(colors); r++ {\n\t\tif colors[r] == colors[r-1] {\n\t\t\ttree.Put(l, r-1)\n\t\t\tbit.Update(min(r, n)-l, 1)\n\t\t\tl = r\n\t\t}\n\t}\n\n\tif l < n {\n\t\tbit.Update(n-l, 1)\n\t}\n\ttree.Put(l, len(colors)-1)\n\n\tfor _, query := range queries {\n\t\tif query[0] == 1 {\n\t\t\tsize := query[1]\n\t\t\tcount := bit.Query(size)\n\t\t\tleft, right, ok := tree.Floor(n - 1)\n\n\t\t\tif ok && right >= n {\n\t\t\t\tleft = max(left, n-size+1)\n\t\t\t\tright = min(right, n+size-2)\n\n\t\t\t\tx := right - left + 2 - size\n\t\t\t\tif x > 0 {\n\t\t\t\t\tcount += x\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tans = append(ans, count)\n\t\t} else {\n\t\t\tdo(colors, bit, query[1], tree, query[2], n, true)\n\t\t\tdo(colors, bit, query[1]+n, tree, query[2], n, false)\n\t\t}\n\t}\n\n\treturn ans\n}\n\nfunc do(colors []int, bit *Bit[int], index int, tree *treemap.Map[int, int], val int, n int, b bool) {\n\tif colors[index] == val {\n\t\treturn\n\t}\n\tcolors[index] = val\n\n\tl := index\n\tr := index\n\n\tv, ok := tree.Get(index)\n\tif ok {\n\t\ttree.Remove(index)\n\t\tif b {\n\t\t\tbit.Update(min(v, n-1)-index+1, -1)\n\t\t}\n\t\tr = v\n\t}\n\n\tk, v, ok := tree.Floor(index - 1)\n\tif ok {\n\t\ttree.Remove(k)\n\t\tif b {\n\t\t\tbit.Update(min(v, n-1)-k+1, -1)\n\t\t}\n\t\tl = k\n\n\t\tif v > index {\n\t\t\tr = v\n\t\t}\n\t}\n\n\tv, ok = tree.Get(index + 1)\n\tif ok {\n\t\tr = v\n\t\ttree.Remove(index + 1)\n\t\tif b {\n\t\t\tbit.Update(min(v, n-1)-index, -1)\n\t\t}\n\n\t}\n\n\tif index != 0 && colors[index-1] == colors[index] {\n\t\ttree.Put(l, index-1)\n\t\tif b {\n\t\t\tbit.Update(index-l, 1)\n\t\t}\n\t\tl = index\n\t}\n\n\tif index != len(colors)-1 && colors[index] == colors[index+1] {\n\t\ttree.Put(index+1, r)\n\t\tif b {\n\t\t\tbit.Update(min(r, n-1)-index, 1)\n\t\t}\n\t\tr = index\n\t}\n\n\ttree.Put(l, r)\n\tif b {\n\t\tbit.Update(min(r, n-1)-l+1, 1)\n\t}\n}\n\ntype Ints interface{ int | int64 | int32 | int16 | int8 }\n\ntype Bit[T Ints] struct {\n\tarray1 []T\n\tarray2 []T\n\tn      int\n}\n\nfunc NewBit[T Ints](n int) *Bit[T] {\n\treturn &Bit[T]{\n\t\tarray1: make([]T, n),\n\t\tarray2: make([]T, n),\n\t\tn:      n,\n\t}\n}\n\nfunc (b *Bit[T]) Update(index int, value T) {\n\tsize := index\n\tarray1 := b.array1\n\tarray2 := b.array2\n\tindex++\n\n\tfor index > 0 {\n\t\tarray1[index] += T(size) * value\n\t\tarray2[index] += value\n\t\tindex -= index & -index\n\t}\n}\n\nfunc (b *Bit[T]) Query(index int) T {\n\tsize := index\n\tindex++\n\tans := T(0)\n\tfor index < b.n {\n\t\tans += b.array1[index] + T(1-size)*b.array2[index]\n\t\tindex += index & -index\n\t}\n\treturn ans\n}\n\n```
['Go']