QOJ.ac

QOJ

時間限制: 1.5 s 記憶體限制: 1024 MB 總分: 100 难度: [顯示]

#15501. Shapez

统计

In the Shapez Factory, there is a tool called a "rotator." Using the rotator once allows you to cyclically shift a substring of length exactly 3 in a binary string, i.e., replacing $xyz$ with $yzx$ or $zxy$.

Given two binary strings $s$ and $t$ of length $n$, there are $q$ queries. Each query provides $l$ and $r$, and you must find the minimum number of times the rotator must be used to transform $s[l, r]$ into $t[l, r]$.

Input

Read the data from standard input.

The first line contains two positive integers $n$ and $q$, representing the length of the strings $s, t$ and the number of queries, respectively. The second line contains a binary string $s$ of length $n$. The third line contains a binary string $t$ of length $n$. The $i$-th line of the next $q$ lines ($1 \le i \le q$) contains two positive integers $l$ and $r$, representing the $i$-th query.

Output

Output to standard output.

For each query, output a single integer representing the minimum number of rotator operations required. Specifically, if it is impossible to transform $s[l, r]$ into $t[l, r]$ under any circumstances, output $-1$.

Examples

Input 1

10 5
1010111000
1111000001
1 6
3 5
4 5
1 10
8 9

Output 1

3
1
-1
5
0

Note

For the first query, one possible sequence of operations is: 1. Select substring $[4, 6]$, replace $011$ with $110$, resulting in $101110$; 2. Select substring $[2, 4]$, replace $011$ with $110$, resulting in $111010$; 3. Select substring $[4, 6]$, replace $010$ with $100$, resulting in $111100$.

Subtasks

For all test data, it is guaranteed that: $1 \le n, q \le 5 \times 10^5$; For all $1 \le i \le n$, $s_i, t_i \in \{0, 1\}$; * $1 \le l \le r \le n$.

Subtask ID Score $n, q \le$ Special Property
1 10 10 None
2 10 $2 \times 10^3$ A
3 25 $2 \times 10^3$ None
4 20 $2 \times 10^5$ None
5 10 $5 \times 10^5$ A
6 25 $5 \times 10^5$ None

Special Property A: For all $1 \le i \le \lfloor \frac{n+1}{2} \rfloor$, $s_{2i-1} = t_{2i-1} = 0$.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download
#102EditorialOpen#15501 异形工厂 线性做法 题解tigerchen2025-12-12 19:34:18View

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.