[Python3] sorted list + fenwick tree
\n\nfrom sortedcontainers import SortedList\n\nclass Fenwick: \n\n    def __init__(self, n: int):\n        self.cnts = [0]*(n+1)\n        self.vals = [0]*(n+1)\
\n```\nfrom sortedcontainers import SortedList\n\nclass Fenwick: \n\n    def __init__(self, n: int):\n        self.cnts = [0]*(n+1)\n        self.vals = [0]*(n+1)\n            \n    def add(self, k: int, v: int) -> None: \n        i = k+1\n        while i < len(self.cnts): \n            self.cnts[i] += v\n            self.vals[i] += k*v\n            i += i & -i \n    \n    def query(self, k: int, v: int) -> int: \n        ans = 0\n        i = k+1\n        while i:\n            ans += self.vals[i] - v*self.cnts[i]\n            i -= i & -i\n        return ans\n    \n\nclass Solution:\n    def numberOfAlternatingGroups(self, colors: List[int], queries: List[List[int]]) -> List[int]:\n        n = len(colors)\n        i = 0 \n        mp = {}\n        while i < n: \n            for j in range(i, i+n): \n                if colors[j%n] == colors[(j+1)%n]: break \n            mp[j%n] = i\n            i = j+1\n        groups = SortedList()\n        fen = Fenwick(n+1)\n        \n        dist = lambda i, j: j-i+1 if i <= j else n+j-i+1\n        \n        def add(i, j): \n            groups.add((j, i))\n            fen.add(dist(i, j), 1)\n            \n        def remove(k): \n            j, i = groups.pop(k)\n            fen.add(dist(i, j), -1)\n            return i, j\n        \n        for j, i in mp.items(): add(i, j)\n        ans = []\n        for q in queries: \n            if q[0] == 1:\n                if len(groups) == 1 and colors[groups[0][0]] != colors[groups[0][1]]: val = n\n                else: \n                    _, sz = q\n                    val = min(n, fen.query(n, sz-1) - fen.query(sz-1, sz-1))\n                ans.append(val)\n            else: \n                _, i, c = q\n                if colors[i] != c: \n                    colors[i] = c\n                    k = groups.bisect_left((i, 0)) % len(groups)\n                    lo, hi = remove(k)\n                    if lo == i == hi: \n                        if colors[(i-1)%n] != colors[i]: lo = remove((k-1) % len(groups))[0]\n                        if colors[i] != colors[(i+1)%n] and groups: hi = remove((k-1) % len(groups) if k else 0)[1]\n                        add(lo, hi)\n                    elif lo == i != hi: \n                        add((i+1)%n, hi)\n                        if colors[(i-1)%n] != colors[i]: lo = remove((k-1)%len(groups))[0]\n                        add(lo, i)\n                    elif lo != i == hi: \n                        add(lo, (i-1)%n)\n                        if colors[i] != colors[(i+1)%n]: hi = remove((k+1)%len(groups) if i else 0)[1]\n                        add(i, hi)\n                    else: \n                        i0 = (i-1)%n\n                        i1 = (i+1)%n\n                        if dist(lo, hi) == n and colors[lo] != colors[hi]: add(i1, i0)\n                        else: \n                            add(lo, i0)\n                            add(i1, hi)\n                        add(i, i)\n        return ans \n```
['Python3']