Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- int t;
- cin>>t;
- while(t--)
- {
- int n;
- string s;
- cin >> n >> s;
- int val = 0;
- map<int, int> sum;
- sum[0]++;
- long long int ans = n *1ll* (n + 1) / 2;
- for (auto e : s){
- if (e == '1')
- val++;
- else
- val--;
- sum[val]++;
- ans += sum[val];
- }
- cout << ans << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment