#P10274. Boolean Expression Fixing
Boolean Expression Fixing
Boolean Expression Fixing
Farmer John has a Boolean expression that consists of N tokens where N is odd (1 ≤ N < 2×105). The tokens in odd positions (1-indexed) are Boolean values (true
or false
), and the tokens in even positions are operators (and
or or
). The expression is evaluated using the operator precedence as in C++: and
has higher precedence than or
. In other words, in each evaluation step:
- If there is any occurrence of
and
in the expression, one may choose any occurrence and replace the surrounding phrasex and y
(wherex
andy
are eithertrue
orfalse
) with its result (true
if both aretrue
,false
otherwise). - If no
and
exists, then an occurrence ofor
is chosen and similarly its phrase is replaced with its result (true
if at least one operand istrue
,false
otherwise).
It can be proven that regardless of the order in which valid choices are made, the expression eventually evaluates to the same result.
Farmer John now faces Q queries. In each query he specifies two odd integers l and r (with 1 ≤ l ≤ r ≤ N), and he removes the contiguous segment of tokens from the l-th to the r-th token (this segment always starts and ends with a Boolean value, and the operators between them are also removed). He then wishes to replace the removed segment with a single Boolean literal (either true
or false
) such that when the modified expression is re‐evaluated (with the remaining tokens kept in their original order), the final result equals a specified target Boolean value. Your task is to help determine, for each query, whether it is possible to choose such a replacement.
inputFormat
The first line contains two integers N and Q.
The second line contains N tokens separated by a space. Tokens in odd positions (1-indexed) are either true
or false
and tokens in even positions are either and
or or
.
Each of the next Q lines contains two integers l and r (both odd) followed by a target Boolean value (true
or false
).
outputFormat
For each query, output a single line with yes
if it is possible to choose a replacement Boolean literal (true
or false
) so that the entire expression evaluates to the target; otherwise, output no
.
sample
7 3
true and false or true and false
3 5 true
1 3 false
5 7 true
yes
no
yes
</p>