CF1878D.Reverse Madness

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a string ss of length nn, containing lowercase Latin letters.

Next you will be given a positive integer kk and two arrays, ll and rr of length kk.

It is guaranteed that the following conditions hold for these 2 arrays:

  • l1=1l_1 = 1;
  • rk=nr_k = n;
  • li≤ril_i \le r_i, for each positive integer ii such that 1≤i≤k1 \le i \le k;
  • li=ri−1+1l_i = r_{i-1}+1, for each positive integer ii such that 2≤i≤k2 \le i \le k;

Now you will be given a positive integer qq which represents the number of modifications you need to do on ss.

Each modification is defined with one positive integer xx:

  • Find an index ii such that li≤x≤ril_i \le x \le r_i (notice that such ii is unique).
  • Let a=min⁡(x,ri+li−x)a=\min(x, r_i+l_i-x) and let b=max⁡(x,ri+li−x)b=\max(x, r_i+l_i-x).
  • Reverse the substring of ss from index aa to index bb.

Reversing the substring [a,b][a, b] of a string ss means to make ss equal to s1,s2,…,sa−1, sb,sb−1,…,sa+1,sa, sb+1,sb+2,…,sn−1,sns_1, s_2, \dots, s_{a-1},\ s_b, s_{b-1}, \dots, s_{a+1}, s_a,\ s_{b+1}, s_{b+2}, \dots, s_{n-1}, s_n.

Print ss after the last modification is finished.

给你一个长度为 nn 的字符串 ss,其中仅包含小写拉丁字母。

接下来,你会得到一个正整数 kk 和两个长度均为 kk 的数组 ll 和 rr。

保证这两个数组满足以下条件:

  • l1=1l_1 = 1;
  • rk=nr_k = n;
  • 对每个满足 1≤i≤k1 \le i \le k 的正整数 ii,有 li≤ril_i \le r_i;
  • 对每个满足 2≤i≤k2 \le i \le k 的正整数 ii,有 li=ri−1+1l_i = r_{i-1}+1;

随后,你将得到一个正整数 qq,表示你需要对 ss 执行的修改次数。

每次修改由一个正整数 xx 定义:

  • 找到唯一的下标 ii,使得 li≤x≤ril_i \le x \le r_i(注意这样的 ii 是唯一的);
  • 令 a=min⁡(x,ri+li−x)a=\min(x, r_i+l_i-x),令 b=max⁡(x,ri+li−x)b=\max(x, r_i+l_i-x);
  • 将字符串 ss 中从下标 aa 到下标 bb 的子串进行翻转。

对字符串 ss 的子串 [a,b][a, b] 进行翻转,是指将 ss 变为
s1,s2,…,sa−1, sb,sb−1,…,sa+1,sa, sb+1,sb+2,…,sn−1,sns_1, s_2, \dots, s_{a-1},\ s_b, s_{b-1}, \dots, s_{a+1}, s_a,\ s_{b+1}, s_{b+2}, \dots, s_{n-1}, s_n。

请输出在完成所有 qq 次修改后最终的字符串 ss。

输入格式

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

The first line of each test case contains two integers nn and kk (1≤k≤n≤2⋅1051 \le k \le n \le 2\cdot 10^5) — the length of the string ss, and the length of arrays ll and rr.

The second line of each test case contains the string ss ($ |s| = n$) containing lowercase Latin letters — the initial string.

The third line of each test case contains kk positive integers l1,l2,…,lkl_1, l_2, \dots, l_k (1≤li≤n1 \le l_i \le n) — the array ll.

The fourth line of each test case contains kk positive integers r1,r2,…,rkr_1, r_2, \dots, r_k (1≤ri≤n1 \le r_i \le n) — the array rr.

The fifth line of each test case contains a positive integer qq ($1 \le q \le 2 \cdot 10^5 $) — the number of modifications you need to do to ss.

The sixth line of each test case contains qq positive integers x1,x2,…,xqx_1, x_2, \dots, x_q (1≤xi≤n1\le x_i \le n) — the description of the modifications.

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

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

It is guaranteed that the conditions in the statement hold for the arrays ll and rr.

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

每个测试用例的第一行包含两个整数 nn 和 kk(1≤k≤n≤2⋅1051 \le k \le n \le 2\cdot 10^5),分别表示字符串 ss 的长度,以及数组 ll 和 rr 的长度。

每个测试用例的第二行包含字符串 ss(∣s∣=n|s| = n),由小写拉丁字母组成——即初始字符串。

每个测试用例的第三行包含 kk 个正整数 l1,l2,…,lkl_1, l_2, \dots, l_k(1≤li≤n1 \le l_i \le n)——即数组 ll。

每个测试用例的第四行包含 kk 个正整数 r1,r2,…,rkr_1, r_2, \dots, r_k(1≤ri≤n1 \le r_i \le n)——即数组 rr。

