#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> /* #pragma GCC target("avx2") #pragma GCC optimization("Ofast") #pragma GCC optimization("unroll-loops") */ using namespace __gnu_pbds; using namespace std; #define mp make_pair #define eb emplace_back #define pb push_back #define e1 first #define e2 second #define vt vector #define maxi(x,y) x=max(x,y) #define mini(x,y) x=min(x,y) #define size(x) (int)x.size() #define all(r) begin(r),end(r) #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define cx real() #define cy imag() #define precision(x) setprecision(x)<<fixed #define time chrono::high_resolution_clock().now().time_since_epoch().count() #ifndef DEBUG #define DEBUG false #endif #define dbg if(DEBUG) typedef long long ll; typedef long double ld; typedef complex<long double> cplx; typedef unsigned int uint; typedef unsigned long long ull; typedef vt<int> vi; typedef vt<vi> vvi; typedef vt<bool> vb; typedef vt<vb> vvb; typedef vt<char> vc; typedef vt<vc> vvc; typedef vt<string> vs; typedef vt<vs> vvs; typedef vt<ll> vll; typedef vt<vll> vvll; typedef vt<ld> vld; typedef vt<vld> vvld; typedef vt<cplx> vcplx; typedef vt<vcplx> vvcplx; template<typename T> using ordset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; template<typename T1,typename T2> ostream& operator<<(ostream &os,pair<T1,T2>& VAR){ os<<"("<<VAR.e1<<","<<VAR.e2<<")"; return os; } template<typename T> ostream& operator<<(ostream& os,vt<T>& VAR){ os<<"["; for(int i=0;i<size(VAR)-1;i++) os<<VAR[i]<<","; if(size(VAR)) os<<VAR.back(); os<<"]"; return os; } template<typename T> istream& operator>>(istream& is,complex<T>& VAR){ T value; is>>value; VAR.real(value); is>>value; VAR.imag(value); return is; } const bool TESTS=false; const int mod=1000'000'000+7; void add(int& a,int b){ a+=b; if(a>=mod) a-=mod; } int sum(int a,int b){ a+=b; if(a>=mod) a-=mod; return a; } void mul(int& a,ll b){ a=(a*b)%mod; } int prod(int a,ll b){ return (a*b)%mod; } int neg(int a){ if(a==0) return 0; return mod-a; } const int MAXN=3e5+10; int t[2][2][MAXN]; int total[2][2]; void update(int p,int k,int x,int y){ add(total[x][y],k); for(;p<MAXN;p=(p|(p-1))+1) add(t[x][y][p],k); } int query(int p,int x,int y){ int ans=0; for(;p;p=(p&(p-1))) add(ans,t[x][y][p]); return ans; } void solve(){ int n; cin>>n; vi a(n),b(n),c(n),p(n); vt<pair<int,int>> vals(n); ll tot=0; for(int i=0;i<n;i++){ cin>>a[i]; tot+=a[i]; b[i]=tot%2; vals[i]={tot%mod,i}; c[i]=(tot/mod)%2; } sort(all(vals)); int prv=0; int cnt=1; for(auto& x:vals){ if(x.e1!=prv){ prv=x.e1; cnt++; } p[x.e2]=cnt; } update(1,1,0,0); int odp=1; for(int i=0;i<n;i++){ int x=b[i]; int y=c[i]; int lwb00=query(p[i],x,y); int lwb11=query(p[i],x^1,y^1); int upb00=sum(total[x][y^1],neg(query(p[i],x,y^1))); int upb11=sum(total[x^1][y],neg(query(p[i],x^1,y))); odp=sum(sum(lwb00,lwb11),sum(upb00,upb11)); update(p[i],odp,x,y); } cout<<odp<<'\n'; } int32_t main(){ fastio; if(TESTS){ int TEST_COUNT; cin>>TEST_COUNT; for(int i=0;i<TEST_COUNT;i++) solve(); } else solve(); 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 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> /* #pragma GCC target("avx2") #pragma GCC optimization("Ofast") #pragma GCC optimization("unroll-loops") */ using namespace __gnu_pbds; using namespace std; #define mp make_pair #define eb emplace_back #define pb push_back #define e1 first #define e2 second #define vt vector #define maxi(x,y) x=max(x,y) #define mini(x,y) x=min(x,y) #define size(x) (int)x.size() #define all(r) begin(r),end(r) #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define cx real() #define cy imag() #define precision(x) setprecision(x)<<fixed #define time chrono::high_resolution_clock().now().time_since_epoch().count() #ifndef DEBUG #define DEBUG false #endif #define dbg if(DEBUG) typedef long long ll; typedef long double ld; typedef complex<long double> cplx; typedef unsigned int uint; typedef unsigned long long ull; typedef vt<int> vi; typedef vt<vi> vvi; typedef vt<bool> vb; typedef vt<vb> vvb; typedef vt<char> vc; typedef vt<vc> vvc; typedef vt<string> vs; typedef vt<vs> vvs; typedef vt<ll> vll; typedef vt<vll> vvll; typedef vt<ld> vld; typedef vt<vld> vvld; typedef vt<cplx> vcplx; typedef vt<vcplx> vvcplx; template<typename T> using ordset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; template<typename T1,typename T2> ostream& operator<<(ostream &os,pair<T1,T2>& VAR){ os<<"("<<VAR.e1<<","<<VAR.e2<<")"; return os; } template<typename T> ostream& operator<<(ostream& os,vt<T>& VAR){ os<<"["; for(int i=0;i<size(VAR)-1;i++) os<<VAR[i]<<","; if(size(VAR)) os<<VAR.back(); os<<"]"; return os; } template<typename T> istream& operator>>(istream& is,complex<T>& VAR){ T value; is>>value; VAR.real(value); is>>value; VAR.imag(value); return is; } const bool TESTS=false; const int mod=1000'000'000+7; void add(int& a,int b){ a+=b; if(a>=mod) a-=mod; } int sum(int a,int b){ a+=b; if(a>=mod) a-=mod; return a; } void mul(int& a,ll b){ a=(a*b)%mod; } int prod(int a,ll b){ return (a*b)%mod; } int neg(int a){ if(a==0) return 0; return mod-a; } const int MAXN=3e5+10; int t[2][2][MAXN]; int total[2][2]; void update(int p,int k,int x,int y){ add(total[x][y],k); for(;p<MAXN;p=(p|(p-1))+1) add(t[x][y][p],k); } int query(int p,int x,int y){ int ans=0; for(;p;p=(p&(p-1))) add(ans,t[x][y][p]); return ans; } void solve(){ int n; cin>>n; vi a(n),b(n),c(n),p(n); vt<pair<int,int>> vals(n); ll tot=0; for(int i=0;i<n;i++){ cin>>a[i]; tot+=a[i]; b[i]=tot%2; vals[i]={tot%mod,i}; c[i]=(tot/mod)%2; } sort(all(vals)); int prv=0; int cnt=1; for(auto& x:vals){ if(x.e1!=prv){ prv=x.e1; cnt++; } p[x.e2]=cnt; } update(1,1,0,0); int odp=1; for(int i=0;i<n;i++){ int x=b[i]; int y=c[i]; int lwb00=query(p[i],x,y); int lwb11=query(p[i],x^1,y^1); int upb00=sum(total[x][y^1],neg(query(p[i],x,y^1))); int upb11=sum(total[x^1][y],neg(query(p[i],x^1,y))); odp=sum(sum(lwb00,lwb11),sum(upb00,upb11)); update(p[i],odp,x,y); } cout<<odp<<'\n'; } int32_t main(){ fastio; if(TESTS){ int TEST_COUNT; cin>>TEST_COUNT; for(int i=0;i<TEST_COUNT;i++) solve(); } else solve(); return 0; } |