#P11245. Good 01 Strings

    ID: 13316 Type: Default 1000ms 256MiB

Good 01 Strings

Good 01 Strings

We call a binary (0-1) string bad if and only if there exists an integer \( k \) in the set \( S \) such that the string contains a contiguous substring of length \( 2k \) that has exactly \( k \) zeros and \( k \) ones. Otherwise, the string is called good.

You are given \( q \) queries. In each query, you are provided four integers \( L, R, m, n \). Here, the set \( S = \{ x \in \mathbb{N}^{+} \mid L \le x \le R \} \) and you need to determine if there exists a good binary string that contains exactly \( m \) zeros and \( n \) ones.

A few observations help in solving the problem:

  • If one of \( m \) or \( n \) is zero, the string is composed of a single character and is trivially good.
  • If the total length \( M = m+n \) is less than \( 2L \), then it is impossible to have a substring of length \( 2k \) for any \( k \ge L \); hence the string is good.
  • If both \( m \) and \( n \) are positive and \( L = 1 \), then any binary string will contain a substring of length \( 2 \) (namely "01" or "10") that is balanced, so the string will be bad.
  • For \( L \ge 2 \) and \( M \ge 2L \), a necessary and sufficient condition for the existence of a good string is that \( \min(m,n) < L \). This is because when \( \min(m, n) \ge L \), one can always arrange the string (for example, by concatenating all 0's followed by all 1's) so that the substring crossing the boundary becomes balanced with a count \( \ge L \), making the string bad.

Thus, the answer for each query is:

if (m == 0 or n == 0):
    print("Yes")
else if (m+n < 2*L):
    print("Yes")
else if (L == 1):
    print("No")
else if (min(m, n) < L):
    print("Yes")
else:
    print("No")

inputFormat

The first line contains an integer \( q \) (\(1 \le q \le 10^5\)), the number of queries.

Each of the following \( q \) lines contains four space-separated integers \( L, R, m, n \) where \(1 \le L \le R \le 10^9\) and \(0 \le m, n \le 10^9\). It is guaranteed that \(m+n \ge 1\).

outputFormat

For each query, output a single line containing either Yes or No depending on whether a good binary string meeting the criteria exists.

sample

6
1 1 3 4
2 3 3 4
3 5 2 2
2 4 5 1
2 4 4 4
5 7 3 0
No

No Yes Yes No Yes

</p>