// O(n^3 log n) // attempt 2 #include <bits/stdc++.h> using std::cin, std::cout, std::vector; using u8 = std::uint8_t; using u16 = std::uint16_t; using i32 = std::int32_t; using u32 = std::uint32_t; using u64 = std::uint64_t; #define REP(i, n) for (u32 i=0; i<(n); ++i) #define DEBUG(x) std::cerr << #x << " = " << x << "\n" void init_io() { cin.tie(nullptr); std::ios::sync_with_stdio(false); } u32 low_bit(const u32 x) { return x & -x; } constexpr u32 bit_clear_threshold = 20; class BIT { public: BIT(const u32 n1): counts(512, 0) {} void clear() { counts.assign(512, 0); } void insert(const u32 x) { u32 p = x + 1; REP(i, 9) { counts[p] += 1; p = (p + low_bit(p)) & 511; } counts[0] = 0; } void remove(const u32 x) { u32 p = x + 1; REP(i, 9) { counts[p] -= 1; p = (p + low_bit(p)) & 511; } counts[0] = 0; } u32 count_below(const u32 x) { u32 p = x; u32 res = 0; REP(i, 8) { res += counts[p]; p &= (p-1); } return res; } private: u32 n; // counts[x] = sum([x-low_bit(x), x)) vector<u32> counts; }; u64 divide(const u64 a, const u64 b) { assert(a % b == 0); return a / b; } vector<i32> read_prefix_sums() { u32 n; cin >> n; vector<i32> s; s.reserve(n + 1); i32 s1 = 0; s.push_back(s1); REP(i, n) { i32 a; cin >> a; s1 += a; s.push_back(s1); } return s; } vector<i32> read_prefix_sums_perf() { std::mt19937 rng; std::uniform_int_distribution<i32> dist{-20000, 20000}; u32 n = 500; vector<i32> s; s.reserve(n + 1); i32 s1 = 0; s.push_back(s1); REP(i, n) { i32 a = dist(rng); s1 += a; s.push_back(s1); } return s; } // Count (a, a') such that: // s(a) = s(a') // a < a' // O(n log n) u64 solve_p1(const vector<i32> &s) { u64 res = 0; std::map<i32, u32> counts; for (const i32 x : s) { u32 &c = counts[x]; res += c; c += 1; } return res; } // Count (a, b, a', b') such that: // mult * s(a) + s(b) = mult * s(a') + s(b') // a < a' // b < b' // O(n^2 log n) u64 solve_p2(const vector<i32> &s, const int mult) { const u32 n = s.size(); struct Entry { u32 a; u32 b; i32 sum; }; vector<Entry> entries; entries.reserve(n * n); REP(a, n) REP(b, n) { entries.push_back(Entry{a, b, mult * s[a] + s[b]}); } std::sort(entries.begin(), entries.end(), [](const Entry &e1, const Entry &e2) -> bool { return std::tuple(e1.sum, e1.b, e1.a) < std::tuple(e2.sum, e2.b, e2.a); } ); u64 result = 0; BIT set_a(n); auto sum_begin = entries.begin(); while (sum_begin != entries.end()) { auto sum_end = sum_begin + 1; while (sum_end != entries.end() && sum_end->sum == sum_begin->sum) { ++sum_end; } auto b_begin = sum_begin; while (b_begin != sum_end) { auto b_end = b_begin + 1; while (b_end != sum_end && b_end->b == b_begin->b) { ++b_end; } for (auto it = b_begin; it != b_end; ++it) { result += set_a.count_below(it->a); } for (auto it = b_begin; it != b_end; ++it) { set_a.insert(it->a); } b_begin = b_end; } for (auto it = sum_begin; it != sum_end; ++it) { set_a.remove(it->a); } sum_begin = sum_end; } return result; } // Count (a, b, c, a', b', c') such that: // s(a) + s(b) + s(c) = s(a') + s(b') + s(c') // a < a' // b < b' // c < c' // O(n^3 log n) u64 solve_p3(const vector<i32> &s) { const u32 n = s.size(); const i32 min_sum = 3 * *std::min_element(s.begin(), s.end()); const i32 max_sum = 3 * *std::max_element(s.begin(), s.end()); // max_sum - min_sum <= 3 * 500 * 20'000 = 30M // 120 MB u32 *entries_begin = new u32[max_sum - min_sum + 2]{}; // count REP(c, n) { REP(b, c) { REP(a, b) { entries_begin[s[c] + s[b] + s[a] - min_sum] += 6; } entries_begin[2 * s[c] + s[b] - min_sum] += 3; entries_begin[2 * s[b] + s[c] - min_sum] += 3; } entries_begin[3 * s[c] - min_sum] += 1; } { u32 pos = 0; for (i32 sum = min_sum; sum <= max_sum; ++sum) { u32 &e = entries_begin[sum - min_sum]; pos += e; e = pos; } entries_begin[max_sum - min_sum + 1] = pos; assert(pos == n * n * n); } struct Entry { u16 b; u16 c; }; // 500 MB // vector<Entry> entries(n * n * n); Entry *entries = new Entry[n * n * n]; REP(c_rev, n) { const u32 c = n - 1 - c_rev; REP(b, n) { REP(a, b) { u32 &e = entries_begin[s[c] + s[b] + s[a] - min_sum]; entries[--e] = Entry{u16(a), u16(c)}; entries[--e] = Entry{u16(b), u16(c)}; } { u32 &e = entries_begin[s[c] + 2 * s[b] - min_sum]; entries[--e] = Entry{u16(b), u16(c)}; } } } u64 count_c_smaller = 0; u64 count_bc_smaller = 0; BIT set_b(n); for (i32 sum = min_sum; sum <= max_sum; ++sum) { const u32 sum_begin = entries_begin[sum - min_sum]; const u32 sum_end = entries_begin[sum - min_sum + 1]; u32 c_begin = sum_begin; while (c_begin != sum_end) { const auto c = entries[c_begin].c; u32 c_end = c_begin + 1; while (c_end != sum_end && entries[c_end].c == c) { ++c_end; } count_c_smaller += u64(c_end - c_begin) * u64(c_begin - sum_begin); for (u32 i = c_begin; i != c_end; ++i) { count_bc_smaller += set_b.count_below(entries[i].b); } for (u32 i = c_begin; i != c_end; ++i) { set_b.insert(entries[i].b); } c_begin = c_end; } if (sum_end - sum_begin >= bit_clear_threshold) { set_b.clear(); } else { for (u32 i = sum_begin; i != sum_end; ++i) { set_b.remove(entries[i].b); } } } // A = #(<<<) // B = #(><<) = #(>><) // C = #(=<<) // D = #(=<>) // E = #(==<) // count_c_smaller = A + 3B + 2C + 2D + E // count_bc_smaller = A + B + C const u64 C = n * solve_p2(s, 1); const u64 D = n * solve_p2(s, -1); const u64 E = n * n * solve_p1(s); // A + 3B const u64 a_plus_3b = count_c_smaller - 2 * C - 2 * D - E; // A + B const u64 a_plus_b = count_bc_smaller - C; // solve for A const u64 A = divide(3 * a_plus_b - a_plus_3b, 2); return A; } // Count (a, b, c, a', b', c') such that: // s(a) + s(b) + s(c) = s(a') + s(b') + s(c') // a < a' // b < b' // c < c' // (a, a') < (b, b') < (c, c') u64 solve_problem(const vector<i32> &s) { // Without the (a, a') < (b, b') < (c, c') constraint. u64 res = solve_p3(s); // Subtract: // E1: (a, a') == (b, b') // E2: (a, a') == (c, c') // E3: (b, b') == (c, c') res -= 3 * solve_p2(s, 2); // Add E1 & E2, E1 & E3, E2 & E3. // Subtract E1 & E2 & E3. // They are all equivalent. res += 2 * solve_p1(s); // Divide by 3! permutations of (a, a'), (b, b'), (c, c'). return divide(res, 6); } int main() { init_io(); const vector<i32> s = read_prefix_sums(); const u64 result = solve_problem(s); 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 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | // O(n^3 log n) // attempt 2 #include <bits/stdc++.h> using std::cin, std::cout, std::vector; using u8 = std::uint8_t; using u16 = std::uint16_t; using i32 = std::int32_t; using u32 = std::uint32_t; using u64 = std::uint64_t; #define REP(i, n) for (u32 i=0; i<(n); ++i) #define DEBUG(x) std::cerr << #x << " = " << x << "\n" void init_io() { cin.tie(nullptr); std::ios::sync_with_stdio(false); } u32 low_bit(const u32 x) { return x & -x; } constexpr u32 bit_clear_threshold = 20; class BIT { public: BIT(const u32 n1): counts(512, 0) {} void clear() { counts.assign(512, 0); } void insert(const u32 x) { u32 p = x + 1; REP(i, 9) { counts[p] += 1; p = (p + low_bit(p)) & 511; } counts[0] = 0; } void remove(const u32 x) { u32 p = x + 1; REP(i, 9) { counts[p] -= 1; p = (p + low_bit(p)) & 511; } counts[0] = 0; } u32 count_below(const u32 x) { u32 p = x; u32 res = 0; REP(i, 8) { res += counts[p]; p &= (p-1); } return res; } private: u32 n; // counts[x] = sum([x-low_bit(x), x)) vector<u32> counts; }; u64 divide(const u64 a, const u64 b) { assert(a % b == 0); return a / b; } vector<i32> read_prefix_sums() { u32 n; cin >> n; vector<i32> s; s.reserve(n + 1); i32 s1 = 0; s.push_back(s1); REP(i, n) { i32 a; cin >> a; s1 += a; s.push_back(s1); } return s; } vector<i32> read_prefix_sums_perf() { std::mt19937 rng; std::uniform_int_distribution<i32> dist{-20000, 20000}; u32 n = 500; vector<i32> s; s.reserve(n + 1); i32 s1 = 0; s.push_back(s1); REP(i, n) { i32 a = dist(rng); s1 += a; s.push_back(s1); } return s; } // Count (a, a') such that: // s(a) = s(a') // a < a' // O(n log n) u64 solve_p1(const vector<i32> &s) { u64 res = 0; std::map<i32, u32> counts; for (const i32 x : s) { u32 &c = counts[x]; res += c; c += 1; } return res; } // Count (a, b, a', b') such that: // mult * s(a) + s(b) = mult * s(a') + s(b') // a < a' // b < b' // O(n^2 log n) u64 solve_p2(const vector<i32> &s, const int mult) { const u32 n = s.size(); struct Entry { u32 a; u32 b; i32 sum; }; vector<Entry> entries; entries.reserve(n * n); REP(a, n) REP(b, n) { entries.push_back(Entry{a, b, mult * s[a] + s[b]}); } std::sort(entries.begin(), entries.end(), [](const Entry &e1, const Entry &e2) -> bool { return std::tuple(e1.sum, e1.b, e1.a) < std::tuple(e2.sum, e2.b, e2.a); } ); u64 result = 0; BIT set_a(n); auto sum_begin = entries.begin(); while (sum_begin != entries.end()) { auto sum_end = sum_begin + 1; while (sum_end != entries.end() && sum_end->sum == sum_begin->sum) { ++sum_end; } auto b_begin = sum_begin; while (b_begin != sum_end) { auto b_end = b_begin + 1; while (b_end != sum_end && b_end->b == b_begin->b) { ++b_end; } for (auto it = b_begin; it != b_end; ++it) { result += set_a.count_below(it->a); } for (auto it = b_begin; it != b_end; ++it) { set_a.insert(it->a); } b_begin = b_end; } for (auto it = sum_begin; it != sum_end; ++it) { set_a.remove(it->a); } sum_begin = sum_end; } return result; } // Count (a, b, c, a', b', c') such that: // s(a) + s(b) + s(c) = s(a') + s(b') + s(c') // a < a' // b < b' // c < c' // O(n^3 log n) u64 solve_p3(const vector<i32> &s) { const u32 n = s.size(); const i32 min_sum = 3 * *std::min_element(s.begin(), s.end()); const i32 max_sum = 3 * *std::max_element(s.begin(), s.end()); // max_sum - min_sum <= 3 * 500 * 20'000 = 30M // 120 MB u32 *entries_begin = new u32[max_sum - min_sum + 2]{}; // count REP(c, n) { REP(b, c) { REP(a, b) { entries_begin[s[c] + s[b] + s[a] - min_sum] += 6; } entries_begin[2 * s[c] + s[b] - min_sum] += 3; entries_begin[2 * s[b] + s[c] - min_sum] += 3; } entries_begin[3 * s[c] - min_sum] += 1; } { u32 pos = 0; for (i32 sum = min_sum; sum <= max_sum; ++sum) { u32 &e = entries_begin[sum - min_sum]; pos += e; e = pos; } entries_begin[max_sum - min_sum + 1] = pos; assert(pos == n * n * n); } struct Entry { u16 b; u16 c; }; // 500 MB // vector<Entry> entries(n * n * n); Entry *entries = new Entry[n * n * n]; REP(c_rev, n) { const u32 c = n - 1 - c_rev; REP(b, n) { REP(a, b) { u32 &e = entries_begin[s[c] + s[b] + s[a] - min_sum]; entries[--e] = Entry{u16(a), u16(c)}; entries[--e] = Entry{u16(b), u16(c)}; } { u32 &e = entries_begin[s[c] + 2 * s[b] - min_sum]; entries[--e] = Entry{u16(b), u16(c)}; } } } u64 count_c_smaller = 0; u64 count_bc_smaller = 0; BIT set_b(n); for (i32 sum = min_sum; sum <= max_sum; ++sum) { const u32 sum_begin = entries_begin[sum - min_sum]; const u32 sum_end = entries_begin[sum - min_sum + 1]; u32 c_begin = sum_begin; while (c_begin != sum_end) { const auto c = entries[c_begin].c; u32 c_end = c_begin + 1; while (c_end != sum_end && entries[c_end].c == c) { ++c_end; } count_c_smaller += u64(c_end - c_begin) * u64(c_begin - sum_begin); for (u32 i = c_begin; i != c_end; ++i) { count_bc_smaller += set_b.count_below(entries[i].b); } for (u32 i = c_begin; i != c_end; ++i) { set_b.insert(entries[i].b); } c_begin = c_end; } if (sum_end - sum_begin >= bit_clear_threshold) { set_b.clear(); } else { for (u32 i = sum_begin; i != sum_end; ++i) { set_b.remove(entries[i].b); } } } // A = #(<<<) // B = #(><<) = #(>><) // C = #(=<<) // D = #(=<>) // E = #(==<) // count_c_smaller = A + 3B + 2C + 2D + E // count_bc_smaller = A + B + C const u64 C = n * solve_p2(s, 1); const u64 D = n * solve_p2(s, -1); const u64 E = n * n * solve_p1(s); // A + 3B const u64 a_plus_3b = count_c_smaller - 2 * C - 2 * D - E; // A + B const u64 a_plus_b = count_bc_smaller - C; // solve for A const u64 A = divide(3 * a_plus_b - a_plus_3b, 2); return A; } // Count (a, b, c, a', b', c') such that: // s(a) + s(b) + s(c) = s(a') + s(b') + s(c') // a < a' // b < b' // c < c' // (a, a') < (b, b') < (c, c') u64 solve_problem(const vector<i32> &s) { // Without the (a, a') < (b, b') < (c, c') constraint. u64 res = solve_p3(s); // Subtract: // E1: (a, a') == (b, b') // E2: (a, a') == (c, c') // E3: (b, b') == (c, c') res -= 3 * solve_p2(s, 2); // Add E1 & E2, E1 & E3, E2 & E3. // Subtract E1 & E2 & E3. // They are all equivalent. res += 2 * solve_p1(s); // Divide by 3! permutations of (a, a'), (b, b'), (c, c'). return divide(res, 6); } int main() { init_io(); const vector<i32> s = read_prefix_sums(); const u64 result = solve_problem(s); cout << result << "\n"; } |