#define make_pair mp #define emplace_back pb #include <bits/stdc++.h> using namespace std; mt19937 mt_rand(time(0)); const int N = 3e5 + 5, MOD = 1e9 + 7, D = 1e6 + 5, pods = (1<<19) - 1; int n, tab[N], d[D][2]; map<int, int> m; void add(int x, int id, int val) { x += pods; while(x != 0) { d[x][id] = (d[x][id] + val) % MOD; x /= 2; } } int sum(int x, int y, int id) { if(x > y) return 0; x += pods; y += pods; int res = d[x][id]; if(x != y) res = (res + d[y][id]) % MOD; while(x / 2 != y / 2) { if(x % 2 == 0) res = (res + d[x+1][id]) % MOD; if(y % 2 == 1) res = (res + d[y-1][id]) % MOD; x /= 2; y /= 2; } return res; } int main() { scanf("%d", &n); m[0] = 0; for(int i=1;i<=n;i++) { scanf("%d", &tab[i]); tab[i] = (tab[i-1] + tab[i]) % MOD; m[tab[i]] = 0; } int cnt = 0; for(auto k : m) { cnt++; m[k.first] += cnt; } add(m[0], 0, 1); for(int i=1;i<=n;i++) { int val = 0; int mm = m[tab[i]]; if(tab[i] % 2 == 0) { val = (sum(1, mm, 0) + sum(mm+1,cnt,1)) % MOD; add(mm, 0, val); } else { val = (sum(1, mm, 1) + sum(mm+1,cnt,0)) % MOD; add(mm, 1, val); } if(i == n) printf("%d\n", val); } 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 | #define make_pair mp #define emplace_back pb #include <bits/stdc++.h> using namespace std; mt19937 mt_rand(time(0)); const int N = 3e5 + 5, MOD = 1e9 + 7, D = 1e6 + 5, pods = (1<<19) - 1; int n, tab[N], d[D][2]; map<int, int> m; void add(int x, int id, int val) { x += pods; while(x != 0) { d[x][id] = (d[x][id] + val) % MOD; x /= 2; } } int sum(int x, int y, int id) { if(x > y) return 0; x += pods; y += pods; int res = d[x][id]; if(x != y) res = (res + d[y][id]) % MOD; while(x / 2 != y / 2) { if(x % 2 == 0) res = (res + d[x+1][id]) % MOD; if(y % 2 == 1) res = (res + d[y-1][id]) % MOD; x /= 2; y /= 2; } return res; } int main() { scanf("%d", &n); m[0] = 0; for(int i=1;i<=n;i++) { scanf("%d", &tab[i]); tab[i] = (tab[i-1] + tab[i]) % MOD; m[tab[i]] = 0; } int cnt = 0; for(auto k : m) { cnt++; m[k.first] += cnt; } add(m[0], 0, 1); for(int i=1;i<=n;i++) { int val = 0; int mm = m[tab[i]]; if(tab[i] % 2 == 0) { val = (sum(1, mm, 0) + sum(mm+1,cnt,1)) % MOD; add(mm, 0, val); } else { val = (sum(1, mm, 1) + sum(mm+1,cnt,0)) % MOD; add(mm, 1, val); } if(i == n) printf("%d\n", val); } return 0; } |