#include <iostream> #include <map> #include <vector> using namespace std; long twofromn(int n) { return (((long)n) * (n - 1)) / 2; } long threefromn(int n) { long result = 0; while (n) { --n; result += ((long)n) * (n - 1); } return result / 2; } long sum2count(map< int, int > const &m, int value) { long result = 0; map< int, int >::const_iterator beg = m.begin(); while (beg != m.end() && beg->first < value) { map< int, int >::const_iterator end = m.lower_bound(value - beg->first); if (end != m.end()) { if (end->first < beg->first) break; if (beg->first + end->first == value) { if (end->first == beg->first) { result += twofromn(beg->second); break; } result += ((long)(beg->second)) * end->second; } } ++beg; } return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector< int > a(n); for (int i = 0; i < n; cin >> a[i++]) { } map< int, int > pos, neg; int zero_count = 0; for (int i = 0; i < n; i++) { int c = 0; for (int j = i; j < n; j++) if (c += a[j]) if (c < 0) neg[-c]++; else pos[c]++; else ++zero_count; } long result = threefromn(zero_count); while (neg.size() && pos.size()) { map< int, int >::iterator neg_it = --neg.end(), pos_it = --pos.end(); if (neg_it->first == pos_it->first) result += (long)zero_count * neg_it->second * pos_it->second; if (neg_it->first > pos_it->first) { result += sum2count(pos, neg_it->first) * neg_it->second; neg.erase(neg_it); } else { result += sum2count(neg, pos_it->first) * pos_it->second; pos.erase(pos_it); } } cout << result << '\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 | #include <iostream> #include <map> #include <vector> using namespace std; long twofromn(int n) { return (((long)n) * (n - 1)) / 2; } long threefromn(int n) { long result = 0; while (n) { --n; result += ((long)n) * (n - 1); } return result / 2; } long sum2count(map< int, int > const &m, int value) { long result = 0; map< int, int >::const_iterator beg = m.begin(); while (beg != m.end() && beg->first < value) { map< int, int >::const_iterator end = m.lower_bound(value - beg->first); if (end != m.end()) { if (end->first < beg->first) break; if (beg->first + end->first == value) { if (end->first == beg->first) { result += twofromn(beg->second); break; } result += ((long)(beg->second)) * end->second; } } ++beg; } return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector< int > a(n); for (int i = 0; i < n; cin >> a[i++]) { } map< int, int > pos, neg; int zero_count = 0; for (int i = 0; i < n; i++) { int c = 0; for (int j = i; j < n; j++) if (c += a[j]) if (c < 0) neg[-c]++; else pos[c]++; else ++zero_count; } long result = threefromn(zero_count); while (neg.size() && pos.size()) { map< int, int >::iterator neg_it = --neg.end(), pos_it = --pos.end(); if (neg_it->first == pos_it->first) result += (long)zero_count * neg_it->second * pos_it->second; if (neg_it->first > pos_it->first) { result += sum2count(pos, neg_it->first) * neg_it->second; neg.erase(neg_it); } else { result += sum2count(neg, pos_it->first) * pos_it->second; pos.erase(pos_it); } } cout << result << '\n'; } |