//{ 2,2,1,0,-1,-1 };
#include <bits/stdc++.h>
using namespace std;
struct ab{
int a,b;
};
bool comp(ab a,ab b){
return a.a<b.a||(a.a==b.b&&a.b<b.b);
}
int t[150001];
int t0[501];
unordered_map<int,vector<int>>mapa;
int bin(int j,vector<int>& v){
int l=0,r=v.size()-1,m;
while(l<=r){
m=(l+r)/2;
if(v[m]<=j){
l=m+1;
}
// else if(v[m]>j&&v[(l+m-1)/2]<j){
//r=m-1;
// return v.size()-m+11;
// }
else r=m-1;
}
if(v[l]<j)l++;
if(l>v.size())return 0;//?? nie mozliwe...
//if(l>1&&v[l-1]>=j)l--;
return v.size()-l;
}
int main()
{
ios_base::sync_with_stdio(false);
// for(int i=0;i<500;++i)cout<<0<<' ';
int n;
cin>>n;
int rz=1;
for(int i=0;i<n;++i){
cin>>t0[i];
if(t0[i]!=0)rz=0;
}
int l=0;//i=0,j=1;
for(int i=0;i<n;++i){
t[l]=t0[i];
//t[l].b=l;
//cout<<t[l].a<<' ';
l++;
for(int j=i+1;j<n;j++){
t[l]=t[l-1]+t0[j];
// t[l].b=l;
// cout<<t[l].a<<' ';
l++;
}
}
// cout<<l<<endl;
// cout<<endl;
if(rz){
long long ww=(l*(l-1)*(l-2))/6;
cout<<ww;
return 0;
}
sort(t,t+l);
n=l;
unsigned long long wyn=0;
//unsigned int wyn=0;
for(int i=0;i<n;++i){
mapa[t[i]].push_back(i);
}
for(int i=0;i<n;++i){
sort(mapa[t[i]].begin(),mapa[t[i]].end());
}
for(int i=0;i<n-2;++i){
for(int j=i+1;j<n-1;++j){
int search_value = -(t[i] + t[j]);
if(mapa.find(search_value)!=mapa.end()&&mapa[search_value].size()>0){
// for(auto k:mapa[search_value]){
// if(k>j)wyn++;
//cout<<k<<' ';
// }
wyn+=bin(j,mapa[search_value]);
}
}
}
cout<<wyn;
return 0;
}
//*/
// 3 2 0 -1
//5 -3 0 4 -2 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | //{ 2,2,1,0,-1,-1 }; #include <bits/stdc++.h> using namespace std; struct ab{ int a,b; }; bool comp(ab a,ab b){ return a.a<b.a||(a.a==b.b&&a.b<b.b); } int t[150001]; int t0[501]; unordered_map<int,vector<int>>mapa; int bin(int j,vector<int>& v){ int l=0,r=v.size()-1,m; while(l<=r){ m=(l+r)/2; if(v[m]<=j){ l=m+1; } // else if(v[m]>j&&v[(l+m-1)/2]<j){ //r=m-1; // return v.size()-m+11; // } else r=m-1; } if(v[l]<j)l++; if(l>v.size())return 0;//?? nie mozliwe... //if(l>1&&v[l-1]>=j)l--; return v.size()-l; } int main() { ios_base::sync_with_stdio(false); // for(int i=0;i<500;++i)cout<<0<<' '; int n; cin>>n; int rz=1; for(int i=0;i<n;++i){ cin>>t0[i]; if(t0[i]!=0)rz=0; } int l=0;//i=0,j=1; for(int i=0;i<n;++i){ t[l]=t0[i]; //t[l].b=l; //cout<<t[l].a<<' '; l++; for(int j=i+1;j<n;j++){ t[l]=t[l-1]+t0[j]; // t[l].b=l; // cout<<t[l].a<<' '; l++; } } // cout<<l<<endl; // cout<<endl; if(rz){ long long ww=(l*(l-1)*(l-2))/6; cout<<ww; return 0; } sort(t,t+l); n=l; unsigned long long wyn=0; //unsigned int wyn=0; for(int i=0;i<n;++i){ mapa[t[i]].push_back(i); } for(int i=0;i<n;++i){ sort(mapa[t[i]].begin(),mapa[t[i]].end()); } for(int i=0;i<n-2;++i){ for(int j=i+1;j<n-1;++j){ int search_value = -(t[i] + t[j]); if(mapa.find(search_value)!=mapa.end()&&mapa[search_value].size()>0){ // for(auto k:mapa[search_value]){ // if(k>j)wyn++; //cout<<k<<' '; // } wyn+=bin(j,mapa[search_value]); } } } cout<<wyn; return 0; } //*/ // 3 2 0 -1 //5 -3 0 4 -2 3 |
English