标签:ade property 题意 head sum strong 理解 byte targe
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 .
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 a single integer — the number of good subarrays of aa .
3 1 2 -3
5
3 41 -41 41
3
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); }
标签:ade property 题意 head sum strong 理解 byte targe
原文地址:https://www.cnblogs.com/asunayi/p/12684469.html