CF2248D.Good Pair Queries

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given two binary strings ss and tt, both of length nn.

For two binary strings aa and bb of the same length, the pair (a,b)(a, b) is called good if both strings can be made empty by performing the following operation zero or more times:

  • Choose a non-empty set of positions 1≤i1<i2<…<ik≤∣a∣1 \le i_1 \lt i_2 \lt \ldots \lt i_k \le |a| and a character c∈0,1c \in {\mathtt{0}, \mathtt{1}}.
  • Let x=ai1ai2…aikx = a_{i_1}a_{i_2}\ldots a_{i_k} and y=bi1bi2…biky = b_{i_1}b_{i_2}\ldots b_{i_k}.
  • The character cc must be a mode∗^{\text{∗}} of both xx and yy.
  • Delete the characters at the chosen positions from both strings. The remaining characters are concatenated without changing their relative order.

A binary string may have both 0\mathtt{0} and 1\mathtt{1} as modes.

You need to answer qq queries. In each query, you are given two integers ll and rr. Determine whether the pair of substrings (slsl+1…sr,tltl+1…tr)(s_l s_{l+1} \ldots s_r, t_l t_{l+1} \ldots t_r) is good.

The queries are independent.

∗^{\text{∗}}For a binary string zz, a character cc is a mode if it appears at least ⌈∣z∣2⌉\left\lceil \frac{|z|}{2} \right\rceil times in zz. Here, ⌈x⌉\lceil x \rceil denotes the smallest integer greater than or equal to xx.

给你两个长度均为 nn 的二进制字符串 ss 和 tt。

对于两个等长的二进制字符串 aa 和 bb,若可通过执行以下操作零次或多次使两个字符串均变为空,则称数对 (a,b)(a, b) 是好的:

  • 选择一个非空的位置集合 1≤i1<i2<…<ik≤∣a∣1 \le i_1 \lt i_2 \lt \ldots \lt i_k \le |a| 以及一个字符 c∈0,1c \in {\mathtt{0}, \mathtt{1}};
  • 令 x=ai1ai2…aikx = a_{i_1}a_{i_2}\ldots a_{i_k},y=bi1bi2…biky = b_{i_1}b_{i_2}\ldots b_{i_k};
  • 字符 cc 必须同时是 xx 和 yy 的众数∗^{\text{∗}};
  • 从两个字符串中同时删除所选位置上的字符;剩余字符保持原有相对顺序拼接。

一个二进制字符串可能同时以 0\mathtt{0} 和 1\mathtt{1} 为众数。

你需要回答 qq 个查询。每次查询给出两个整数 ll 和 rr,判断子串对 (slsl+1…sr,  tltl+1…tr)(s_l s_{l+1} \ldots s_r,\; t_l t_{l+1} \ldots t_r) 是否为好的。

各查询相互独立。

∗^{\text{∗}} 对于二进制字符串 zz,字符 cc 是其众数,当且仅当 cc 在 zz 中出现次数至少为 ⌈∣z∣2⌉\left\lceil \frac{|z|}{2} \right\rceil。其中 ⌈x⌉\lceil x \rceil 表示不小于 xx 的最小整数。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and qq (1≤n,q≤2⋅1051 \le n, q \le 2 \cdot 10^5) — the length of each string and the number of queries.

The second line of each test case contains the binary string ss of length nn.

The third line of each test case contains the binary string tt of length nn.

Each of the next qq lines contains two integers ll and rr (1≤l≤r≤n1 \le l \le r \le n) — a query.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

It is guaranteed that the sum of qq over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 qq(1≤n,q≤2⋅1051 \le n, q \le 2 \cdot 10^5)—— 分别表示每个字符串的长度以及查询次数。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss。

每个测试用例的第三行包含一个长度为 nn 的二进制字符串 tt。

接下来的 qq 行中,每行包含两个整数 ll 和 rr(1≤l≤r≤n1 \le l \le r \le n)—— 表示一次查询。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

