Tap here to switch tabs
Problem
Submissions

Problem: Maximize Active Section with Trade II

Try to solve the Maximize Active Section with Trade II problem.

Statement

You are given a binary string s of length n and a 2D\text{2D} integer array queries where each queries[i] = [l_i, r_i] represents a query on the substring spanning from index l_i to index r_i inclusive.

An active section in s is defined as any character equal to '1'.

For each query queries[i] = [l_i, r_i], you can perform at most one trade operation within the substring from index l_i to r_i. A trade operation consists of the following steps:

  1. Select a contiguous sequence of '1's strictly inside the range [l_i, r_i] that is surrounded by '0's on both sides within the range. That is, the sequence cannot start at index l_i and cannot end at index r_i.

  2. Change all characters in this selected sequence of '1's to '0's.

  3. Select any contiguous sequence of '0's located within [l_i, r_i] and change all of its characters to '1's.

Each query is evaluated independently without permanently modifying s.

Return an array of integers where the ithi^{th} element is the maximum number of active sections in s after applying at most one valid trade operation for queries[i].

Constraints:

  • 1≤n=s.length≤1051 \leq n = \text{s.length} \leq 10^5

  • 1≤queries.length≤1051 \leq \text{queries.length} \leq 10^5

  • s[i] is either '0' or '1'.

  • queries[i] = [l_i, r_i]

  • 0≤li≤ri<n0 \leq l_i \leq r_i < n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximize Active Section with Trade II

Try to solve the Maximize Active Section with Trade II problem.

Statement

You are given a binary string s of length n and a 2D\text{2D} integer array queries where each queries[i] = [l_i, r_i] represents a query on the substring spanning from index l_i to index r_i inclusive.

An active section in s is defined as any character equal to '1'.

For each query queries[i] = [l_i, r_i], you can perform at most one trade operation within the substring from index l_i to r_i. A trade operation consists of the following steps:

  1. Select a contiguous sequence of '1's strictly inside the range [l_i, r_i] that is surrounded by '0's on both sides within the range. That is, the sequence cannot start at index l_i and cannot end at index r_i.

  2. Change all characters in this selected sequence of '1's to '0's.

  3. Select any contiguous sequence of '0's located within [l_i, r_i] and change all of its characters to '1's.

Each query is evaluated independently without permanently modifying s.

Return an array of integers where the ithi^{th} element is the maximum number of active sections in s after applying at most one valid trade operation for queries[i].

Constraints:

  • 1≤n=s.length≤1051 \leq n = \text{s.length} \leq 10^5

  • 1≤queries.length≤1051 \leq \text{queries.length} \leq 10^5

  • s[i] is either '0' or '1'.

  • queries[i] = [l_i, r_i]

  • 0≤li≤ri<n0 \leq l_i \leq r_i < n