#include <bits/stdc++.h> using namespace std; const int mod=1e9+7; struct Node { int sum[2]; Node *left; Node *right; }; void Extend(Node *v) { v->left=new Node(); v->right=new Node(); } void Update(Node *v, int bl, int br, int p, int w) { v->sum[p&1]+=w; if(v->sum[p&1]>=mod)v->sum[p&1]-=mod; if(bl==br)return; int bm=(bl+br)/2; if(v->left==nullptr)Extend(v); if(p<=bm)Update(v->left, bl, bm, p, w); else Update(v->right, bm+1, br, p, w); } int Query(Node *v, int bl, int br, int l, int r, char type) { if(bl>r || br<l)return 0; if(bl>=l && br<=r)return v->sum[type]; int bm=(bl+br)/2; if(v->left==nullptr)Extend(v); int res=Query(v->left, bl, bm, l, r, type) + Query(v->right, bm+1, br, l, r, type); if(res>=mod)res-=mod; return res; } int main() { ios_base::sync_with_stdio(0); Node *root=new Node(); Update(root, 0, mod-1, 0, 1); int n; cin>>n; int val=0; int sum=0; for(int i=0;i<n;i++) { int a; cin>>a; sum+=a; if(sum>=mod)sum-=mod; val=Query(root, 0, mod-1, 0, sum, sum&1); if(sum+1<mod)val+=Query(root, 0, mod-1, sum+1, mod-1, (sum+1)&1); if(val>=mod)val-=mod; Update(root, 0, mod-1, sum, val); } cout<<val<<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 | #include <bits/stdc++.h> using namespace std; const int mod=1e9+7; struct Node { int sum[2]; Node *left; Node *right; }; void Extend(Node *v) { v->left=new Node(); v->right=new Node(); } void Update(Node *v, int bl, int br, int p, int w) { v->sum[p&1]+=w; if(v->sum[p&1]>=mod)v->sum[p&1]-=mod; if(bl==br)return; int bm=(bl+br)/2; if(v->left==nullptr)Extend(v); if(p<=bm)Update(v->left, bl, bm, p, w); else Update(v->right, bm+1, br, p, w); } int Query(Node *v, int bl, int br, int l, int r, char type) { if(bl>r || br<l)return 0; if(bl>=l && br<=r)return v->sum[type]; int bm=(bl+br)/2; if(v->left==nullptr)Extend(v); int res=Query(v->left, bl, bm, l, r, type) + Query(v->right, bm+1, br, l, r, type); if(res>=mod)res-=mod; return res; } int main() { ios_base::sync_with_stdio(0); Node *root=new Node(); Update(root, 0, mod-1, 0, 1); int n; cin>>n; int val=0; int sum=0; for(int i=0;i<n;i++) { int a; cin>>a; sum+=a; if(sum>=mod)sum-=mod; val=Query(root, 0, mod-1, 0, sum, sum&1); if(sum+1<mod)val+=Query(root, 0, mod-1, sum+1, mod-1, (sum+1)&1); if(val>=mod)val-=mod; Update(root, 0, mod-1, sum, val); } cout<<val<<endl; } |