CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!)
H
BinaryStringForces
Codeforces
3500
原题
← G→
翻译中…

You are given a binary string $s$ of length $n$. We define a maximal substring as a substring that cannot be extended while keeping all elements equal. For example, in the string $11000111$ there are three maximal substrings: $11$, $000$ and $111$.

翻译中…

In one operation, you can select two maximal adjacent substrings. Since they are maximal and adjacent, it's easy to see their elements must have different values. Let $a$ be the length of the sequence of ones and $b$ be the length of the sequence of zeros. Then do the following:

翻译中…
  • If $a \ge b$, then replace $b$ selected zeros with $b$ ones.
  • If $a \lt b$, then replace $a$ selected ones with $a$ zeros.
翻译中…

As an example, for $1110000$ we make it $0000000$, for $0011$ we make it $1111$. We call a string being good if it can be turned into $1111...1111$ using the aforementioned operation any number of times (possibly, zero). Find the number of good substrings among all $\frac{n(n+1)}{2}$ non-empty substrings of $s$.

翻译中…

Input

翻译中…

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The description of test cases follows.

翻译中…

The first line of each test case contains $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$.

翻译中…

The second line of each test case contains the binary string $s$ of length $n$.

翻译中…

It is guaranteed that sum of $n$ across all test cases doesn't exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, print a single integer — the number of good substrings.

翻译中…

Note

翻译中…

Let's define a substring from index $l$ to index $r$ as $[l, r]$.

翻译中…

For the first test case, the good substrings are:

翻译中…
  • $[1,1]$,
  • $[1,2]$,
  • $[3,6]$,
  • $[4,5]$,
  • $[4,6]$,
  • $[5,5]$,
  • $[5,6]$,
  • $[6,6]$.
翻译中…

In the second test case, all substrings are good except $[2,2]$.

翻译中…

In the third test case, all substrings are good.

翻译中…

样例

Input #1
4
6
100011
3
101
5
11111
6
010101
Output #1
8
5
15
18
InputCopy OutputCopy
题目信息
题号
1750H
时间限制
2 s
内存限制
256 MB
标签
构造
数据结构
动态规划

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。