#include <bits/stdc++.h> using namespace std; int n; long long p=10e8+7; struct maciek{long long r; int ind; int scal;}; maciek pref[300200]; bool skal[300200]; bool majonez1(maciek A, maciek B) { return A.r<B.r; } bool majonez2(maciek A, maciek B) { return A.ind<B.ind; } const int baza=1<<19; long long d0[baza * 2]; void update0(int poz, long long war) { while(poz > 0) { d0[poz] =(d0[poz]+ war)%p; poz /= 2; } return; } long long czytaj0(int targetleft, int targetright, int currentleft, int currentright, int poz) { if(targetleft <= currentleft && currentright <= targetright) { return d0[poz]; } long long wyn = 0; if(targetleft <= (currentleft + currentright) / 2) { wyn =(wyn+ czytaj0(targetleft, targetright, currentleft, (currentleft + currentright) / 2, poz * 2))%p; } if((currentleft + currentright) / 2 < targetright) { wyn =(wyn+ czytaj0(targetleft, targetright, (currentleft + currentright) / 2 + 1, currentright, poz * 2 + 1))%p; } return wyn; } long long d1[baza * 2]; void update1(int poz, long long war) { while(poz > 0) { d1[poz] += war; d1[poz]=d1[poz]%p; poz /= 2; } return; } long long czytaj1(int targetleft, int targetright, int currentleft, int currentright, int poz) { if(targetleft <= currentleft && currentright <= targetright) { return d1[poz]; } long long wyn = 0; if(targetleft <= (currentleft + currentright) / 2) { wyn =(wyn+ czytaj1(targetleft, targetright, currentleft, (currentleft + currentright) / 2, poz * 2))%p; } if((currentleft + currentright) / 2 < targetright) { wyn =(wyn+ czytaj1(targetleft, targetright, (currentleft + currentright) / 2 + 1, currentright, poz * 2 + 1))%p; } return wyn; } int main() { ios_base::sync_with_stdio(0); cin>>n; for(int i=1;i<=n;i++) { long long a; cin>>a; pref[i].r=(pref[i-1].r+a)%p; pref[i].ind=i; } sort(pref, pref+n+1, majonez1); int l=0; for(int i=0;i<=n;i++) { if(skal[i]==0) { skal[i]=1; pref[i].scal=l; int x=i; while(pref[x+1].r==pref[i].r&&x<n) { pref[x+1].scal=l; skal[x+1]=1; x++; } l++; } } sort(pref,pref+n+1,majonez2); // for(int i=0;i<=n;i++)cout<<pref[i].r<<" "<<pref[i].scal<<endl; update0(baza,1); for(int i=1;i<n;i++) { if(pref[i].r%2==0) { long long b=0; long long a=czytaj0(baza,baza+pref[i].scal,baza,2*baza-1,1); if(pref[i].scal<n)b=czytaj1(baza+pref[i].scal+1,baza+n,baza,baza*2-1,1); long long dp=(a+b)%p; update0(baza+pref[i].scal,dp); } else { long long b=0; long long a=czytaj1(baza,baza+pref[i].scal,baza,2*baza-1,1); if(pref[i].scal<n)b=czytaj0(baza+pref[i].scal+1,baza+n,baza,baza*2-1,1); long long dp=(a+b)%p; update1(baza+pref[i].scal,dp); } } if(pref[n].r%2==0) { long long b=0; long long a=czytaj0(baza,baza+pref[n].scal,baza,2*baza-1,1); if(pref[n].scal<n)b=czytaj1(baza+pref[n].scal+1,baza+n,baza,baza*2-1,1); long long dp=(a+b)%p; cout<<dp; } else { long long b=0; long long a=czytaj1(baza,baza+pref[n].scal,baza,2*baza-1,1); if(pref[n].scal<n)b=czytaj0(baza+pref[n].scal+1,baza+n,baza,baza*2-1,1); long long dp=(a+b)%p; cout<<dp; } }
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 | #include <bits/stdc++.h> using namespace std; int n; long long p=10e8+7; struct maciek{long long r; int ind; int scal;}; maciek pref[300200]; bool skal[300200]; bool majonez1(maciek A, maciek B) { return A.r<B.r; } bool majonez2(maciek A, maciek B) { return A.ind<B.ind; } const int baza=1<<19; long long d0[baza * 2]; void update0(int poz, long long war) { while(poz > 0) { d0[poz] =(d0[poz]+ war)%p; poz /= 2; } return; } long long czytaj0(int targetleft, int targetright, int currentleft, int currentright, int poz) { if(targetleft <= currentleft && currentright <= targetright) { return d0[poz]; } long long wyn = 0; if(targetleft <= (currentleft + currentright) / 2) { wyn =(wyn+ czytaj0(targetleft, targetright, currentleft, (currentleft + currentright) / 2, poz * 2))%p; } if((currentleft + currentright) / 2 < targetright) { wyn =(wyn+ czytaj0(targetleft, targetright, (currentleft + currentright) / 2 + 1, currentright, poz * 2 + 1))%p; } return wyn; } long long d1[baza * 2]; void update1(int poz, long long war) { while(poz > 0) { d1[poz] += war; d1[poz]=d1[poz]%p; poz /= 2; } return; } long long czytaj1(int targetleft, int targetright, int currentleft, int currentright, int poz) { if(targetleft <= currentleft && currentright <= targetright) { return d1[poz]; } long long wyn = 0; if(targetleft <= (currentleft + currentright) / 2) { wyn =(wyn+ czytaj1(targetleft, targetright, currentleft, (currentleft + currentright) / 2, poz * 2))%p; } if((currentleft + currentright) / 2 < targetright) { wyn =(wyn+ czytaj1(targetleft, targetright, (currentleft + currentright) / 2 + 1, currentright, poz * 2 + 1))%p; } return wyn; } int main() { ios_base::sync_with_stdio(0); cin>>n; for(int i=1;i<=n;i++) { long long a; cin>>a; pref[i].r=(pref[i-1].r+a)%p; pref[i].ind=i; } sort(pref, pref+n+1, majonez1); int l=0; for(int i=0;i<=n;i++) { if(skal[i]==0) { skal[i]=1; pref[i].scal=l; int x=i; while(pref[x+1].r==pref[i].r&&x<n) { pref[x+1].scal=l; skal[x+1]=1; x++; } l++; } } sort(pref,pref+n+1,majonez2); // for(int i=0;i<=n;i++)cout<<pref[i].r<<" "<<pref[i].scal<<endl; update0(baza,1); for(int i=1;i<n;i++) { if(pref[i].r%2==0) { long long b=0; long long a=czytaj0(baza,baza+pref[i].scal,baza,2*baza-1,1); if(pref[i].scal<n)b=czytaj1(baza+pref[i].scal+1,baza+n,baza,baza*2-1,1); long long dp=(a+b)%p; update0(baza+pref[i].scal,dp); } else { long long b=0; long long a=czytaj1(baza,baza+pref[i].scal,baza,2*baza-1,1); if(pref[i].scal<n)b=czytaj0(baza+pref[i].scal+1,baza+n,baza,baza*2-1,1); long long dp=(a+b)%p; update1(baza+pref[i].scal,dp); } } if(pref[n].r%2==0) { long long b=0; long long a=czytaj0(baza,baza+pref[n].scal,baza,2*baza-1,1); if(pref[n].scal<n)b=czytaj1(baza+pref[n].scal+1,baza+n,baza,baza*2-1,1); long long dp=(a+b)%p; cout<<dp; } else { long long b=0; long long a=czytaj1(baza,baza+pref[n].scal,baza,2*baza-1,1); if(pref[n].scal<n)b=czytaj0(baza+pref[n].scal+1,baza+n,baza,baza*2-1,1); long long dp=(a+b)%p; cout<<dp; } } |