cf C. Eugene and an array

Posted asunayi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 (1n2×1051≤n≤2×105 )  — the length of array aa .

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (109ai109−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
",s);
        //printf("pos.count(s)=%d
",pos.count(s));
        if (pos.count(s)) q=max(q,pos[s]);
        //cout<<"q="<<q<<endl;
        ans+=i-q-1;
        //printf("ans=%lld
",ans);
        pos[s]=i;
        //printf("pos[s]=%d

",i);
    }
    printf("%lld
",ans);
}

 

 

以上是关于cf C. Eugene and an array的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 1333C Eugene and an array

codeforces-1333C-Eugene and an array

CodeForces 1333-C Eugene and an array(子区间和为0前缀和)

C. Ayoub and Lost Array cf dp

cf1557 C. Moamen and XOR

CF354C Vasya and Beautiful Arrays