linkkkk
题意:
求满足∑ i = l r a i = r − l + 1的子区间个数。
思路:
开始写了个降智代码;
实际上令b i = a i − 1,那么式子就变成了∑ i = l r b i = 0
经 典 题
记录一下这个小思维
代码:
// Problem: C. Good Subarrays // Contest: Codeforces - Educational Codeforces Round 93 (Rated for Div. 2) // URL: https://codeforces.com/problemset/problem/1398/C // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+100; int n,a[maxn]; map<ll,ll>mp; int main(){ int _;cin>>_; while(_--){ cin>>n; for(int i=1;i<=n;i++) scanf("%1d",&a[i]),a[i]--; ll ans=0,sum=0; mp.clear(); mp[0]=1; for(int i=1;i<=n;i++){ sum+=a[i]; ans=ans+mp[sum]; mp[sum]++; } cout<<ans<<endl; } return 0; }