#include <bits/stdc++.h> using namespace std; const uint P = 1e9 + 7; struct fenwick_tree { size_t n; vector<uint64_t> F; uint64_t sum = 0; fenwick_tree(size_t m) : n(m), F(n+1) {} void delta(size_t p, uint64_t v) { sum += v; p++; while(p <= n) F[p] += v, p += p & -p; } uint64_t get_prefix(size_t p) { uint64_t r = 0; while(p) r += F[p], p &= p - 1; return r; } uint64_t get_suffix(size_t p) { return sum - get_prefix(p); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); size_t n; cin >> n; vector<uint> A(n); for(auto& a : A) cin >> a; vector<uint64_t> S(n+1); for(size_t i = 0; i < n; i++) S[i+1] = S[i] + A[i]; map<uint, size_t> scale; scale[UINT_MAX]; for(size_t i = 0; i <= n; i++) { const auto a = S[i] % P; const auto x = a == 0 ? 0 : P - a; scale[x]; } size_t f = 0; for(auto& [k, v] : scale) v = f++; fenwick_tree xs[2] = {n, n}; xs[0].delta(0, 1); uint64_t result; for(size_t i = 1; i <= n; i++) { const auto a = S[i] % P; const size_t l = scale.lower_bound(P - a)->second; uint64_t q = xs[a&1].get_prefix(l) + xs[(1^a)&1].get_suffix(l); const auto x = a == 0 ? 0 : P - a; xs[x % 2].delta(scale[x], result = q % P); } cout << result << endl; }
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 | #include <bits/stdc++.h> using namespace std; const uint P = 1e9 + 7; struct fenwick_tree { size_t n; vector<uint64_t> F; uint64_t sum = 0; fenwick_tree(size_t m) : n(m), F(n+1) {} void delta(size_t p, uint64_t v) { sum += v; p++; while(p <= n) F[p] += v, p += p & -p; } uint64_t get_prefix(size_t p) { uint64_t r = 0; while(p) r += F[p], p &= p - 1; return r; } uint64_t get_suffix(size_t p) { return sum - get_prefix(p); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); size_t n; cin >> n; vector<uint> A(n); for(auto& a : A) cin >> a; vector<uint64_t> S(n+1); for(size_t i = 0; i < n; i++) S[i+1] = S[i] + A[i]; map<uint, size_t> scale; scale[UINT_MAX]; for(size_t i = 0; i <= n; i++) { const auto a = S[i] % P; const auto x = a == 0 ? 0 : P - a; scale[x]; } size_t f = 0; for(auto& [k, v] : scale) v = f++; fenwick_tree xs[2] = {n, n}; xs[0].delta(0, 1); uint64_t result; for(size_t i = 1; i <= n; i++) { const auto a = S[i] % P; const size_t l = scale.lower_bound(P - a)->second; uint64_t q = xs[a&1].get_prefix(l) + xs[(1^a)&1].get_suffix(l); const auto x = a == 0 ? 0 : P - a; xs[x % 2].delta(scale[x], result = q % P); } cout << result << endl; } |