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 t and n. You can make hacks only if both versions of the problem are solved.
For a binary† pattern p and a binary string q, both of length m, q is called p-good if for every i (1≤i≤m), there exist indices l and r such that:
- 1≤l≤i≤r≤m, and
- pi is a mode‡ of the string qlql+1…qr.
For a pattern p, let f(p) be the minimum possible number of 1s in a p-good binary string (of the same length as the pattern).
You are given a binary string s of size n. 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 f over all 2n(n+1) substrings of s.
† A binary pattern is a string that only consists of characters 0 and 1.
‡ Character c is a mode of string t of length m if the number of occurrences of c in t is at least ⌈2m⌉. For example, 0 is a mode of 010, 1 is not a mode of 010, and both 0 and 1 are modes of 011010.
这是该问题的困难版本。两个版本之间的唯一区别在于对 t 和 n 的约束条件。仅当该问题的两个版本均被解决时,才允许进行 hack。
对于一个长度为 m 的二进制† 模式 p 和一个长度同样为 m 的二进制字符串 q,若对每个 i(1≤i≤m),均存在下标 l 和 r,使得:
- 1≤l≤i≤r≤m,且
- pi 是子串 qlql+1…qr 的一个众数‡,
则称 q 是 p-好(p-good)的。
对一个模式 p,定义 f(p) 为所有 p-好二进制字符串(其长度与 p 相同)中 1 的最少可能个数。
现给定一个长度为 n 的二进制字符串 s。请计算
i=1∑nj=i∑nf(sisi+1…sj).
换言之,你需要对 s 的全部 2n(n+1) 个子串,求其对应的 f 值之和。
† 二进制模式是指仅由字符 0 和 1 组成的字符串。
‡ 字符 c 是长度为 m 的字符串 t 的众数,当且仅当 c 在 t 中的出现次数至少为 ⌈2m⌉。例如:0 是 010 的众数;1 不是 010 的众数;而 0 和 1 均为 011010 的众数。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤105) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤106) — the length of the binary string s.
The second line of each test case contains a binary string s of length n consisting of only characters 0 and 1.
It is guaranteed that the sum of n over all test cases does not exceed 106.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤105)——即测试用例的数目。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤106)——即二进制字符串 s 的长度。
每个测试用例的第二行包含一个长度为 n 的二进制字符串 s,该字符串仅由字符 0 和 1 组成。
保证所有测试用例的 n 之和不超过 106。
输出格式
For each test case, output the sum of values of f over all substrings of s.
对于每个测试用例,输出函数 f 在字符串 s 的所有子串上的取值之和。
输入输出样例
输入#1
4 1 1 2 10 5 00000 20 11110110000000111111
输出#1
1 2 0 346
说明/提示
In the first test case, the only 1-good string is 1. Thus, f(1)=1.
In the second test case, f(10)=1 because 01 is 10-good, and 00 is not 10-good. Thus, the answer is f(1)+f(10)+f(0)=1+1+0=2.
In the third test case, f equals to 0 for all 1≤i≤j≤5. Thus, the answer is 0.
在第一个测试用例中,唯一的 1-好字符串是 1。因此,f(1)=1。
在第二个测试用例中,f(10)=1,因为 01 是 10-好字符串,而 00 不是 10-好字符串。因此,答案为 f(1)+f(10)+f(0)=1+1+0=2。
在第三个测试用例中,对所有 1≤i≤j≤5,均有 f=0。因此,答案为 0。
输入解题思路,AI测评打分。不知道怎么写?