Linearization

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

Bitwise "and" of two non-negative integers is calculated as follows: write both numbers in binary, then the ii-th binary digit of the result is equal to 11 if both arguments have the ii-th digit equal to 11. For example, (14 and 7)=(1110_2 and 0111_2)=110_2=6(14 \text{ and } 7) = (1110\_2 \text{ and } 0111\_2) = 110\_2 = 6.

"Exclusive or" (xor) of two binary digits equals 11 if they are unequal, and 00 if they are equal. Thus, 0 xor 0=00 \text{ xor } 0 = 0, 0 xor 1=10 \text{ xor } 1 = 1, 1 xor 0=11 \text{ xor } 0 = 1 and 1 xor 1=01 \text{ xor } 1 = 0.

Parity function P(x)P(x) for a non-negative integer xx equals 11 if the binary notation of xx has odd number of ones, and 00 if the binary notation of xx has even number of ones. For example, P(5)=P(101_2)=0P(5) = P(101\_2) = 0, P(7)=P(111_2)=1P(7) = P(111\_2) = 1.

Consider a binary string whose length is a power of two: s=s_0s_1s_n1s = s\_0s\_1\ldots s\_{n-1}, where n=2kn = 2^k. We will call this string linear, if there is an integer xx, 0x<n0 \le x < n, and a binary digit bb, such that for all ii from 00 to n1n-1 holds s_i=P(i and x) xor bs\_i = P(i \text{ and } x) \text{ xor } b.

For example, a string "1100" is linear: take x=2=10_2x = 2 = 10\_2 and b=1b = 1.

  • s_0=P(0 and 2) xor 1=P(0) xor 1=0 xor 1=1s\_0 = P(0 \text{ and } 2) \text{ xor } 1 = P(0) \text{ xor } 1 = 0 \text{ xor } 1 = 1
  • s_1=P(1 and 2) xor 1=P(0) xor 1=0 xor 1=1s\_1 = P(1 \text{ and } 2) \text{ xor } 1 = P(0) \text{ xor } 1 = 0 \text{ xor } 1 = 1
  • s_2=P(2 and 2) xor 1=P(2) xor 1=1 xor 1=0s\_2 = P(2 \text{ and } 2) \text{ xor } 1 = P(2) \text{ xor } 1 = 1 \text{ xor } 1 = 0
  • s_3=P(3 and 2) xor 1=P(2) xor 1=1 xor 1=0s\_3 = P(3 \text{ and } 2) \text{ xor } 1 = P(2) \text{ xor } 1 = 1 \text{ xor } 1 = 0

Meanwhile, "0001" is not linear: whatever xx we chose, we would have P(0 and x)=P(0)=0P(0 \text{ and } x) = P(0) = 0, therefore b=0b = 0. We have 0=P(1 and x)0 = P(1 \text{ and } x) and 0=P(2 and x)0 = P(2 \text{ and } x), therefore x=0x = 0. But P(3 and 0)=0s_3=1P(3 \text{ and } 0) = 0 \ne s\_3 = 1.

Consider a binary string. In one action you can take a continuous segment of digits and invert them: change all zeros to ones and vice versa. Call hardness of linearization of this string the minimal number of actions one needs to make it linear.

For example, the hardness of linearization for the string "0001" is 11: you can invert the left three digits to get the string "1111" which is linear with x=0x = 0, b=1b = 1. There are other ways to linearize it in one action.

You are given a string tt and qq queries (l_i,r_i)(l\_i, r\_i). For each query, consider a substring of tt from l_il\_i-th digit to r_ir\_i-th digit, inclusive. Digits of tt are numbered from left to right, starting with 00. It is guaranteed that the length of each query is a power of two. Calculate the hardness of linearization for every given substring.

입력

The first line of input contains a single integer mm --- the length of the string tt (1m200,000)1 \le m \le 200\\,000). The second line contains a binary string tt of length mm.

The next line contains integer qq --- the number of queries (1q200,0001 \le q \le 200\\,000). Each of the next qq lines contains two integers, l_il\_i and r_ir\_i (0l_ir_i<m0 \le l\_i \le r\_i < m, r_il_i+12r\_i - l\_i + 1 \ge 2, substring length is a power of two).

출력

For each query, print one integer: the hardness of linearization of the corresponding substring of tt.

힌트

In the first query we need to linearize the whole string. This can be done, for example, by inverting the segment from 44-th to 66-th digit, getting the string "00001011", and then inverting the 55-th digit, getting "00001111" which is linear with  x=4x = 4 and b=0b = 0.

In the second query, the string "0001" can be linearized in one action, as described in the problem statement.

In the third query the string "0000" is already linear with x=0x = 0, b=0b = 0.