CF2267E.Clean Substrings

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A binary string tt of length mm is called clean if ti=ti+1t_i = t_{i+1} for all 1≤i<m1\le i\lt m.

The chief engineer of the country has made another discovery. He invented a smart robot that can perform the following operation on any binary string tt for one coin:

  • Choose any clean substring∗^{\text{∗}} of the string tt.
  • Invert all elements of the substring (change 00 to 11 and vice versa).

Define the beauty of a string tt as the minimum number of coins needed to make it clean. Define the power of a string tt as the sum of the beauties of all its substrings.

You are given a binary string ss of length nn. The engineer's competitors are going to modify the string exactly qq times. Each modification is described by one number ii. After the modification, sis_i is inverted. Your task is to compute the power of the string ss before and after each modification.

∗^{\text{∗}}A string aa is a substring of a string bb if aa can be obtained from bb by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

长度为 mm 的二进制字符串 tt 被称为洁净的,当且仅当对所有 1≤i<m1 \le i < m,均有 ti=ti+1t_i = t_{i+1}。

该国总工程师又有一项新发现:他发明了一种智能机器人,该机器人可以对任意二进制字符串 tt 执行如下操作(每次消耗一枚硬币):

  • 任选 tt 的一个洁净子串∗^{\text{∗}};
  • 将该子串中所有元素取反(即 00 变为 11,11 变为 00)。

定义字符串 tt 的美观度为使其变为洁净字符串所需的最少硬币数。定义字符串 tt 的威力为 tt 的所有子串的美观度之和。

现给定一个长度为 nn 的二进制字符串 ss。总工程师的竞争对手将恰好对字符串执行 qq 次修改。每次修改由一个整数 ii 描述:修改后,sis_i 被取反。你的任务是在每次修改之前及之后,分别计算字符串 ss 的威力。

∗^{\text{∗}} 字符串 aa 是字符串 bb 的子串,当且仅当 aa 可通过从 bb 的开头删除若干(可能为零或全部)字符、并从 bb 的末尾删除若干(可能为零或全部)字符而得到。

输入格式

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 the binary string and the number of modifications.

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

The next qq lines of each test case contain one integer ii (1≤i≤n1\le i\le n) — the description of the modifications.

It is guaranteed that the sum of all nn values and the sum of all qq values over all test cases do 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)—— 分别表示二进制字符串的长度和修改操作的次数。

每个测试用例的第二行包含二进制字符串 ss。

每个测试用例接下来的 qq 行,每行包含一个整数 ii(1≤i≤n1\le i\le n)—— 表示一次修改操作的位置。

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

输出格式

For each test case, output q+1q + 1 integers separated by spaces — the answer before the modifications and after each modification of the string.

对于每个测试用例,输出 q+1q + 1 个整数(以空格分隔)——即字符串修改前的答案,以及每次字符串修改后的答案。

输入输出样例

  • 输入#1

    4
    3 1
    110
    2
    4 2
    1010
    1
    2
    8 4
    10101110
    3
    5
    2
    3
    10 5
    1100101110
    1
    6
    10
    7
    3

    输出#1

    2 2 
    7 5 5 
    36 23 22 25 26 
    61 66 41 35 59 60

说明/提示

Consider the first test case.

Before the modifications, the string ss is 110. The power of the string is 22:

  • The beauty of substring s[1,1]s[1, 1] is 0.
  • The beauty of substring s[2,2]s[2, 2] is 0.
  • The beauty of substring s[3,3]s[3, 3] is 0.
  • The beauty of substring s[1,2]s[1, 2] is 0.
  • The beauty of substring s[2,3]s[2, 3] is 1.
  • The beauty of substring s[1,3]s[1, 3] is 1.

After the modification, the string ss is 100. Now the power is 22:

  • The beauty of substring s[1,1]s[1, 1] is 0.
  • The beauty of substring s[2,2]s[2, 2] is 0.
  • The beauty of substring s[3,3]s[3, 3] is 0.
  • The beauty of substring s[1,2]s[1, 2] is 1.
  • The beauty of substring s[2,3]s[2, 3] is 0.
  • The beauty of substring s[1,3]s[1, 3] is 1.

考虑第一个测试用例。

修改前,字符串 ss 为 110。该字符串的“能量”为 22:

  • 子串 s[1,1]s[1, 1] 的“优美度”为 0。
  • 子串 s[2,2]s[2, 2] 的“优美度”为 0。
  • 子串 s[3,3]s[3, 3] 的“优美度”为 0。
  • 子串 s[1,2]s[1, 2] 的“优美度”为 0。
  • 子串 s[2,3]s[2, 3] 的“优美度”为 1。
  • 子串 s[1,3]s[1, 3] 的“优美度”为 1。

修改后,字符串 ss 变为 100。此时“能量”仍为 22:

  • 子串 s[1,1]s[1, 1] 的“优美度”为 0。
  • 子串 s[2,2]s[2, 2] 的“优美度”为 0。
  • 子串 s[3,3]s[3, 3] 的“优美度”为 0。
  • 子串 s[1,2]s[1, 2] 的“优美度”为 1。
  • 子串 s[2,3]s[2, 3] 的“优美度”为 0。
  • 子串 s[1,3]s[1, 3] 的“优美度”为 1。

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

首页