https://codeforces.com/problemset/problem/1333/C
2.Idea
Keep tracking right most sum zero segment.
3.Source
int n;
map<ll, int> mp;
ll ans, sum;
int x, q;
int main()
{
cin >> n;
q = -1;
mp[0] = 0;
for (int i = 1; i <= n; i++)
{
cin >> x;
sum += x;
if (mp.count(sum)) {
q = max(q, mp[sum]);
}
ans += (i - q - 1);
mp[sum] = i;
}
cout << ans << endl;
return 0;
}
No comments:
Post a Comment