CF1930D2.Sum over all Substrings (Hard Version)

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is the hard version of the problem. The only difference between the two versions is the constraint on tt and nn. You can make hacks only if both versions of the problem are solved.

For a binary†^\dagger pattern pp and a binary string qq, both of length mm, qq is called pp-good if for every ii (1≤i≤m1 \leq i \leq m), there exist indices ll and rr such that:

  • 1≤l≤i≤r≤m1 \leq l \leq i \leq r \leq m, and
  • pip_i is a mode‡^\ddagger of the string qlql+1…qrq_l q_{l+1} \ldots q_{r}.

For a pattern pp, let f(p)f(p) be the minimum possible number of 1\mathtt{1}s in a pp-good binary string (of the same length as the pattern).

You are given a binary string ss of size nn. Find $$\sum_{i=1}^{n} \sum_{j=i}^{n} f(s_i s_{i+1} \ldots s_j).$$ In other words, you need to sum the values of ff over all n(n+1)2\frac{n(n+1)}{2} substrings of ss.

†^\dagger A binary pattern is a string that only consists of characters 0\mathtt{0} and 1\mathtt{1}.

‡^\ddagger Character cc is a mode of string tt of length mm if the number of occurrences of cc in tt is at least ⌈m2⌉\lceil \frac{m}{2} \rceil. For example, 0\mathtt{0} is a mode of 010\mathtt{010}, 1\mathtt{1} is not a mode of 010\mathtt{010}, and both 0\mathtt{0} and 1\mathtt{1} are modes of 011010\mathtt{011010}.

这是该问题的困难版本。两个版本之间的唯一区别在于对 tt 和 nn 的约束条件。仅当该问题的两个版本均被解决时,才允许进行 hack。

对于一个长度为 mm 的二进制†^\dagger 模式 pp 和一个长度同样为 mm 的二进制字符串 qq,若对每个 ii(1≤i≤m1 \leq i \leq m),均存在下标 ll 和 rr,使得:

  • 1≤l≤i≤r≤m1 \leq l \leq i \leq r \leq m,且
  • pip_i 是子串 qlql+1…qrq_l q_{l+1} \ldots q_{r} 的一个众数‡^\ddagger,

则称 qq 是 pp-好(pp-good)的。

对一个模式 pp,定义 f(p)f(p) 为所有 pp-好二进制字符串(其长度与 pp 相同)中 1\mathtt{1} 的最少可能个数。

现给定一个长度为 nn 的二进制字符串 ss。请计算

∑i=1n∑j=inf(sisi+1…sj).\sum_{i=1}^{n} \sum_{j=i}^{n} f(s_i s_{i+1} \ldots s_j).

换言之,你需要对 ss 的全部 n(n+1)2\frac{n(n+1)}{2} 个子串,求其对应的 ff 值之和。

†^\dagger 二进制模式是指仅由字符 0\mathtt{0} 和 1\mathtt{1} 组成的字符串。

‡^\ddagger 字符 cc 是长度为 mm 的字符串 tt 的众数,当且仅当 cc 在 tt 中的出现次数至少为 ⌈m2⌉\lceil \frac{m}{2} \rceil。例如:0\mathtt{0} 是 010\mathtt{010} 的众数;1\mathtt{1} 不是 010\mathtt{010} 的众数;而 0\mathtt{0} 和 1\mathtt{1} 均为 011010\mathtt{011010} 的众数。

输入格式

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

The first line of each test case contains a single integer nn (1≤n≤1061 \le n \le 10^6) — the length of the binary string ss.

The second line of each test case contains a binary string ss of length nn consisting of only characters 0\mathtt{0} and 1\mathtt{1}.

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

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

每个测试用例的第一行包含一个整数 nn(1≤n≤1061 \le n \le 10^6)——即二进制字符串 ss 的长度。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss,该字符串仅由字符 0\mathtt{0} 和 1\mathtt{1} 组成。

保证所有测试用例的 nn 之和不超过 10610^6。

输出格式

For each test case, output the sum of values of ff over all substrings of ss.

对于每个测试用例,输出函数 ff 在字符串 ss 的所有子串上的取值之和。

输入输出样例

  • 输入#1

    4
    1
    1
    2
    10
    5
    00000
    20
    11110110000000111111

    输出#1

    1
    2
    0
    346

说明/提示

In the first test case, the only 1\mathtt{1}-good string is 1\mathtt{1}. Thus, f(1)=1f(\mathtt{1})=1.

In the second test case, f(10)=1f(\mathtt{10})=1 because 01\mathtt{01} is 10\mathtt{10}-good, and 00\mathtt{00} is not 10\mathtt{10}-good. Thus, the answer is f(1)+f(10)+f(0)=1+1+0=2f(\mathtt{1})+f(\mathtt{10})+f(\mathtt{0}) = 1 + 1 + 0 = 2.

In the third test case, ff equals to 00 for all 1≤i≤j≤51 \leq i \leq j \leq 5. Thus, the answer is 00.

在第一个测试用例中,唯一的 1\mathtt{1}-好字符串是 1\mathtt{1}。因此,f(1)=1f(\mathtt{1})=1。

在第二个测试用例中,f(10)=1f(\mathtt{10})=1,因为 01\mathtt{01} 是 10\mathtt{10}-好字符串,而 00\mathtt{00} 不是 10\mathtt{10}-好字符串。因此,答案为 f(1)+f(10)+f(0)=1+1+0=2f(\mathtt{1})+f(\mathtt{10})+f(\mathtt{0}) = 1 + 1 + 0 = 2。

在第三个测试用例中,对所有 1≤i≤j≤51 \leq i \leq j \leq 5,均有 f=0f=0。因此,答案为 00。

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

首页