#include <iostream> #include <map> #include <vector> using namespace std; using u32 = unsigned int; struct TestCase { u32 n; vector<int> seq; }; TestCase read_test_case() { TestCase tc; cin >> tc.n; tc.seq.resize(tc.n); for (auto& a : tc.seq) cin >> a; return tc; } void solve_test_case(TestCase tc); int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); solve_test_case(read_test_case()); } struct Pair { u32 a, b; }; void solve_test_case(TestCase tc) { vector<int> sums; for (u32 a = 0; a < tc.n; a++) for (u32 b = 0; b <= a; b++) { int s = 0; for (u32 i = b; i <= a; i++) s += tc.seq[i]; sums.push_back(s); } map<int, vector<u32>> indices; for (u32 i = 0; i < sums.size(); i++) indices[sums[i]].push_back(i); u32 cnt = 0; for (u32 i = 0; i < sums.size(); i++) for (u32 j = 0; j < i; j++) { int s = -(sums[i] + sums[j]); for (auto x : indices[s]) { if (x < j) cnt++; } } cout << cnt << "\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 | #include <iostream> #include <map> #include <vector> using namespace std; using u32 = unsigned int; struct TestCase { u32 n; vector<int> seq; }; TestCase read_test_case() { TestCase tc; cin >> tc.n; tc.seq.resize(tc.n); for (auto& a : tc.seq) cin >> a; return tc; } void solve_test_case(TestCase tc); int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); solve_test_case(read_test_case()); } struct Pair { u32 a, b; }; void solve_test_case(TestCase tc) { vector<int> sums; for (u32 a = 0; a < tc.n; a++) for (u32 b = 0; b <= a; b++) { int s = 0; for (u32 i = b; i <= a; i++) s += tc.seq[i]; sums.push_back(s); } map<int, vector<u32>> indices; for (u32 i = 0; i < sums.size(); i++) indices[sums[i]].push_back(i); u32 cnt = 0; for (u32 i = 0; i < sums.size(); i++) for (u32 j = 0; j < i; j++) { int s = -(sums[i] + sums[j]); for (auto x : indices[s]) { if (x < j) cnt++; } } cout << cnt << "\n"; } |