每个测试用例的第五行包含一个正整数 qq($1 \le q \le 2 \cdot 10^5 $)——表示需要对 ss 执行的修改次数。

每个测试用例的第六行包含 qq 个正整数 x1,x2,…,xqx_1, x_2, \dots, x_q(1≤xi≤n1\le x_i \le n)——表示每次修改的位置。

保证所有测试用例中 nn 的总和不超过 2⋅1052\cdot10^5。

保证所有测试用例中 qq 的总和不超过 2⋅1052\cdot10^5。

保证题目中所述条件对数组 ll 和 rr 均成立。

输出格式

For each test case, in a new line, output the string ss after the last modification is done.

对于每个测试用例,在新的一行中输出完成最后一次修改后的字符串 ss。

输入输出样例

  • 输入#1

    5
    4 2
    abcd
    1 3
    2 4
    2
    1 3
    5 3
    abcde
    1 2 3
    1 2 5
    3
    1 2 3
    3 1
    gaf
    1
    3
    2
    2 2
    10 1
    aghcdegdij
    1
    10
    5
    1 2 3 4 2
    1 1
    a
    1
    1
    1
    1

    输出#1

    badc
    abedc
    gaf
    jihgedcdga
    a

说明/提示

In the first test case:

The initial string is "abcd". In the first modification, we have x=1x=1. Since l1=1≤x≤r1=2l_1=1\leq x \leq r_1=2, we find the index i=1i = 1. We reverse the substring from index x=1x=1 to l1+r1−x=1+2−1=2l_1+r_1-x=1+2-1=2. After this modification, our string is "bacd".

In the second modification (and the last modification), we have x=3x=3. Since l2=3≤x≤r2=4l_2=3\leq x \leq r_2=4, we find the index i=2i = 2. We reverse the substring from index x=3x=3 to l2+r2−x=3+4−3=4l_2+r_2-x=3+4-3=4. After this modification, our string is "badc".

In the second test case:

The initial string is "abcde". In the first modification, we have x=1x=1. Since l1=1≤x≤r1=1l_1=1\leq x \leq r_1=1, we find the index i=1i = 1. We reverse the substring from index x=1x=1 to l1+r1−x=1+1−1=1l_1+r_1-x=1+1-1=1. After this modification, our string hasn't changed ("abcde").

In the second modification, we have x=2x=2. Since l2=2≤x≤r2=2l_2=2\leq x \leq r_2=2, we find the index i=2i = 2. We reverse the substring from index x=2x=2 to l2+r2−x=2+2−2=2l_2+r_2-x=2+2-2=2. After this modification, our string hasn't changed ("abcde").

In the third modification (and the last modification), we have x=3x=3. Since l3=3≤x≤r3=5l_3=3\leq x \leq r_3=5, we find the index i=3i = 3. We reverse the substring from index x=3x=3 to l3+r3−x=3+5−3=5l_3+r_3-x=3+5-3=5. After this modification, our string is "abedc".

在第一个测试用例中:

初始字符串为 "abcd"。在第一次修改中,x=1x=1。由于 l1=1≤x≤r1=2l_1=1\leq x \leq r_1=2,我们找到索引 i=1i = 1。我们将从索引 x=1x=1 到 l1+r1−x=1+2−1=2l_1+r_1-x=1+2-1=2 的子串进行翻转。此次修改后,字符串变为 "bacd"。

在第二次(也是最后一次)修改中,x=3x=3。由于 l2=3≤x≤r2=4l_2=3\leq x \leq r_2=4,我们找到索引 i=2i = 2。我们将从索引 x=3x=3 到 l2+r2−x=3+4−3=4l_2+r_2-x=3+4-3=4 的子串进行翻转。此次修改后,字符串变为 "badc"。

在第二个测试用例中:

初始字符串为 "abcde"。在第一次修改中,x=1x=1。由于 l1=1≤x≤r1=1l_1=1\leq x \leq r_1=1,我们找到索引 i=1i = 1。我们将从索引 x=1x=1 到 l1+r1−x=1+1−1=1l_1+r_1-x=1+1-1=1 的子串进行翻转。此次修改后,字符串未发生变化(仍为 "abcde")。

在第二次修改中,x=2x=2。由于 l2=2≤x≤r2=2l_2=2\leq x \leq r_2=2,我们找到索引 i=2i = 2。我们将从索引 x=2x=2 到 l2+r2−x=2+2−2=2l_2+r_2-x=2+2-2=2 的子串进行翻转。此次修改后,字符串未发生变化(仍为 "abcde")。

在第三次(也是最后一次)修改中,x=3x=3。由于 l3=3≤x≤r3=5l_3=3\leq x \leq r_3=5,我们找到索引 i=3i = 3。我们将从索引 x=3x=3 到 l3+r3−x=3+5−3=5l_3+r_3-x=3+5-3=5 的子串进行翻转。此次修改后,字符串变为 "abedc"。

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

首页