Keep track of blocks...
typedef long long ll;\n\nconstexpr ll MAXN = (5e4 + 5) * 2;\nconstexpr ll BB = 360;\n\nll N;\n\nll BIT[MAXN];\nll BITv[MAXN];\n\nvoid upd(ll B, ll x, ll v) {\n 
typedef long long ll;\n\nconstexpr ll MAXN = (5e4 + 5) * 2;\nconstexpr ll BB = 360;\n\nll N;\n\nll BIT[MAXN];\nll BITv[MAXN];\n\nvoid upd(ll *B, ll x, ll v) {\n    for (; x <= N; x += (x & -x)) {\n        B[x] +=v;\n    }\n}\nll query(ll *B, ll x) {\n    ll ans = 0;\n    for (; x; x -= ( x & -x )) {\n        ans += B[x];\n    }\n    return ans;\n}\n\nll query(ll *B, ll l, ll r) {\n    return query(B, r) - query(B, l-1);\n}\n\nll val[MAXN];\n\nset<ll> stbeg;\nset<ll> stbegrev;\nll blcknxt[MAXN];\n\nbool isalt(ll x) {\n    if (x == 0) return false;\n    if (x == N) return false;\n    return val[x] != val[x + 1];\n}\n\nvoid rmblck(ll x) {\n    ll y = blcknxt[x];\n    stbeg.erase(x);\n    stbegrev.erase(-x);\n    blcknxt[x] = -1;\n    upd(BIT, y - x + 1, -1);\n    upd(BITv, y - x + 1, -(y - x + 1));\n}\n\nvoid addblck(ll x, ll y) {\n    if (x > y) return;\n    stbeg.insert(x);\n    stbegrev.insert(-x);\n    blcknxt[x] = y;\n    upd(BIT, y - x + 1, 1);\n    upd(BITv, y -x + 1, y - x + 1);\n}\n\nll nxtblck(ll x) {\n    auto it = stbeg.lower_bound(x);\n    if (it == stbeg.end()) {\n        return -1;\n    } else {\n        return *it;\n    }\n}\n\nll preblck(ll x) {\n    auto it = stbegrev.lower_bound(-x);\n    if (it == stbegrev.end()) {\n        return -1;\n    } else {\n        return -(*it);\n    }\n}\n\nll findblckin(ll x) {\n    ll pre = preblck(x);\n    if (pre == -1) return -1;\n    if (blcknxt[pre] < x) return -1;\n    return pre;\n}\n\nvoid setalt(ll x) {\n    bool origL = isalt(x - 1), origR = isalt(x);\n    if (origL && origR) {\n        ll pre = preblck(x);\n        ll nxt = blcknxt[pre];\n        rmblck(pre);\n        addblck(pre, x - 1);\n        addblck(x + 1, nxt);\n        addblck(x, x);\n    } else if (origL && !origR) {\n        ll pre = preblck(x);\n        ll nxt = blcknxt[pre];\n        rmblck(pre);\n        addblck(pre, x - 1);\n\n        if (x < N) {\n            ll nxt = blcknxt[x + 1];\n            rmblck(x + 1);\n            addblck(x, nxt);\n        } else {\n            addblck(x, x);\n        }\n    } else if (!origL && origR) {\n        ll nxt = blcknxt[x];\n        rmblck(x);\n        addblck(x + 1, nxt);\n\n        if (x > 1) {\n            ll pre = preblck(x);\n            nxt = blcknxt[pre];\n            if (nxt == x - 1) {\n                rmblck(pre);\n                addblck(pre, x);\n            } else {\n                addblck(x - 1, x);\n            }\n        } else {\n            addblck(x, x);\n        }\n    } else {\n        ll L = max(x - 1, 1ll), R = min(x + 1, N);\n\n        rmblck(x);\n\n        if (x > 1) {\n            ll pre = preblck(x - 1);\n            if (pre != -1) {\n                rmblck(pre);\n                L = pre;\n            }\n        }\n\n        if (x < N) {\n            ll nxt = blcknxt[x + 1];\n            rmblck(x + 1);\n            R = nxt;\n        }\n\n        addblck(L, R);\n    }\n\n    val[x] = 1 - val[x];\n}\n\nll calcans(ll S) {\n    ll ans = query(BITv, S, N) - query(BIT, S, N) * (S - 1);\n    if (N == 1) return ans;\n\n    if (val[1] != val[N]) {\n        ll blck = preblck(N);\n        if (blck != -1 && blcknxt[1] != -1) {\n            ll blckbeg = blcknxt[1];\n            ll stt = max(N - S + 2, blck);\n            ll edd = min(N, N + blckbeg - S + 1);\n            ans += max(edd - stt + 1, 0ll);\n        }\n    }\n    return ans;\n}\n\nclass Solution {\npublic:\n    vector<int> numberOfAlternatingGroups(vector<int>& colors, vector<vector<int>>& queries) {\n        vector<int> ans;\n        stbeg.clear();\n        stbegrev.clear();\n\n        N = colors.size();\n        for (ll i = 1; i <= N; i ++) {\n            val[i] = colors[i-1];\n            BIT[i] = BITv[i] = 0;\n            blcknxt[i] = -1;\n        }\n\n        for (ll i = 1, j; i <= N; i ++) {\n            for (j = i; j + 1 <= N && val[j] != val[j + 1]; j ++);\n            addblck(i, j);\n            // cout << i << \' \' << j << \'\\n\';\n            i = j;\n        }\n\n        for (auto &q: queries) {\n            if (q[0] == 1) {\n                ans.push_back(calcans(q[1]));\n            } else {\n                q[1] ++;\n                if (val[q[1]] != q[2]) {\n                    setalt(q[1]);\n                }\n            }\n        }\n\n        return ans;\n    }\n};
['C++']