保证所有测试用例的 qq 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each query, output "YES" if the pair (slsl+1…sr,tltl+1…tr)(s_l s_{l+1} \ldots s_r, t_l t_{l+1} \ldots t_r) is good. Otherwise, output "NO".

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

对于每个查询,若数对 (slsl+1…sr,tltl+1…tr)(s_l s_{l+1} \ldots s_r, t_l t_{l+1} \ldots t_r) 是“好”的,则输出 “YES”;否则输出 “NO”。

你可以以任意大小写形式输出答案(大写或小写均可)。例如,字符串 “yEs”、“yes”、“Yes” 和 “YES” 均会被识别为肯定回答。

输入输出样例

  • 输入#1

    3
    2 2
    01
    10
    1 1
    1 2
    5 1
    11111
    11111
    1 3
    5 1
    00011
    01111
    1 5

    输出#1

    NO
    YES
    YES
    YES

说明/提示

In the first test case, for the first query, the pair is (0,1)(\mathtt{0}, \mathtt{1}). No character is a mode of both strings, so no operation can be performed.

For the second query, choose both positions and c=0c = \mathtt{0}. Then x=01x = \mathtt{01} and y=10y = \mathtt{10}, so cc is a mode of both strings and the whole pair is deleted.

In the third test case, neither character is a mode of both 00011\mathtt{00011} and 01111\mathtt{01111}, so the whole pair cannot be deleted in one operation. It can be emptied in three operations:

  • Choose positions 22 and 44 and c=1c = \mathtt{1}. Then x=01x = \mathtt{01} and y=11y = \mathtt{11}, so cc is a mode of both xx and yy. After deleting the selected positions, the remaining characters are concatenated, giving the pair (001,011)(\mathtt{001}, \mathtt{011}).
  • Choose positions 11 and 22 and c=0c = \mathtt{0}. Then x=00x = \mathtt{00} and y=01y = \mathtt{01}, so cc is a mode of both xx and yy. After deleting the selected positions, the pair becomes (1,1)(\mathtt{1}, \mathtt{1}).
  • Choose the remaining position 11 and c=1c = \mathtt{1}. Then x=1x = \mathtt{1} and y=1y = \mathtt{1}, so cc is a mode of both xx and yy. Deleting the selected position makes both strings empty.

在第一个测试用例中,对于第一个查询,该数对为 (0,1)(\mathtt{0}, \mathtt{1})。没有任何字符同时是两个字符串的众数,因此无法执行任何操作。

对于第二个查询,选择两个位置,并令 c=0c = \mathtt{0}。此时 x=01x = \mathtt{01},y=10y = \mathtt{10},因此 cc 是两个字符串的众数,整个数对被删除。

在第三个测试用例中,00011\mathtt{00011} 和 01111\mathtt{01111} 中没有任何一个字符同时是这两个字符串的众数,因此整个数对无法通过一次操作删除。但可以通过三次操作将其清空:

  • 选择位置 22 和 44,并令 c=1c = \mathtt{1}。此时 x=01x = \mathtt{01},y=11y = \mathtt{11},因此 cc 是 xx 和 yy 的众数。删除所选位置后,剩余字符被连接,得到新数对 (001,011)(\mathtt{001}, \mathtt{011})。
  • 选择位置 11 和 22,并令 c=0c = \mathtt{0}。此时 x=00x = \mathtt{00},y=01y = \mathtt{01},因此 cc 是 xx 和 yy 的众数。删除所选位置后,数对变为 (1,1)(\mathtt{1}, \mathtt{1})。
  • 选择剩余的位置 11,并令 c=1c = \mathtt{1}。此时 x=1x = \mathtt{1},y=1y = \mathtt{1},因此 cc 是 xx 和 yy 的众数。删除所选位置后,两个字符串均变为空。

输入解题思路,AI测评打分。不知道怎么写?

首页