#include <bits/stdc++.h> using namespace std; const int N = 1048576; const int M = 1000000007; int n,l,a; int t[300005]; int dp[300005]; int tree1[2097152]; int tree2[2097152]; vector <int> v; map <int,int> m; void add1(int w, int val) { w+=N-1; while (w>0) { tree1[w]+=val; tree1[w]%=M; w/=2; } } void add2(int w, int val) { w+=N-1; while (w>0) { tree2[w]+=val; tree2[w]%=M; w/=2; } } int query1(int w, int p, int k, int x, int y) { if (x>k || y<p) return 0; if (x<=p && k<=y) return tree1[w]; return (query1(w*2,p,(p+k)/2,x,y)+query1(w*2+1,(p+k)/2+1,k,x,y))%M; } int query2(int w, int p, int k, int x, int y) { if (x>k || y<p) return 0; if (x<=p && k<=y) return tree2[w]; return (query2(w*2,p,(p+k)/2,x,y)+query2(w*2+1,(p+k)/2+1,k,x,y))%M; } void wypisz() { cout << endl << endl; for (int i=1; i<2*N; i++) { cout << i << " " << tree2[i] << endl; } cout << endl; } int main() { scanf("%d",&n); for (int i=1; i<=n; i++) { scanf("%d",&t[i]); t[i]+=t[i-1]; t[i]%=M; //cout << t[i] << " "; v.push_back(t[i]); } //cout << endl; v.push_back(0); sort(v.begin(),v.end()); for (auto i : v) { if (m[i]) continue; l++; if (l%2 != i%2) l++; m[i]=l; } add2(2,1); for (int i=1; i<=n; i++) { a=m[t[i]]; //cout << a << endl; if (a&1) { dp[i]=query1(1,1,N,1,a)+query2(1,1,N,a,l); dp[i]%=M; add1(a,dp[i]); } else { dp[i]=query2(1,1,N,1,a)+query1(1,1,N,a,l); dp[i]%=M; add2(a,dp[i]); } //wypisz(); //cout << dp[i] << endl; } printf("%d\n",dp[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 | #include <bits/stdc++.h> using namespace std; const int N = 1048576; const int M = 1000000007; int n,l,a; int t[300005]; int dp[300005]; int tree1[2097152]; int tree2[2097152]; vector <int> v; map <int,int> m; void add1(int w, int val) { w+=N-1; while (w>0) { tree1[w]+=val; tree1[w]%=M; w/=2; } } void add2(int w, int val) { w+=N-1; while (w>0) { tree2[w]+=val; tree2[w]%=M; w/=2; } } int query1(int w, int p, int k, int x, int y) { if (x>k || y<p) return 0; if (x<=p && k<=y) return tree1[w]; return (query1(w*2,p,(p+k)/2,x,y)+query1(w*2+1,(p+k)/2+1,k,x,y))%M; } int query2(int w, int p, int k, int x, int y) { if (x>k || y<p) return 0; if (x<=p && k<=y) return tree2[w]; return (query2(w*2,p,(p+k)/2,x,y)+query2(w*2+1,(p+k)/2+1,k,x,y))%M; } void wypisz() { cout << endl << endl; for (int i=1; i<2*N; i++) { cout << i << " " << tree2[i] << endl; } cout << endl; } int main() { scanf("%d",&n); for (int i=1; i<=n; i++) { scanf("%d",&t[i]); t[i]+=t[i-1]; t[i]%=M; //cout << t[i] << " "; v.push_back(t[i]); } //cout << endl; v.push_back(0); sort(v.begin(),v.end()); for (auto i : v) { if (m[i]) continue; l++; if (l%2 != i%2) l++; m[i]=l; } add2(2,1); for (int i=1; i<=n; i++) { a=m[t[i]]; //cout << a << endl; if (a&1) { dp[i]=query1(1,1,N,1,a)+query2(1,1,N,a,l); dp[i]%=M; add1(a,dp[i]); } else { dp[i]=query2(1,1,N,1,a)+query1(1,1,N,a,l); dp[i]%=M; add2(a,dp[i]); } //wypisz(); //cout << dp[i] << endl; } printf("%d\n",dp[n]); } |