#include <bits/stdc++.h> using namespace std; const int maxn = 300000 + 7; const int q = 1e9 + 7; typedef long long ll; template <typename T,typename U> std::pair<T,U> operator+(const std::pair<T,U> & l,const std::pair<T,U> & r) { return {l.first+r.first,l.second+r.second}; } /*struct node{ ll prefix; ll wynik; };*/ pair<ll,ll> tree[maxn*2]; int n; void build(pair<ll,int> tab[]) { for(int i=0;i<n;i++) { /*if(tab[i].first%2 == 0) { tree[n+i].first = tab[i].first; tree[n+i].second = 0; } else { tree[n+i].second = tab[i].first; tree[n+i].first = 0; }*/ tree[n+i].first = 0; tree[n+i].second = 0; } for(int i=n-1;i>0;i--) { tree[i] = tree[2*i] + tree[2*i | 1]; } } void update(int p,int value, bool nieparz) { if(!nieparz) { tree[p+n].first = value; } else { tree[p+n].second = value; } p = p+n; for(int i=p;i>1;i/=2) { tree[i/2] = tree[i] + tree[i^1]; } } //daje sume ale bez r ll query(int l,int r, bool nieparz) { //cout<<"query: "<<l<<' '<<r<<' '<<nieparz<<" wyn: "; ll wyn = 0; for(l += n, r += n;l < r; l/=2 , r/=2) { if(l&1) { if(!nieparz) wyn += tree[l++].first; else wyn += tree[l++].second; wyn %= q; } if(r&1) { if(!nieparz) wyn += tree[--r].first; else wyn += tree[--r].second; wyn %= q; } } //cout<<wyn<<'\n'; return wyn; } void wypisz(int x) { cout<<'\n'; cout<<"=========================\n"; for(int i=1;i<2*n;i++) { if(i == n) cout<<'\n'; if(i == x+n) cout<<" "; cout<<"{ "<<tree[i].first<<", "<<tree[i].second<<" } \n"; } cout<<"=========================\n"; cout<<'\n'; /*int ile = n, gleb = n; while(ile > 0) { for(int i=0;i<n;i++) { cout<<"{ "<<tree[gleb+i].first<<", "<<tree[gleb+i].second<<" } , "; } ile /= 2; }*/ } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; int tab[n]; for(int i=0;i<n;i++) { cin>>tab[i]; } n++; pair<ll,int> suma_prefix[n]; //suma_prefix[0] = {tab[0],0}; ll suma = 0; for(int i=0;i<n;i++) { suma_prefix[i] = {suma,i}; if(i==n-1)break; suma += tab[i]; suma %= q; } sort(suma_prefix,suma_prefix+n); int index[n]; for(int i=0;i<n;i++) { index[suma_prefix[i].second] = i; } /*for(int i=0;i<n;i++) { cout<<i<<": suma: "<<suma_prefix[i].first<<" index: "<<suma_prefix[i].second<<'\n'; }*/ /*ll wyn[n+1]; fill_n(wyn+1,n,0); wyn[0] = 1;*/ ll wyn = 0; for(int i=0;i<n;i++) { //wypisz(index[i]); if(i==0) { update(index[0],1,0); continue; } if(suma_prefix[index[i]].first%2==0) { //update(i,wyn[i]); wyn = query(0,index[i]+1,0); wyn += query(index[i]+1,n,1); wyn %= q; } else { wyn = query(0,index[i]+1,1); wyn += query(index[i]+1,n,0); wyn %= q; //update(i,wyn[i]); } bool nieparz = false; if(suma_prefix[index[i]].first%2 == 1)nieparz = true; update(index[i],wyn,nieparz); //cout<<wyn<<'\n'; } cout<<wyn<<'\n'; //cout<<wyn[n]<<'\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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | #include <bits/stdc++.h> using namespace std; const int maxn = 300000 + 7; const int q = 1e9 + 7; typedef long long ll; template <typename T,typename U> std::pair<T,U> operator+(const std::pair<T,U> & l,const std::pair<T,U> & r) { return {l.first+r.first,l.second+r.second}; } /*struct node{ ll prefix; ll wynik; };*/ pair<ll,ll> tree[maxn*2]; int n; void build(pair<ll,int> tab[]) { for(int i=0;i<n;i++) { /*if(tab[i].first%2 == 0) { tree[n+i].first = tab[i].first; tree[n+i].second = 0; } else { tree[n+i].second = tab[i].first; tree[n+i].first = 0; }*/ tree[n+i].first = 0; tree[n+i].second = 0; } for(int i=n-1;i>0;i--) { tree[i] = tree[2*i] + tree[2*i | 1]; } } void update(int p,int value, bool nieparz) { if(!nieparz) { tree[p+n].first = value; } else { tree[p+n].second = value; } p = p+n; for(int i=p;i>1;i/=2) { tree[i/2] = tree[i] + tree[i^1]; } } //daje sume ale bez r ll query(int l,int r, bool nieparz) { //cout<<"query: "<<l<<' '<<r<<' '<<nieparz<<" wyn: "; ll wyn = 0; for(l += n, r += n;l < r; l/=2 , r/=2) { if(l&1) { if(!nieparz) wyn += tree[l++].first; else wyn += tree[l++].second; wyn %= q; } if(r&1) { if(!nieparz) wyn += tree[--r].first; else wyn += tree[--r].second; wyn %= q; } } //cout<<wyn<<'\n'; return wyn; } void wypisz(int x) { cout<<'\n'; cout<<"=========================\n"; for(int i=1;i<2*n;i++) { if(i == n) cout<<'\n'; if(i == x+n) cout<<" "; cout<<"{ "<<tree[i].first<<", "<<tree[i].second<<" } \n"; } cout<<"=========================\n"; cout<<'\n'; /*int ile = n, gleb = n; while(ile > 0) { for(int i=0;i<n;i++) { cout<<"{ "<<tree[gleb+i].first<<", "<<tree[gleb+i].second<<" } , "; } ile /= 2; }*/ } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; int tab[n]; for(int i=0;i<n;i++) { cin>>tab[i]; } n++; pair<ll,int> suma_prefix[n]; //suma_prefix[0] = {tab[0],0}; ll suma = 0; for(int i=0;i<n;i++) { suma_prefix[i] = {suma,i}; if(i==n-1)break; suma += tab[i]; suma %= q; } sort(suma_prefix,suma_prefix+n); int index[n]; for(int i=0;i<n;i++) { index[suma_prefix[i].second] = i; } /*for(int i=0;i<n;i++) { cout<<i<<": suma: "<<suma_prefix[i].first<<" index: "<<suma_prefix[i].second<<'\n'; }*/ /*ll wyn[n+1]; fill_n(wyn+1,n,0); wyn[0] = 1;*/ ll wyn = 0; for(int i=0;i<n;i++) { //wypisz(index[i]); if(i==0) { update(index[0],1,0); continue; } if(suma_prefix[index[i]].first%2==0) { //update(i,wyn[i]); wyn = query(0,index[i]+1,0); wyn += query(index[i]+1,n,1); wyn %= q; } else { wyn = query(0,index[i]+1,1); wyn += query(index[i]+1,n,0); wyn %= q; //update(i,wyn[i]); } bool nieparz = false; if(suma_prefix[index[i]].first%2 == 1)nieparz = true; update(index[i],wyn,nieparz); //cout<<wyn<<'\n'; } cout<<wyn<<'\n'; //cout<<wyn[n]<<'\n'; } |