其他分享
首页 > 其他分享> > cf C. Eugene and an array

cf C. Eugene and an array

作者:互联网

C. Eugene and an array ps:又是一道我理解错题意的题 time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output

Eugene likes working with arrays. And today he needs your help in solving one challenging task.

An array cc is a subarray of an array b if cc can be obtained from bb by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array [−1,2,−3] is good, as all arrays [−1] , [−1,2] , [−1,2,−3] , [2] , [2,−3] , [−3] have nonzero sums of elements. However, array [−1,2,−1,−3] isn't good, as his subarray [−1,2,−1] has sum of elements equal to 00 .

Help Eugene to calculate the number of nonempty good subarrays of a given array a .

Input

The first line of the input contains a single integer nn (1≤n≤2×1051≤n≤2×105 )  — the length of array aa .

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109 )  — the elements of aa .

Output

Output a single integer  — the number of good subarrays of aa .

Examples Input Copy
3
1 2 -3
Output Copy
5
Input Copy
3
41 -41 41
Output Copy
3
Note

In the first sample, the following subarrays are good: [1] , [1,2] , [2] , [2,−3] , [−3] . However, the subarray [1,2,−3] isn't good, as its subarray [1,2,−3] has sum of elements equal to 0 .

In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray [41,−41,41] isn't good, as its subarray [41,−41] has sum of elements equal to 0 .

#include<bits/stdc++.h>
using namespace std;
#define ll long long
map<ll,int> pos;
ll ans,s;
int n,x;
int main() {
    scanf("%d",&n);
    pos[0]=0;
    int q=-1;
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        s+=x;
        //printf("s=%lld\n",s);
        //printf("pos.count(s)=%d\n",pos.count(s));
        if (pos.count(s)) q=max(q,pos[s]);
        //cout<<"q="<<q<<endl;
        ans+=i-q-1;
        //printf("ans=%lld\n",ans);
        pos[s]=i;
        //printf("pos[s]=%d\n\n",i);
    }
    printf("%lld\n",ans);
}

 

 

标签:good,subarrays,cf,41,elements,subarray,array,Eugene
来源: https://www.cnblogs.com/asunayi/p/12684469.html