#include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; int shift = 500*20000; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> S; for (int i = 0; i < n; ++i) { int s = 0; for (int j = i; j < n; ++j) { s += a[j]; S.push_back(s); } } long long ans = 0; vector<vector<int> > H(2*shift+1); int nS = S.size(); for (int i = 0; i < nS; ++i) { H[S[i]+shift].push_back(i); } for (int i = 0; i < nS - 2; ++i) { for (int j = i + 1; j < nS - 1; ++j) { int r = -(S[i] + S[j])+shift; auto it = upper_bound(H[r].begin(), H[r].end(), j); if (it != H[r].end()) { ans += H[r].size() - distance(H[r].begin(), it); } } } cout << ans << endl; 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 | #include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; int shift = 500*20000; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> S; for (int i = 0; i < n; ++i) { int s = 0; for (int j = i; j < n; ++j) { s += a[j]; S.push_back(s); } } long long ans = 0; vector<vector<int> > H(2*shift+1); int nS = S.size(); for (int i = 0; i < nS; ++i) { H[S[i]+shift].push_back(i); } for (int i = 0; i < nS - 2; ++i) { for (int j = i + 1; j < nS - 1; ++j) { int r = -(S[i] + S[j])+shift; auto it = upper_bound(H[r].begin(), H[r].end(), j); if (it != H[r].end()) { ans += H[r].size() - distance(H[r].begin(), it); } } } cout << ans << endl; return 0; } |