You are given a binary string s of length n, where:
'1' represents an active section.
'0' represents an inactive section.
You can perform at most one trade to maximize the number of active sections in s. In a trade, you:
Convert a contiguous block of '1's that is surrounded by '0's to all '0's.
Afterward, convert a contiguous block of '0's that is surrounded by '1's to all '1's.
Additionally, you are given a 2D arrayqueries, where queries[i] = [li, ri] represents a substrings[li...ri].
For each query, determine the maximum possible number of active sections in s after making the optimal trade on the substring s[li...ri].
Return an array answer, where answer[i] is the result for queries[i].
Note
For each query, treat s[li...ri] as if it is augmented with a '1' at both ends, forming t = '1' + s[li...ri] + '1'. The augmented '1's do not contribute to the final count.
The queries are independent of each other.
Example 1:
Input:s = "01", queries = [[0,1]]
Output:[1]
Explanation:
Because there is no block of '1's surrounded by '0's, no valid trade is possible. The maximum number of active sections is 1.
Query [0, 3] → Substring "0100" → Augmented to "101001" Choose "0100", convert "0100" → "0000" → "1111". The final string without augmentation is "1111". The maximum number of active sections is 4.
Query [0, 2] → Substring "010" → Augmented to "10101" Choose "010", convert "010" → "000" → "111". The final string without augmentation is "1110". The maximum number of active sections is 3.
Query [1, 3] → Substring "100" → Augmented to "11001" Because there is no block of '1's surrounded by '0's, no valid trade is possible. The maximum number of active sections is 1.
Query [2, 3] → Substring "00" → Augmented to "1001" Because there is no block of '1's surrounded by '0's, no valid trade is possible. The maximum number of active sections is 1.
Query [1, 5] → Substring "00010" → Augmented to "1000101" Choose "00010", convert "00010" → "00000" → "11111". The final string without augmentation is "1111110". The maximum number of active sections is 6.
Query [0, 6] → Substring "1000100" → Augmented to "110001001" Choose "000100", convert "000100" → "000000" → "111111". The final string without augmentation is "1111111". The maximum number of active sections is 7.
Query [0, 4] → Substring "10001" → Augmented to "1100011" Because there is no block of '1's surrounded by '0's, no valid trade is possible. The maximum number of active sections is 2.
Example 4:
Input:s = "01010", queries = [[0,3],[1,4],[1,3]]
Output:[4,4,2]
Explanation:
Query [0, 3] → Substring "0101" → Augmented to "101011" Choose "010", convert "010" → "000" → "111". The final string without augmentation is "11110". The maximum number of active sections is 4.
Query [1, 4] → Substring "1010" → Augmented to "110101" Choose "010", convert "010" → "000" → "111". The final string without augmentation is "01111". The maximum number of active sections is 4.
Query [1, 3] → Substring "101" → Augmented to "11011" Because there is no block of '1's surrounded by '0's, no valid trade is possible. The maximum number of active sections is 2.
Constraints:
1 <= n == s.length <= 105
1 <= queries.length <= 105
s[i] is either '0' or '1'.
queries[i] = [li, ri]
0 <= li <= ri < n
Solutions
Solution 1: Sparse Table
Thinking
Scanning every '0' run inside a query is \(\Theta(q \cdot n)\) in the worst case, and both \(n\) and \(q\) can be \(10^5\). A legal trade’s net gain is the sum of two '0' runs separated by '1's, so the answer is the global number of '1's plus the best gain inside the query.
Adjacent pairs that lie wholly inside the interval can be queried in \(O(1)\) with a sparse table on the sums of neighboring run lengths. Residual runs that straddle \(l\) or \(r\) pair with the next full run or with each other. After storing each run as \((\textit{start},\textit{len})\), each query needs only a constant number of comparisons.
A valid trade picks two consecutive '0' runs separated by '1's, flips the '1's to '0's, then flips the merged '0' run back to '1's. The net gain is the sum of the two '0' run lengths, and the original number of '1's stays the same. Thus the answer for each query is the total number of '1's in \(s\), plus the maximum gain obtainable inside that query range.
Query \([l, r]\) may only operate on \(s[l..r]\), treated as \(t = \texttt{'1'} + s[l..r] + \texttt{'1'}\). Therefore:
Any pair of adjacent '0' runs completely inside the range is a candidate, with gain equal to the sum of their lengths;
If \(s[l]\) or \(s[r]\) lies in the middle of a '0' run, the suffix / prefix of that run inside the query range can also be paired.
We record every '0' run as \((\textit{start}, \textit{len})\) and build a sparse table on the sums of adjacent runs. For each query:
Query the sparse table for the maximum adjacent-run sum fully inside the range;
Also consider the leftover left run with the next run, the leftover right run with the previous run, and the special case where the two leftovers enclose a single '1' run.
Add the best gain to the global '1' count. If no trade is possible, the gain is \(0\).
Time complexity is \(O(n \log n + q)\), and space complexity is \(O(n \log n)\), where \(n\) is the length of \(s\) and \(q\) is the number of queries.