You are given an array 𝑎1,𝑎2,…,𝑎𝑛 consisting of integers from 0 to 9. A subarray 𝑎𝑙,𝑎𝑙+1,𝑎𝑙+2,…,𝑎𝑟−1,𝑎𝑟 is good if the sum of elements of this subarray is equal to the length of this subarray (∑𝑖=𝑙𝑟𝑎𝑖=𝑟−𝑙+1).

For example, if 𝑎=[1,2,0], then there are 3 good subarrays: 𝑎1…1=[1],𝑎2…3=[2,0] and 𝑎1…3=[1,2,0].

Calculate the number of good subarrays of the array 𝑎.

Input
The first line contains one integer 𝑡 (1≤𝑡≤1000) — the number of test cases.

The first line of each test case contains one integer 𝑛 (1≤𝑛≤105) — the length of the array 𝑎.

The second line of each test case contains a string consisting of 𝑛 decimal digits, where the 𝑖-th digit is equal to the value of 𝑎𝑖.

It is guaranteed that the sum of 𝑛 over all test cases does not exceed 105.

题意:
问数组中如果满足 ∑ l r a [ i ] = = r − l + 1 \sum_l^r{a[i]}==r-l+1 lra[i]==rl+1,那就是一个good 数组。
分析:
我们通过等式变换转化成 ∑ l r a [ i ] − 1 = = 0 \sum_l^r{a[i]-1}==0 lra[i]1==0,然后拿桶来记录每个sum存在的次数就可以了
要注意的是sum如果累加成0要另外算(前面没数的情况)。
代码:

#include<iostream>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
int a[100010];
int sum[100010];
map<int,int>mp;
int main()
{
   
	int t;
	cin>>t;
	while(t--)
	{
   
		mp.clear();
		int n;
		cin>>n;
		string s;
		cin>>s;
		for(int i=0;i<n;i++)
			a[i+1]=s[i]-'0' - 1;
		int sum=0;
		long long res=0;
		for(int i=1;i<=n;i++)
		{
   
			sum+=a[i];
			if(sum==0)
				res++;
			res+=mp[sum]++;
		}
		cout<<res<<endl;
	}
	return 0;
}