Link
I got the main idea of the solution but I implemented the update function in a different way:
I used three segment trees:
1. stsum : for finding the sum in the range
2. st1 : for finding number of ones in the range
3. st2 : for finding number of twos in the range
void update(int l, int r) {
if (r < l) return;
if (n1(l, r) + n2(l, r) == r - l + 1) return;
if (r - l <= 1) {
for (int i = l; i <= r; ++i) {
stsum.update(i, d[a[i]]);
a[i] = d[a[i]];
if (a[i] == 2) {
st2.update(i, 1);
}
}
return;
}
int mid = (r + l) / 2;
update(l, mid);
update(mid + 1, r);
}
Above, functions n1(l, r): returns number of 1's in the range l to r and n2(l, r) returns the number of 2's in the range.
I'm calling this function whenever there is update query.
What is the correct time complexity of the solution using this function for update?