#include <bits/stdc++.h> using namespace std; int n; long long i,j,s,k,x,y,z,h,w,ak,ai,aj; vector<int> a,d,f; array<int,40000001> b; //array<vector<short>,60000001> b,c; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; a={0}; for (i=0;i<n;i++){ cin >> j; s+=j; a.push_back(s); } s=0; for (k=0;k<n;k++){ ak=a[k]-20000000; for (i=0;i<n;i++){ ai=a[i]+ak; for (j=i+1;j<=n;j++){ b[a[j]-ai]++; } } ak=a[k+1]-20000000; for (i=0;i<n;i++){ ai=a[i]-ak; for (j=i+1;j<=n;j++){ s+=b[-a[j]+ai]; } } } for (i=0;i<n;i++){ for (j=i+1;j<=n;j++){ d.push_back(a[j]-a[i]); } } make_heap(d.begin(),d.end()); sort_heap(d.begin(),d.end()); x=1e+9; j=d.size()-1; i=0; //cout << s << '\n'; k=0; while (i<d.size()){ w=d[i]; y=1; i++; while (i<d.size() and d[i]==w){ i++; y++; } while (j>=0 and d[j]+2*w>0) j--; if (j<0) break; if (d[j]==-2*w){ k=0; while (j>=0 and d[j]+2*w==0){ k++; j--; } } s-=3*k*y; //cout << k << '#' << y << '\n'; k=0; if (w==0) s+=2*y; } cout << s/6 << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; int n; long long i,j,s,k,x,y,z,h,w,ak,ai,aj; vector<int> a,d,f; array<int,40000001> b; //array<vector<short>,60000001> b,c; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; a={0}; for (i=0;i<n;i++){ cin >> j; s+=j; a.push_back(s); } s=0; for (k=0;k<n;k++){ ak=a[k]-20000000; for (i=0;i<n;i++){ ai=a[i]+ak; for (j=i+1;j<=n;j++){ b[a[j]-ai]++; } } ak=a[k+1]-20000000; for (i=0;i<n;i++){ ai=a[i]-ak; for (j=i+1;j<=n;j++){ s+=b[-a[j]+ai]; } } } for (i=0;i<n;i++){ for (j=i+1;j<=n;j++){ d.push_back(a[j]-a[i]); } } make_heap(d.begin(),d.end()); sort_heap(d.begin(),d.end()); x=1e+9; j=d.size()-1; i=0; //cout << s << '\n'; k=0; while (i<d.size()){ w=d[i]; y=1; i++; while (i<d.size() and d[i]==w){ i++; y++; } while (j>=0 and d[j]+2*w>0) j--; if (j<0) break; if (d[j]==-2*w){ k=0; while (j>=0 and d[j]+2*w==0){ k++; j--; } } s-=3*k*y; //cout << k << '#' << y << '\n'; k=0; if (w==0) s+=2*y; } cout << s/6 << '\n'; } |