My code
\nimpl Solution {\n    fn lowbit(x: i32) -> i32 {\n        x & -x\n    }\n\n    fn update<T>(mut i: i32, x: T, c: &mut [T])\n    where\n        T: std::ops::Add
```\nimpl Solution {\n    fn lowbit(x: i32) -> i32 {\n        x & -x\n    }\n\n    fn update<T>(mut i: i32, x: T, c: &mut [T])\n    where\n        T: std::ops::AddAssign + Copy,\n    {\n        if i == 0 {\n            return;\n        }\n        while i < c.len() as i32 {\n            c[i as usize] += x;\n            i += Self::lowbit(i);\n        }\n    }\n\n    fn getsum<T>(mut i: i32, c: &[T]) -> T\n    where\n        T: std::ops::Add<Output = T> + Copy + Default,\n    {\n        let mut r = T::default();\n        while i != 0 {\n            r = r + c[i as usize];\n            i -= Self::lowbit(i);\n        }\n        r\n    }\n\n    fn insert(n: i32, x: i32, s: &mut std::collections::BTreeSet<i32>, count: &mut [i32], sum: &mut [i64]) {\n        s.insert(x);\n        let t = s.get(&x).unwrap();\n        if s.len() == 1 {\n            Self::update(n, 1, count);\n            Self::update(n, n as i64, sum);\n        } else {\n            let before = s.range(..t).next_back().unwrap_or(s.last().unwrap());\n            let after = s.range(*t + 1..).next().unwrap_or(s.first().unwrap());\n\n            let len1 = if s.len() == 2 { n } else { (*after - *before + n) % n };\n            Self::update(len1, -1, count);\n            Self::update(len1, -len1 as i64, sum);\n            let len2 = (*t - *before + n) % n;\n            Self::update(len2, 1, count);\n            Self::update(len2, len2 as i64, sum);\n            let len3 = (*after - *t + n) % n;\n            Self::update(len3, 1, count);\n            Self::update(len3, len3 as i64, sum);\n        }\n    }\n\n    fn remove(n: i32, x: i32, s: &mut std::collections::BTreeSet<i32>, count: &mut [i32], sum: &mut [i64]) {\n        if s.len() == 1 {\n            s.clear();\n            Self::update(n, -1, count);\n            Self::update(n, -n as i64, sum);\n        } else {\n            let t = s.get(&x).unwrap();\n            let before = s.range(..t).next_back().unwrap_or(s.last().unwrap());\n            let after = s.range(*t + 1..).next().unwrap_or(s.first().unwrap());\n            let len1 = if s.len() == 2 { n } else { (*after - *before + n) % n };\n            Self::update(len1, 1, count);\n            Self::update(len1, len1 as i64, sum);\n            let len2 = (*t - *before + n) % n;\n            Self::update(len2, -1, count);\n            Self::update(len2, -len2 as i64, sum);\n            let len3 = (*after - *t + n) % n;\n            Self::update(len3, -1, count);\n            Self::update(len3, -len3 as i64, sum);\n            s.remove(&x);\n        }\n    }\n\n    pub fn number_of_alternating_groups(colors: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> {\n        let mut colors = colors;\n        let n = colors.len() as i32;\n        let mut s = std::collections::BTreeSet::new();\n        let mut sum = vec![0; n as usize + 1];\n        let mut count = vec![0; n as usize + 1];\n        let mut r = Vec::new();\n        for i in 1..n {\n            if colors[i as usize] == colors[i as usize - 1] {\n                Self::insert(n, i - 1, &mut s, &mut count, &mut sum);\n            }\n        }\n        if colors[n as usize - 1] == colors[0] {\n            Self::insert(n, n - 1, &mut s, &mut count, &mut sum);\n        }\n        for q in queries {\n            if q[0] == 1 {\n                let k = q[1];\n                r.push(if s.is_empty() {\n                    n\n                } else {\n                    Self::getsum(n, &sum) as i32\n                        - Self::getsum(k - 1, &sum) as i32\n                        - (k - 1) * (Self::getsum(n, &count) - Self::getsum(k - 1, &count))\n                });\n            } else {\n                let ind = q[1];\n                if colors[ind as usize] == q[2] {\n                    continue;\n                }\n                colors[ind as usize] = q[2];\n                let before = (ind + n - 1) % n;\n                if colors[ind as usize] == colors[before as usize] {\n                    Self::insert(n, before, &mut s, &mut count, &mut sum);\n                } else {\n                    Self::remove(n, before, &mut s, &mut count, &mut sum);\n                }\n                let after = (ind + 1) % n;\n                if colors[ind as usize] == colors[after as usize] {\n                    Self::insert(n, ind as i32, &mut s, &mut count, &mut sum);\n                } else {\n                    Self::remove(n, ind as i32, &mut s, &mut count, &mut sum);\n                }\n            }\n        }\n        r\n    }\n}\n\n```
['Rust']