CSES - Bit Inversions
Author: Dustin Miao
We can use a Segment Tree over the given array of bits. Assume, for now, that we are querying for the longest continuous subsegment of 1s. In a node of the segment tree corresponding to range , we will store three pieces of information:
- The longest prefix of 1s in the range (denote this as )
- The longest suffix of 1s in the range (denote this as )
- The longest subarray of 1s in the range (denote this as )
- The length of the subarray (equivalent to ) (denote this as )
To merge left child and right child to make node
- If , then ; otherwise,
- If , then ; otherwise,
A bit flip operation corresponds to a point update on the segment tree and a longest homogeneous subarray query corresponds to the value of the root node. We can thus handle all operations efficiently. Note that using two segment trees that are inverses of each other (one for the 0s and one for the 1s) makes the implementation easier.
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;struct node {int P, S, A, L;} val0{0, 0, 0, 1}, val1{1, 1, 1, 1};node operator+(const node &a, const node &b) {return {a.P == a.L ? a.L + b.P : a.P, b.S == b.L ? b.L + a.S : b.S,max(max(a.A, b.A), a.S + b.P), a.L + b.L};
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!