#include<bits/stdc++.h> using namespace std; long long n, t[1000005]; long long wyn; vector<long long> v; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 0; i < n; i++) { cin >> t[i]; } for(int L = 0; L < n; L++) { long long sum = t[L]; v.push_back(t[L]); for(int P = L + 1; P < n; P++) { sum += t[P]; v.push_back(sum); } } sort(v.begin(), v.end()); while(v.size() > 2) { long long val = -v[v.size() - 1]; v.pop_back(); long long l = 0; long long p = v.size() - 1; while(l < p) { if(v[l] + v[p] > val) p--; else if(v[l] + v[p] < val) l++; else { if(v[l] != v[p]) { long long ilP = 1; long long ilL = 1; while(p > 0 && v[p - 1] == v[p]) { p--; ilP++; } p--; while(l < v.size() && v[l + 1] == v[l]) { l++; ilL++; } l++; wyn += ilP * ilL; } else { long long ilP = 1; while(p > 0 && v[p - 1] == v[p]) { p--; ilP++; } wyn += (ilP * (ilP - 1)) / 2; } } } } cout << wyn; }
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 | #include<bits/stdc++.h> using namespace std; long long n, t[1000005]; long long wyn; vector<long long> v; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 0; i < n; i++) { cin >> t[i]; } for(int L = 0; L < n; L++) { long long sum = t[L]; v.push_back(t[L]); for(int P = L + 1; P < n; P++) { sum += t[P]; v.push_back(sum); } } sort(v.begin(), v.end()); while(v.size() > 2) { long long val = -v[v.size() - 1]; v.pop_back(); long long l = 0; long long p = v.size() - 1; while(l < p) { if(v[l] + v[p] > val) p--; else if(v[l] + v[p] < val) l++; else { if(v[l] != v[p]) { long long ilP = 1; long long ilL = 1; while(p > 0 && v[p - 1] == v[p]) { p--; ilP++; } p--; while(l < v.size() && v[l + 1] == v[l]) { l++; ilL++; } l++; wyn += ilP * ilL; } else { long long ilP = 1; while(p > 0 && v[p - 1] == v[p]) { p--; ilP++; } wyn += (ilP * (ilP - 1)) / 2; } } } } cout << wyn; } |