#include <iostream> #include <vector> #include <unordered_map> #include <algorithm> #include <tuple> int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cin.tie(0); int N; std::cin >> N; std::vector<int> a(N); for(int& x : a) { std::cin >> x; } std::unordered_map<int, long long> cnt; for(int i = 0; i < N; i++) { int sum = 0; for(int j = i; j < N; j++) { sum += a[j]; cnt[sum]++; } } std::vector<std::tuple<int, long long>> segments; for(auto [sum, c] : cnt) { segments.push_back({sum, c}); } std::sort(segments.begin(), segments.end()); long long result = 0; for(int i = 0; i < (int)segments.size(); i++) { int it = (int)segments.size() - 1; auto [sum1, cnt1] = segments[i]; for(int j = i; j < (int)segments.size(); j++) { auto [sum2, cnt2] = segments[j]; int s = -(sum1 + sum2); while(it >= 0 && std::get<0>(segments[it]) > s) { it--; } if(it < j) { break; } auto [sum3, cnt3] = segments[it]; if(sum3 == s) { if(i == j) { if(j == it) { result += cnt1 * (cnt1 - 1LL) * (cnt1 - 2LL) / 6LL; } else { result += cnt1 * (cnt1 - 1LL) / 2LL * cnt3; } } else { if(j == it) { result += cnt1 * cnt2 * (cnt2 - 1LL) / 2LL; } else { result += cnt1 * cnt2 * cnt3; } } } } } std::cout << result << '\n'; 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 | #include <iostream> #include <vector> #include <unordered_map> #include <algorithm> #include <tuple> int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cin.tie(0); int N; std::cin >> N; std::vector<int> a(N); for(int& x : a) { std::cin >> x; } std::unordered_map<int, long long> cnt; for(int i = 0; i < N; i++) { int sum = 0; for(int j = i; j < N; j++) { sum += a[j]; cnt[sum]++; } } std::vector<std::tuple<int, long long>> segments; for(auto [sum, c] : cnt) { segments.push_back({sum, c}); } std::sort(segments.begin(), segments.end()); long long result = 0; for(int i = 0; i < (int)segments.size(); i++) { int it = (int)segments.size() - 1; auto [sum1, cnt1] = segments[i]; for(int j = i; j < (int)segments.size(); j++) { auto [sum2, cnt2] = segments[j]; int s = -(sum1 + sum2); while(it >= 0 && std::get<0>(segments[it]) > s) { it--; } if(it < j) { break; } auto [sum3, cnt3] = segments[it]; if(sum3 == s) { if(i == j) { if(j == it) { result += cnt1 * (cnt1 - 1LL) * (cnt1 - 2LL) / 6LL; } else { result += cnt1 * (cnt1 - 1LL) / 2LL * cnt3; } } else { if(j == it) { result += cnt1 * cnt2 * (cnt2 - 1LL) / 2LL; } else { result += cnt1 * cnt2 * cnt3; } } } } } std::cout << result << '\n'; return 0; } |