#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 5e2 + 7; const int MAX = 1e7 + 7; const ll INF = 1e18; int tab[MAXN]; //map<int, int> cnt; int cntmin[MAX], cntmax[MAX]; void solve(){ int n; cin >> n; for(int i = 1; i <= n; i++){ cin >> tab[i]; } vector<int> v; int ile0 = 0; for(int i = 1; i <= n; i++){ int val = 0; for(int j = i; j <= n; j++){ val += tab[j]; if(val == 0) ile0++; else v.push_back(val); } } sort(v.begin(), v.end()); for(int i = 0; i < v.size(); i++){ if(v[i] < 0) cntmin[-v[i]]++; else cntmax[v[i]]++; } int zeroans = 0, ans = 0; zeroans += (ile0 * (ile0 - 1) * (ile0 - 2))/6; for(auto u : v){ if(u > 0) break; if(cntmax[-u]) zeroans += ile0; } for(int i = 0; i < v.size(); i++){ for(int j = i + 1; j < v.size(); j++){ int left = 0 - (v[i] + v[j]); int ile; if(left < 0) ile = cntmin[-left]; else ile = cntmax[left]; if(v[i] == left) ile--; if(v[j] == left) ile--; ans += ile; } } cout << zeroans + ans/3 << '\n'; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; //cin >> t; while(t--) solve(); return 0; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 5e2 + 7; const int MAX = 1e7 + 7; const ll INF = 1e18; int tab[MAXN]; //map<int, int> cnt; int cntmin[MAX], cntmax[MAX]; void solve(){ int n; cin >> n; for(int i = 1; i <= n; i++){ cin >> tab[i]; } vector<int> v; int ile0 = 0; for(int i = 1; i <= n; i++){ int val = 0; for(int j = i; j <= n; j++){ val += tab[j]; if(val == 0) ile0++; else v.push_back(val); } } sort(v.begin(), v.end()); for(int i = 0; i < v.size(); i++){ if(v[i] < 0) cntmin[-v[i]]++; else cntmax[v[i]]++; } int zeroans = 0, ans = 0; zeroans += (ile0 * (ile0 - 1) * (ile0 - 2))/6; for(auto u : v){ if(u > 0) break; if(cntmax[-u]) zeroans += ile0; } for(int i = 0; i < v.size(); i++){ for(int j = i + 1; j < v.size(); j++){ int left = 0 - (v[i] + v[j]); int ile; if(left < 0) ile = cntmin[-left]; else ile = cntmax[left]; if(v[i] == left) ile--; if(v[j] == left) ile--; ans += ile; } } cout << zeroans + ans/3 << '\n'; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; //cin >> t; while(t--) solve(); return 0; } |