CF1660F2.Promising String (hard version)

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

This is the hard version of Problem F. The only difference between the easy version and the hard version is the constraints.

We will call a non-empty string balanced if it contains the same number of plus and minus signs. For example: strings "+--+" and "++-+--" are balanced, and strings "+--", "--" and "" are not balanced.

We will call a string promising if the string can be made balanced by several (possibly zero) uses of the following operation:

  • replace two adjacent minus signs with one plus sign.

In particular, every balanced string is promising. However, the converse is not true: not every promising string is balanced.

For example, the string "-+---" is promising, because you can replace two adjacent minuses with plus and get a balanced string "-++-", or get another balanced string "-+-+".

How many non-empty substrings of the given string ss are promising? Each non-empty promising substring must be counted in the answer as many times as it occurs in string ss .

Recall that a substring is a sequence of consecutive characters of the string. For example, for string "+-+" its substring are: "+-", "-+", "+", "+-+" (the string is a substring of itself) and some others. But the following strings are not its substring: "--", "", "-".

输入格式

The first line of the input contains an integer tt ( 1t1041 \le t \le 10^4 ) —the number of test cases in the test.

Then the descriptions of test cases follow.

Each test case of input data consists of two lines. The first line consists of the number nn ( 1n21051 \le n \le 2 \cdot 10^5 ): the length of ss .

The second line of the test case contains the string ss of length nn , consisting only of characters "+" and "-".

It is guaranteed that the sum of values nn over all test cases does not exceed 21052 \cdot 10^5 .

输出格式

For each test case, print a single number: the number of the promising non-empty substrings of string ss . Each non-empty promising substring must be counted in the answer as many times as it occurs in string ss .

输入输出样例

  • 输入#1

    5
    3
    +-+
    5
    -+---
    4
    ----
    7
    --+---+
    6
    +++---

    输出#1

    2
    4
    2
    7
    4

说明/提示

The following are the promising substrings for the first three test cases in the example:

  1. s[12]s[1 \dots 2] ="+-", s[23]s[2 \dots 3] ="-+";
  2. s[12]s[1 \dots 2] ="-+", s[23]s[2 \dots 3] ="+-", s[15]s[1 \dots 5] ="-+---", s[35]s[3 \dots 5] ="---";
  3. s[13]s[1 \dots 3] ="---", s[24]s[2 \dots 4] ="---".
首页