#include <iostream> #include <vector> #include <algorithm> #include <unordered_map> #include <cassert> using namespace std; const bool DBG = false; const bool DBG2 = false; unordered_map<int, int> U, D; long long zera; long long solve() { long long result = 0; if (zera >= 3) { result += zera * (zera - 1) * (zera - 2) / 6; if (DBG) { cout << " 3 zera " << zera << " => " << result << endl; } } if (U.size() == 0 || D.size() == 0) { return result; } for (auto u1 = U.begin(); u1 != U.end(); ++u1) { if (zera) { int d = -u1->first; auto it = D.find(d); if (it != D.end()) { auto cnt = (long long) u1->second * zera * it->second; result += cnt; if (DBG) { cout << u1->first << '[' << u1->second << "] + 0[" << zera << "] + " << d << '[' << it->second << "] => " << cnt << '\n'; } } } for (auto u2 = u1; u2 != U.end(); ++u2 ) { if (u2 == u1) { int d = -2*u1->first; auto it = D.find(d); if (it != D.end()) { auto cnt = (long long) u1->second * (u1->second - 1) * (it->second) / 2; result += cnt; if (DBG) { cout << "2 * " << u1->first << '[' << u1->second << "] + " << d << '[' << it->second << "] => " << cnt << '\n'; } } } else { int d = -u1->first - u2->first; auto it = D.find(d); if (it != D.end()) { auto cnt = (long long) u1->second * (u2->second) * it->second; result += cnt; if (DBG) { cout << u1->first << '[' << u1->second << "] + " << u2->first << '[' << u2->second << "] + " << d << '[' << it->second << "] => " << cnt << '\n'; } } } } } return result; } long long solve2() { long long result = 0; if (U.size() == 0 || D.size() == 0) { return result; } for (auto u1 = U.begin(); u1 != U.end(); ++u1) { for (auto u2 = u1; u2 != U.end(); ++u2 ) { if (u2 == u1) { int d = -2*u1->first; auto it = D.find(d); if (it != D.end()) { auto cnt = (long long) u1->second * (u1->second - 1) * (it->second) / 2; result += cnt; if (DBG) { cout << "2 * " << u1->first << '[' << u1->second << "] + " << d << '[' << it->second << "] => " << cnt << '\n'; } } } else { int d = -u1->first - u2->first; auto it = D.find(d); if (it != D.end()) { auto cnt = (long long) u1->second * (u2->second) * it->second; result += cnt; if (DBG) { cout << u1->first << '[' << u1->second << "] + " << u2->first << '[' << u2->second << "] + " << d << '[' << it->second << "] => " << cnt << '\n'; } } } } } return result; } int main() { int n; cin >> n; vector<int> liczby(n); for (int i = 0; i < n; ++i) { cin >> liczby[i]; } //cout << D.bucket_count() << endl; D.reserve(20000); U.reserve(20000); //cout << D.bucket_count() << endl; for (int i = 0; i < n; ++i) { int sum = 0; for (int j = i; j < n; ++j) { sum += liczby[j]; if (sum > 0) D[sum]++; else if (sum < 0) U[sum]++; else ++zera; if (DBG2) { cout << "sum " << sum << " zera " << zera << endl; } } if (DBG) { cout << "i " << i << " sizes " << D.size() << ' ' << U.size() << endl; } } long long result; if (D.size() < U.size()) { swap(D, U); } //cout << D.bucket_count() << endl; //cout << D.size() << endl; result = solve(); swap(U, D); result += solve2(); cout << result << 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 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #include <iostream> #include <vector> #include <algorithm> #include <unordered_map> #include <cassert> using namespace std; const bool DBG = false; const bool DBG2 = false; unordered_map<int, int> U, D; long long zera; long long solve() { long long result = 0; if (zera >= 3) { result += zera * (zera - 1) * (zera - 2) / 6; if (DBG) { cout << " 3 zera " << zera << " => " << result << endl; } } if (U.size() == 0 || D.size() == 0) { return result; } for (auto u1 = U.begin(); u1 != U.end(); ++u1) { if (zera) { int d = -u1->first; auto it = D.find(d); if (it != D.end()) { auto cnt = (long long) u1->second * zera * it->second; result += cnt; if (DBG) { cout << u1->first << '[' << u1->second << "] + 0[" << zera << "] + " << d << '[' << it->second << "] => " << cnt << '\n'; } } } for (auto u2 = u1; u2 != U.end(); ++u2 ) { if (u2 == u1) { int d = -2*u1->first; auto it = D.find(d); if (it != D.end()) { auto cnt = (long long) u1->second * (u1->second - 1) * (it->second) / 2; result += cnt; if (DBG) { cout << "2 * " << u1->first << '[' << u1->second << "] + " << d << '[' << it->second << "] => " << cnt << '\n'; } } } else { int d = -u1->first - u2->first; auto it = D.find(d); if (it != D.end()) { auto cnt = (long long) u1->second * (u2->second) * it->second; result += cnt; if (DBG) { cout << u1->first << '[' << u1->second << "] + " << u2->first << '[' << u2->second << "] + " << d << '[' << it->second << "] => " << cnt << '\n'; } } } } } return result; } long long solve2() { long long result = 0; if (U.size() == 0 || D.size() == 0) { return result; } for (auto u1 = U.begin(); u1 != U.end(); ++u1) { for (auto u2 = u1; u2 != U.end(); ++u2 ) { if (u2 == u1) { int d = -2*u1->first; auto it = D.find(d); if (it != D.end()) { auto cnt = (long long) u1->second * (u1->second - 1) * (it->second) / 2; result += cnt; if (DBG) { cout << "2 * " << u1->first << '[' << u1->second << "] + " << d << '[' << it->second << "] => " << cnt << '\n'; } } } else { int d = -u1->first - u2->first; auto it = D.find(d); if (it != D.end()) { auto cnt = (long long) u1->second * (u2->second) * it->second; result += cnt; if (DBG) { cout << u1->first << '[' << u1->second << "] + " << u2->first << '[' << u2->second << "] + " << d << '[' << it->second << "] => " << cnt << '\n'; } } } } } return result; } int main() { int n; cin >> n; vector<int> liczby(n); for (int i = 0; i < n; ++i) { cin >> liczby[i]; } //cout << D.bucket_count() << endl; D.reserve(20000); U.reserve(20000); //cout << D.bucket_count() << endl; for (int i = 0; i < n; ++i) { int sum = 0; for (int j = i; j < n; ++j) { sum += liczby[j]; if (sum > 0) D[sum]++; else if (sum < 0) U[sum]++; else ++zera; if (DBG2) { cout << "sum " << sum << " zera " << zera << endl; } } if (DBG) { cout << "i " << i << " sizes " << D.size() << ' ' << U.size() << endl; } } long long result; if (D.size() < U.size()) { swap(D, U); } //cout << D.bucket_count() << endl; //cout << D.size() << endl; result = solve(); swap(U, D); result += solve2(); cout << result << endl; return 0; } |