#B4181. Interesting Subsequence

    ID: 11838 Type: Default 1000ms 256MiB

Interesting Subsequence

Interesting Subsequence

Given a binary string (S) of length (n) (indexed from 1) and (Q) queries, each query consists of two integers (l) and (r) representing a contiguous subinterval ([l,r]) of (S). For every query, you need to determine whether there exists an interesting subsequence of (S) that is identical to the subinterval (S[l, r]) but is not a contiguous segment of (S).

Formally, a subsequence is a sequence that can be derived from (S) by deleting some (or no) characters without changing the order, and an interval is a contiguous subsequence. An interesting subsequence is defined as one that satisfies the following conditions:

  1. The interesting subsequence is exactly equal (character by character, in order) to the subinterval (S[l, r]).
  2. This subsequence is not identical to the contiguous subsegment (S[l, r]) (i.e. it is obtained by selecting indices from both inside and outside ([l,r]) in such a way that the resulting sequence is the same, but the indices are not consecutive).

    A key observation is that if there exists an index (i (< l)) such that (S[i] = S[l]) or an index (j (> r)) such that (S[j] = S[r]), then one can form a subsequence that equals (S[l, r]) and is not contiguous. Otherwise, if neither condition holds, the answer is No.

inputFormat

The first line contains two integers (n) and (Q) ((1 \leq n, Q \leq 1000)) — the length of the binary string and the number of queries. The second line contains a binary string (S) of length (n) consisting of characters '0' and '1'. Each of the next (Q) lines contains two integers (l) and (r) ((1 \leq l \leq r \leq n)) representing a query.

outputFormat

For each query, print a single line containing Yes if there exists an interesting subsequence satisfying the conditions, or No otherwise.

sample

8 3
01011010
2 4
1 3
1 8
Yes

Yes No

</p>