Task
A flip operation on an array , consisting only of the digits and , is performed in the following two steps:
Step 1
: Choose two natural numbers and such that .Step 2
: For each position in the interval , if is , it changes its value to , otherwise, it changes its value to (from to and from to ).
Given , the array , and queries of the form :
For each given query, find the minimum number of flip operations needed, starting from the initial array, such that for every position in the interval , is equal to .
Solve this problem and we will reward you with points! (or, as much as you can...).
The operations performed for each query DO NOT carry over to subsequent queries (in other words, all queries start from the initial array).
Input data
The first line of the input file flip.in
will contain the number , representing the length of the array, followed by the array , , ..., on the second line. The third line of the file will contain the number , representing the number of queries. The next lines will contain two numbers and , which represent the endpoints of the interval for which we want to find the answer to the question in the statement.
Output data
The output file flip.out
will contain the answers to the queries on separate lines, in the order they were given.
Constraints and clarifications
- For each query,
# | Points | Constraints |
---|---|---|
1 | 9 | |
2 | 7 | For every position , , |
3 | 23 | |
4 | 21 | |
5 | 40 |
Example 1
flip.in
5
0 1 0 1 1
3
4 5
4 4
1 4
flip.out
1
1
2
Explanation
For the third query, we need to use the flip operation for the intervals and .
Example 2
flip.in
1
1
1
1 1
flip.out
1