Advertisement
tepyotin2

Good Subarray

Jun 20th, 2025
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. int t;
  8.  
  9. int main(){
  10.     //freopen("goodsubarrays.in", "r", stdin);
  11.    
  12.     cin >> t;
  13.     while(t--){
  14.         int n;
  15.         cin >> n;
  16.         string s;
  17.         cin >> s;
  18.         ll sum = 0;
  19.         unordered_map<ll, int> freq;
  20.         freq[0] = 1;
  21.         ll ans = 0;
  22.         for(int i=0; i<n; i++){
  23.             sum+=s[i]-'0';
  24.             ll val = sum-(i+1);
  25.             ans+=freq[val];
  26.             freq[val]++;
  27.         }
  28.         cout << ans << '\n';
  29.     }
  30.    
  31.     return 0;
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement