learn smart way
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfrom sortedcontainers import SortedList\n\nclass Solution:\n    def numberOfAlternatingGroups(self, colors: List[int], queries: List[List[int]]) -> List[int]:\n        n = len(colors)\n        counts = Counter()\n        store = SortedList()\n\n        def add(g):\n            store.add(g)\n            key = width(g)\n            counts[key] += 1\n        \n        def remove(g):\n            # print(g)\n            # if g not in store:\n            #     exit()\n            store.remove(g)\n            key = width(g)\n            counts[key] -= 1\n            if counts[key] == 0:\n                del counts[key]\n\n        def init():\n            i = 0\n            groups = []\n            while i < len(colors):\n                l = i\n                while i+1 < len(colors) and colors[i] != colors[i+1]:\n                    i += 1\n                r = i\n                groups.append((l, r))\n                i += 1\n            if colors[-1] != colors[0] and len(groups) > 1:\n                groups[0] = (groups[-1][0], groups[0][1])\n                groups.pop()\n            for g in groups:\n                add(g)\n        \n        def merge(x):\n            if i == 0 and store[i][0] > i:\n                i = len(store) - 1\n            g1 = store[i]\n            g2 = store[(i+1) % len(store)]\n            remove(g1)\n            remove(g2)\n            add((g1[0], g2[1]))\n        \n        def at(x):\n            return colors[x % n]\n        \n        def group(x):\n            x %= n\n            i = store.bisect_right((x, inf)) - 1\n            return store[i % len(store)]\n        \n        def width(g):\n            l, r = g\n            if l > r:\n                l -= n\n            return (r-l+1)\n\n        init()\n        ans = []\n        for q in queries:\n            # print(colors)\n            # print(store)\n            if q[0] == 1:\n                size = q[1]\n                total = 0\n                # print(counts)\n                for k, v in counts.items():\n                    if k == n and n % 2 == 0:\n                        total += n\n                    elif k >= size:\n                        total += v * (k - size + 1)\n                ans.append(total)\n            else:\n                x, c = q[1:]\n                if c == at(x):\n                    continue\n                g = group(x)\n                if at(x-1) != at(x) and at(x) != at(x+1):\n                    remove(g)\n                    add((x, x))\n                    if width(g) == n and n % 2 == 0:\n                        add(((x+1)%n, (x-1)%n))\n                    else:\n                        add((g[0], (x-1)%n))\n                        add(((x+1)%n, g[1]))\n                elif at(x-1) != at(x):\n                    g1 = group(x+1)\n                    remove(g)\n                    if g1 == g:\n                        add((x, (x-1)%n))\n                    else:\n                        remove(g1)\n                        add((g[0], (x-1)%n))\n                        add((x, g1[1]))\n                elif at(x) != at(x+1):\n                    g1 = group(x-1)\n                    remove(g)\n                    if g1 == g:\n                        add(((x+1)%n, x))\n                    else:\n                        remove(g1)\n                        add((g1[0], x))\n                        add(((x+1)%n, g[1]))\n                else:\n                    g1 = group(x-1)\n                    g2 = group(x+1)\n                    remove(g)\n                    remove(g1)\n                    if g1 == g2:\n                        add((0, n-1))\n                    else:\n                        remove(g2)\n                        add((g1[0], g2[1]))\n                colors[x] = c\n        return ans\n\n```
['Python3']