#7450. [SWERC 2025] Jewels Building

[SWERC 2025] Jewels Building

[SWERC 2025] Jewels Building

题目描述

You are playing a fantasy game where you start with a row of $n$ power crystals. The $i$-th crystal has energy level $a_i$.

You can perform the following operation any number of times:

  • choose a consecutive group of identical crystals, that is, choose ll and rr (1≤l≤r≤∣a∣1 \le l \le r \le |a|) such that al=al+1=…=ara_l = a_{l+1} = \ldots = a_r;
  • fuse them into a single crystal whose energy becomes r−l+1r - l + 1, obtaining the new sequence $[a_1, \ldots, a_{l-1}, r - l + 1, a_{r+1}, \ldots, a_{|a|}]$.

Note that you may also choose l=rl = r.

You want to craft a specific configuration of crystals with energy levels b1,…,bmb_1, \ldots, b_m. Determine whether it is possible.

输入格式

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.

The first line of each test case contains two integers n,mn, m (1≤m≤n≤40001 \le m \le n \le 4000) — the number of crystals in the initial and target configurations, respectively.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the energy levels of the initial crystals.

The third line of each test case contains mm integers b1,b2,…,bmb_1, b_2, \ldots, b_m (1≤bi≤1091 \le b_i \le 10^9) — the desired energy levels of the target crystals.

It is guaranteed that the sum of nn over all test cases does not exceed 40004000.

输出格式

For each test case, output **YES** if you can transform the initial configuration into the target one, and **NO** otherwise.

The judge is case-insensitive (for example, YES, Yes, yes, yEs will all be recognized as positive answers).

3
5 1
2 4 4 2 3
2
5 2
2 4 4 2 3
4 4
1 1
2
1
YES
NO
YES

提示

#### Explanation of sample 1.

In the first test case:

  • the initial configuration is [2,4,4,2,3][2, 4, 4, 2, 3];
  • after fusing the two crystals in the subarray [l,r]=[2,3][l, r] = [2, 3], the configuration becomes [2,2,2,3][2, 2, 2, 3];
  • after fusing crystals in the subarray [l,r]=[1,3][l, r] = [1, 3], the configuration becomes [3,3][3, 3];
  • after fusing crystals in the subarray [l,r]=[1,2][l, r] = [1, 2], the configuration becomes [2]=[b1][2] = [b_1]. So the answer is YES.

In the second test case, it is not possible to obtain [4,4][4, 4] starting from [2,4,4,2,3][2, 4, 4, 2, 3], so the answer is NO. 标签: P15144|2025|Special Judge|ICPC

来源

P15144|[SWERC 2025] Jewels Building