Topic link
Title:
Find out that the sum of this sequence is equal to the number of elements in this sequence. Ask how many such sequences you have.
answer:
First look at what we require is the sequence and the number of sequences, then we can convert the entire sequence to minus 1, and the interval is 0 is the sequence we require.
In this way, we only need to process a prefix sum, and then count the number of repeated occurrences of the prefix.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+7;
ll a[maxn],b[maxn],c[maxn];
string str;
map<ll,ll> mp;
int main()
{
ll t;
cin>>t;
while(t--){
ll n;
scanf("%lld",&n);
cin>>str;
mp.clear();
ll sum=0;
for(int i=0;i<str.size();i++){
a[i+1]=a[i]+(str[i]-'0')-1;
if(a[i+1]==0) sum++;
sum+=mp[a[i+1]];
mp[a[1+i]]++;
}
cout<<sum<<endl;
}
return 0;
}