#include<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; long long tab[300009]; set<int> sums; long long fenwick[2][2][600009], fen_sum[2][2]; const long long M = 1e9 + 7; map<long long, long long> przen; // -----------Fenwick tree implementation based on Wikipedia------------ long long prefix_sum(int i, int j, long long b) { long long su = fenwick[i][j][0] % M; for(int k=b; k>0; k=k-(k&(-k))) { su = (su + fenwick[i][j][k]) % M; } return su; } void add(int i, int j, long long b, long long w) { fen_sum[i][j] = (fen_sum[i][j] + w) % M; if(b == 0) { fenwick[i][j][b] = (fenwick[i][j][b] + w) % M; return; } for(int k=b; k<524288; k=k+(k&(-k))) { fenwick[i][j][k] = (fenwick[i][j][k] + w) % M; } } // --------------------------------------------------------------------- int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, a = 0, b = 0, c, w, w1, w2, w3, w4; cin>>n; for(int i=1; i<=n; i++) cin>>tab[i]; sums.insert(0); for(int i=1; i<=n; i++) { b += tab[i]; while(b >= M) { b -= M; a++; } sums.insert(b); } a = 0; for(auto s : sums) { przen[s] = a; a++; } a = b = 0; fenwick[0][0][0] = 1; fen_sum[0][0] = 1; for(int i=1; i<=n; i++) { b += tab[i]; while(b >= M) { b -= M; a++; } c = przen[b]; w1 = prefix_sum(a%2, b%2, c); w2 = prefix_sum(1-(a%2), 1-(b%2), c); w3 = prefix_sum(a%2, 1-(b%2), c); w4 = prefix_sum(1-(a%2), b%2, c); w2 = fen_sum[1-(a%2)][1-(b%2)] - w2; w3 = fen_sum[a%2][1-(b%2)] - w3; while(w2 < 0) w2 += M; while(w3 < 0) w3 += M; w1 = (w1 + w2) % M; w3 = (w3 + w4) % M; w = (w1 + w3) % M; add(a%2, b%2, c, w); } c = przen[b]; cout<<w % M; 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 93 | #include<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; long long tab[300009]; set<int> sums; long long fenwick[2][2][600009], fen_sum[2][2]; const long long M = 1e9 + 7; map<long long, long long> przen; // -----------Fenwick tree implementation based on Wikipedia------------ long long prefix_sum(int i, int j, long long b) { long long su = fenwick[i][j][0] % M; for(int k=b; k>0; k=k-(k&(-k))) { su = (su + fenwick[i][j][k]) % M; } return su; } void add(int i, int j, long long b, long long w) { fen_sum[i][j] = (fen_sum[i][j] + w) % M; if(b == 0) { fenwick[i][j][b] = (fenwick[i][j][b] + w) % M; return; } for(int k=b; k<524288; k=k+(k&(-k))) { fenwick[i][j][k] = (fenwick[i][j][k] + w) % M; } } // --------------------------------------------------------------------- int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, a = 0, b = 0, c, w, w1, w2, w3, w4; cin>>n; for(int i=1; i<=n; i++) cin>>tab[i]; sums.insert(0); for(int i=1; i<=n; i++) { b += tab[i]; while(b >= M) { b -= M; a++; } sums.insert(b); } a = 0; for(auto s : sums) { przen[s] = a; a++; } a = b = 0; fenwick[0][0][0] = 1; fen_sum[0][0] = 1; for(int i=1; i<=n; i++) { b += tab[i]; while(b >= M) { b -= M; a++; } c = przen[b]; w1 = prefix_sum(a%2, b%2, c); w2 = prefix_sum(1-(a%2), 1-(b%2), c); w3 = prefix_sum(a%2, 1-(b%2), c); w4 = prefix_sum(1-(a%2), b%2, c); w2 = fen_sum[1-(a%2)][1-(b%2)] - w2; w3 = fen_sum[a%2][1-(b%2)] - w3; while(w2 < 0) w2 += M; while(w3 < 0) w3 += M; w1 = (w1 + w2) % M; w3 = (w3 + w4) % M; w = (w1 + w3) % M; add(a%2, b%2, c, w); } c = przen[b]; cout<<w % M; return 0; } |