Bay and black horses
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
At Zenik and Marichka’s horse farm, there are horses of two colors: bay and black.
Initially, Zenik lines up \(n\) horses in a row. Each horse in the row has one of the two colors: bay or black. Then Zenik leaves the horses from the \(l\)-th to the \(r\)-th inclusive in the row, and takes all the others to the pasture.
After this, Marichka performs an arbitrary (possibly zero) number of the following operations: she chooses two different horses in the row of the same color, and takes them and all the horses standing between them to the pasture.
Your task is to answer \(q\) queries. For each query, two numbers \(l\) and \(r\) are given (\(1 \le l \le r \le n\)). You need to tell whether Marichka can take all the horses to the pasture if Zenik leaves in the row only the horses from the \(l\)-th to the \(r\)-th inclusive.
Input
The first line contains \(n\) — the number of horses in the row.
The second line contains a string of \(n\) digits 4 and
7. The digit 4 denotes a bay horse, and
7 denotes a black horse.
The third line contains an integer \(q\) — the number of queries.
The next \(q\) lines contain two integers \(l\) and \(r\) each — the queries to be answered.
Output
For each query, print one word on a separate line — Yes,
if Marichka can take all the horses to the pasture, and No,
otherwise.
Constraints
\(1 \le n \le 10^6\),
\(1 \le q \le 10^5\),
\(1 \le l \le r \le n\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 7 4477447 5 2 3 1 6 3 6 1 7 4 4 | No Yes Yes Yes No |
Notes
If Zenik leaves the second and third horses in the row, the row will look like
47. Marichka will not be able to perform any operation with this row, so the answer here isNo.If Zenik leaves the horses from the first to the sixth in the row, the row will look like
447744. Marichka will choose the two outermost bay horses and take all the horses to the pasture in one operation — the answer here isYes.If Zenik leaves Marichka the row
7744, Marichka can take the horses to the pasture in two operations: first the two black ones, and then the two bay ones.For the row
4477447, there is also a sequence of two operations to take all the horses to the pasture.When only one horse remains in the row, Marichka cannot take it to the pasture.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|