#include <iostream> #include <unordered_map> #include <vector> #include <algorithm> const long MAXT = 1l<<20; const long MOD = 1000000007; const int MAX = 300009; class RT { public: RT(){ for (int i=0;i<2*MAXT;++i) { H[i].v[0] = 0; H[i].v[1] = 0; } } void Add(long p, int parity, long v) { p += MAXT; auto &hv = H[p].v[parity]; hv = (hv+v)%MOD; p /= 2; while (p > 0) { //H[p].v[parity] = (H[2*p].v[parity] + H[2*p+1].v[parity]) % MOD; auto &hv = H[p].v[parity]; hv = (hv+v)%MOD; p /= 2; } } long Query(long l, long r, int parity) { if (l>r) return 0; l += MAXT; r += MAXT; long s = H[l].v[parity]; if (l!=r) s += H[r].v[parity]; while (l/2 < r/2) { if (!(l&1)) s += H[l+1].v[parity]; if (r&1) s += H[r-1].v[parity]; l /= 2; r /= 2; } return s % MOD; } private: struct Node { uint32_t v[2]; }; //std::unordered_map<uint32_t, Node> H; Node H[2*MAXT]; } rt; long A[MAX]; int main() { std::ios_base::sync_with_stdio(0); long n; std::cin >> n; long o = 0; std::vector<uint32_t> V = {0, MOD-1}; for (int i=0;i<n;++i) { std::cin >> A[i]; // A[i] = i*1337l%MOD; o = (o+A[i]) % MOD; long x = MOD-o; V.push_back(x-1); V.push_back(x); } std::sort(V.begin(), V.end()); V.erase(std::unique(V.begin(),V.end()), V.end()); std::unordered_map<uint32_t, uint32_t> M; for (int i=0;i<V.size();++i) M[V[i]] = i; o = 0; long s = 0; for (int i=0;i<n;++i) { o = (o+A[i]) % MOD; long x = MOD-o; s = ( rt.Query(M[0], M[x-1], o%2) + rt.Query(M[x], M[MOD-1], 1-o%2) + 1-o%2) % MOD; rt.Add(M[x%MOD], x%MOD%2, s); } std::cout << s << std::endl; 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include <iostream> #include <unordered_map> #include <vector> #include <algorithm> const long MAXT = 1l<<20; const long MOD = 1000000007; const int MAX = 300009; class RT { public: RT(){ for (int i=0;i<2*MAXT;++i) { H[i].v[0] = 0; H[i].v[1] = 0; } } void Add(long p, int parity, long v) { p += MAXT; auto &hv = H[p].v[parity]; hv = (hv+v)%MOD; p /= 2; while (p > 0) { //H[p].v[parity] = (H[2*p].v[parity] + H[2*p+1].v[parity]) % MOD; auto &hv = H[p].v[parity]; hv = (hv+v)%MOD; p /= 2; } } long Query(long l, long r, int parity) { if (l>r) return 0; l += MAXT; r += MAXT; long s = H[l].v[parity]; if (l!=r) s += H[r].v[parity]; while (l/2 < r/2) { if (!(l&1)) s += H[l+1].v[parity]; if (r&1) s += H[r-1].v[parity]; l /= 2; r /= 2; } return s % MOD; } private: struct Node { uint32_t v[2]; }; //std::unordered_map<uint32_t, Node> H; Node H[2*MAXT]; } rt; long A[MAX]; int main() { std::ios_base::sync_with_stdio(0); long n; std::cin >> n; long o = 0; std::vector<uint32_t> V = {0, MOD-1}; for (int i=0;i<n;++i) { std::cin >> A[i]; // A[i] = i*1337l%MOD; o = (o+A[i]) % MOD; long x = MOD-o; V.push_back(x-1); V.push_back(x); } std::sort(V.begin(), V.end()); V.erase(std::unique(V.begin(),V.end()), V.end()); std::unordered_map<uint32_t, uint32_t> M; for (int i=0;i<V.size();++i) M[V[i]] = i; o = 0; long s = 0; for (int i=0;i<n;++i) { o = (o+A[i]) % MOD; long x = MOD-o; s = ( rt.Query(M[0], M[x-1], o%2) + rt.Query(M[x], M[MOD-1], 1-o%2) + 1-o%2) % MOD; rt.Add(M[x%MOD], x%MOD%2, s); } std::cout << s << std::endl; return 0; } |