std::iterator magic
Yet another solution to handle shit load of cases and manipulate map::iterator.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> numberOfAlternatingGroups(vector<int>& colors, vector<vector<int>>& queries) {\n        const int N = colors.size();\n\n        int fgr_beg = -1;\n        for (int i = 0; i < N - 1; ++i) {\n            if (colors[i] == colors[i + 1]) {\n                fgr_beg = i + 1;\n                break;\n            }\n        }\n        if (fgr_beg < 0)\n            fgr_beg = 0;\n        for (int i = 0; i < N; ++i) {\n            int j = (fgr_beg + i) % N;\n            int k = (fgr_beg + i + 1) % N;\n            if (colors[j] != colors[k]) {\n                fgr_beg = j;\n                break;\n            }\n        }\n\n        map<int, int> grbeg2len;\n        map<int, int> grlen2count;\n\n        for (int idx = 1, grb_idx = 0, i = fgr_beg; idx <= N; ++idx) {\n            int j = (fgr_beg + idx) % N;\n            if (colors[i] == colors[j] || idx == N) {\n                int len = idx - grb_idx;\n                grbeg2len[(fgr_beg + grb_idx) % N] = len;\n                ++grlen2count[len];\n                grb_idx = idx;\n            }\n            i = j;\n        }\n\n        vector<int> res;\n        res.reserve(queries.size());\n\n        for (const auto& q : queries) {\n            if (q[0] == 1) {\n                int target_len = q[1];\n                int res_count = 0;\n                for (auto it = grlen2count.lower_bound(target_len); it != grlen2count.end(); ++it)\n                    res_count += it->second * (it->first - target_len + 1);\n                if (grbeg2len.size() == 1 && grbeg2len.begin()->second == N && N % 2 == 0)\n                    res_count = N;\n                res.push_back(res_count);\n            } else {\n                int uppos = q[1];\n                int upcol = q[2];\n                if (colors[uppos] == upcol) continue;\n\n                colors[uppos] = upcol;\n\n                auto it_next = grbeg2len.upper_bound(uppos);\n                auto it = it_next;\n                iter_bw(it, grbeg2len);\n                if (it_next == grbeg2len.end())\n                    it_next = grbeg2len.begin();\n\n                auto [old_beg, old_len] = *it;\n                int old_end = (old_beg + old_len - 1) % N;\n                dec_len_count(grlen2count, old_len);\n\n                if (uppos == old_beg) {\n                    if (old_len == N) {\n                        assert(grbeg2len.size() == 1);\n                        if (N % 2 == 1) {\n                            grbeg2len.erase(it);\n                            grbeg2len[(old_beg + 1) % N] = old_len;\n                            ++grlen2count[old_len];\n                        } else {\n                            grbeg2len[old_beg] = 1;\n                            ++grlen2count[1];\n                            grbeg2len[(old_beg + 1) % N] = old_len - 1;\n                            ++grlen2count[old_len - 1];\n                        }\n                    } else if (old_len > 1) {\n                        auto it_prev = it;\n                        iter_bw(it_prev, grbeg2len);\n                        dec_len_count(grlen2count, it_prev->second);\n                        ++it_prev->second;\n                        ++grlen2count[it_prev->second];\n\n                        grbeg2len.erase(it);\n                        grbeg2len[(old_beg + 1) % N] = old_len - 1;\n                        ++grlen2count[old_len - 1];\n                    } else /* old_len == 1 */ {\n                        auto it_prev = it;\n                        iter_bw(it_prev, grbeg2len);\n                        dec_len_count(grlen2count, it_prev->second);\n                        it_prev->second += old_len;\n                        grbeg2len.erase(it);\n                        if (it_next != it_prev) {\n                            it_prev->second += it_next->second;\n                            dec_len_count(grlen2count, it_next->second);\n                            grbeg2len.erase(it_next);\n                        }\n                        ++grlen2count[it_prev->second];\n                    }\n                } else if (uppos == old_end) {\n                    if (old_len == N) {\n                        assert(grbeg2len.size() == 1);\n                        if (N % 2 == 1) {\n                            grbeg2len.erase(it);\n                            grbeg2len[(old_beg - 1 + N) % N] = old_len;\n                            ++grlen2count[old_len];\n                        } else {\n                            --it->second;\n                            ++grlen2count[it->second];\n                            grbeg2len[uppos] = 1;\n                            ++grlen2count[1];\n                        }\n                    } else if (old_len > 1) {\n                        auto [nx_beg, nx_len] = *it_next;\n                        dec_len_count(grlen2count, nx_len);\n                        grbeg2len.erase(it_next);\n                        grbeg2len[(nx_beg - 1 + N) % N] = nx_len + 1;\n                        ++grlen2count[nx_len + 1];\n\n                        --it->second;\n                        ++grlen2count[it->second];\n                    } else {\n                        assert(false); // old_len == 1 => uppos == old_beg == old_end => handled above\n                    }\n                } else {\n                    if (old_len == N && N % 2 == 0) {\n                        assert(grbeg2len.size() == 1);\n                        grbeg2len.erase(it);\n\n                        grbeg2len[uppos] = 1;\n                        ++grlen2count[1];\n\n                        grbeg2len[(uppos + 1) % N] = N - 1;\n                        ++grlen2count[N - 1];\n                    } else {\n                        grbeg2len.erase(it);\n                        int beg_half_len = (uppos - old_beg + N) % N;\n                        grbeg2len[old_beg] = beg_half_len;\n                        ++grlen2count[beg_half_len];\n\n                        grbeg2len[uppos] = 1;\n                        ++grlen2count[1];\n\n                        int end_half_len = old_len - beg_half_len - 1;\n                        grbeg2len[(uppos + 1) % N] = end_half_len;\n                        ++grlen2count[end_half_len];\n                    }\n                }\n            }\n        }\n\n        return res;\n    }\n\nprivate:\n    void dec_len_count(map<int, int>& grlen2count, int len) {\n        auto it = grlen2count.find(len);\n        if (it == grlen2count.end()) return;\n        if (it->second > 1)\n            --it->second;\n        else\n            grlen2count.erase(it);\n    }\n\n    template<typename IterT, typename CollT>\n    void iter_bw(IterT& it, CollT& coll) {\n        if (it == coll.begin())\n            it = coll.end();\n        --it;\n    }\n};\n\n```
['C++']