CF BUDDY
← Problems·

1978E · Computing Machine

2000 · brute force, data structures, dp

Problem: Sasha has two binary strings ss and tt of the same length nn, consisting of the characters 0 and 1.

There is also a computing machine that can perform two types of operations on binary strings aa and bb of the same length kk:

  1. If ai=ai+2=a_{i} = a_{i + 2} = 0, then you can assign bi+1:=b_{i + 1} := 1 (1ik21 \le i \le k - 2).
  2. If bi=bi+2=b_{i} = b_{i + 2} = 1, then you can assign ai+1:=a_{i + 1} := 1 (1ik21 \le i \le k - 2).

Sasha became interested in the following: if we consider the string a=slsl+1sra=s_ls_{l+1}\ldots s_r and the string b=tltl+1trb=t_lt_{l+1}\ldots t_r, what is the maximum number of 1 characters in the string aa that can be obtained using the computing machine. Since Sasha is very curious but lazy, it is up to you to answer this question for several pairs (li,ri)(l_i, r_i) that interest him.

Input Format: Each test consists of multiple test cases. The first line contains a single integer tt (1t1041 \le t \le 10^{4}) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5) — the length of the strings ss and tt.

The second line of each test case contains a binary string ss of length nn, consisting of the characters 0 and 1.

The third line of each test case contains a binary string tt of length nn, consisting of the characters 0 and 1.

The fourth line of each test case contains a single integer qq (1q21051 \le q \le 2 \cdot 10^5) — the number of queries.

The ii-th of the following lines contains two integers lil_{i} and rir_{i} (1lirin1 \le l_{i} \le r_{i} \le n) — the boundaries of the ii-th pair of substrings that interest Sasha.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5 and the sum of qq over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output qq integers — the answers to all queries.

Note: In the first test case:

  • In the first query, a=a = 11, so the maximum number of 1 characters is 22.
  • In the second query, a=a = 111, so the maximum number of 1 characters is 33.

In the second test case:

  • In the first query, a=a = 101 and b=b = 110. No operations can be performed, so the maximum number of 1 characters is 22.
  • In the second query, a=a = 1010 and b=b = 1101. Since a2=a4=a_2 = a_4 = 0, we can assign b3:=b_3 := 1. Now b1=b3=b_1 = b_3 = 1, so we can assign a2:=a_2 := 1. The string aa becomes 1110, so the maximum number of 1 characters is 33.

Sample Cases

Case 1

Input

3
4
1111
0000
2
1 2
2 4
4
1010
1101
2
1 3
1 4
6
010101
011010
5
2 3
1 6
2 5
4 4
3 6

Output

2
3
2
3
1
4
3
1
2

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback