给定长度为n的序列,定义序列a为“好的”,当且仅当,a的子段中不存在sum值为0。那么根据题意,若sum[a[i]]在i前面存在,那么这样的序列只能最后一次取到sum[a[i]]的下标k以后,数量为i - k。
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define f(i,a,b) for(int i = a; i < b; i++)
typedef pair<int,int> P;
#define ll long long
ll a[200010];
map<ll,int> mp;
int main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
ff(i,1,n) cin >> a[i];
int least = 0;
ll sum = 0,ans = 0;
mp[0] = 1;
ff(i,1,n){
sum += a[i];
if(mp.count(sum)) least = max(least,mp[sum]);
mp[sum] = i + 1;
ans += i - least;
}
cout << ans << endl;
return 0;
}