#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; //mt19937 mrand(random_device{}()); const ll mod=1000000007; const ll mod2=1000000007*2; //int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=301000; int n,q,m; bitset<1000> blk[N]; ll lev[N],sz[N],dis[N],ans[N]; vector<array<int,3>> que[N]; int a[N],ev[N]; int dd[N*2],pool1[N*2],pool2[N*2],*d=pool1+N,*rd=pool2+N; int ps1[N*2],ps2[N*2],ps3[N*2],ps4[N*2]; int *s00=ps1+N,*s01=ps2+N,*s10=ps3+N,*s11=ps4+N; int lab[N],lenb[N],rk[N],cc[N]; int main() { scanf("%d%d",&n,&q); lev[0]=1; ll tot=1; for (int i=1;i<=n-1;i++) { scanf("%d",a+i); lev[i]=lev[i-1]*a[i]%mod2; tot=(tot+lev[i])%mod2; } int nev=n; ev[n+1]=ev[n]=n; for (int i=n-1;i>=0;i--) { if (a[i]%2==0) nev=i; ev[i]=nev; } sz[n-1]=1; per(i,0,n-1) sz[i]=(sz[i+1]*a[i+1]+1)%mod2; rep(i,1,n) dis[0]=(dis[0]+i*lev[i])%mod2; rep(i,1,n) dis[i]=(dis[i-1]-sz[i]+tot-sz[i])%mod2; rep(i,0,q) { int u,v,w; scanf("%d%d%d",&u,&v,&w); que[w].pb({u,v,i}); ans[i]=(dis[u-1]-(dis[u-1]+dis[v-1]-(tot%2)*(u-w+v-w-2))%mod2/2)%mod; } rep(i,1,n) { if (a[i]%2==0) lab[i]=++m; lenb[m]++; } lab[n]=m+1; set<int> dlen; rep(i,1,m+1) dlen.insert(lenb[i]); int mm=0; memset(rk,-1,sizeof(rk)); for (auto x:dlen) { rk[x]=mm; cc[mm]=x; mm++; } assert(mm<=1000); rep(i,1,n) if (a[i]%2==0) { blk[lab[i]]=blk[lab[i]-1]; assert(rk[lenb[lab[i]]]!=-1); blk[lab[i]].flip(rk[lenb[lab[i]]]); } per(i,0,n) { s00[i]=s00[i+1]; s01[i]=s10[i+1]; s10[i]=s01[i+1]+1; s11[i]=s11[i+1]+1; } for (int w=1;w<=n;w++) { int dd2=w-1+n-1; --d; --rd; --s00; --s01; --s10; --s11; rd[0]=rd[1]; s00[0]=s00[1]+rd[0]; s01[0]=s10[1]+rd[0]; s10[0]=s01[1]+(rd[0]^1); s11[0]=s11[1]+(rd[0]^1); if (a[w]%2==0) { for (int i=1;i<=lenb[lab[w]];i++) d[i]^=1; for (int i=lenb[lab[w]];i>=0;i--) { rd[i]=rd[i+1]^d[i]; s00[i]=s00[i+1]+rd[i]; s01[i]=s10[i+1]+rd[i]; s10[i]=s01[i+1]+(rd[i]^1); s11[i]=s11[i+1]+(rd[i]^1); } } for (auto q:que[w]) { int u=q[0],v=q[1]; if (u>v) swap(u,v); auto mblk=blk[lab[ev[v+1]]-1]^blk[lab[ev[u+1]]-1]; VI plen,qlen; rep(i,0,mm) if (mblk[i]) plen.pb(cc[i]); qlen.pb(ev[u+1]-u); qlen.pb(ev[v+1]-v); qlen.pb(ev[w+1]-w); qlen.pb(w-1); sort(all(qlen)); while (!qlen.empty()&&qlen[0]==0) qlen.erase(qlen.begin()); VI mlen(SZ(plen)+SZ(qlen)); merge(all(plen),all(qlen),mlen.begin()); for (int i=0;i+1<mlen.size();i++) if (mlen[i]==mlen[i+1]) { mlen.erase(mlen.begin()+i); mlen.erase(mlen.begin()+i); i-=1; } reverse(all(mlen)); mlen.pb(0); int pre=dd2,cc=0,rans=0; auto count=[&](int type,int r,int l) { if (type==0) { if (cc==0) rans+=s00[l]-s00[r+1]; else rans+=s11[l]-s11[r+1]; } else { if (cc==0) rans-=s01[r+1]; else rans-=s10[r+1]; cc=(cc+r-l+1)%2; if (cc==0) rans+=s01[l]; else rans+=s10[l]; } }; rep(i,0,SZ(mlen)) { count(i%2,pre,mlen[i]+1); pre=mlen[i]; } rans+=(cc+u-w+v-w+1+rd[0])%2; ans[q[2]]=(ans[q[2]]+rans)%mod; } } rep(i,0,q) { if (ans[i]<0) ans[i]+=mod; printf("%lld\n",ans[i]); } }
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 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; //mt19937 mrand(random_device{}()); const ll mod=1000000007; const ll mod2=1000000007*2; //int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=301000; int n,q,m; bitset<1000> blk[N]; ll lev[N],sz[N],dis[N],ans[N]; vector<array<int,3>> que[N]; int a[N],ev[N]; int dd[N*2],pool1[N*2],pool2[N*2],*d=pool1+N,*rd=pool2+N; int ps1[N*2],ps2[N*2],ps3[N*2],ps4[N*2]; int *s00=ps1+N,*s01=ps2+N,*s10=ps3+N,*s11=ps4+N; int lab[N],lenb[N],rk[N],cc[N]; int main() { scanf("%d%d",&n,&q); lev[0]=1; ll tot=1; for (int i=1;i<=n-1;i++) { scanf("%d",a+i); lev[i]=lev[i-1]*a[i]%mod2; tot=(tot+lev[i])%mod2; } int nev=n; ev[n+1]=ev[n]=n; for (int i=n-1;i>=0;i--) { if (a[i]%2==0) nev=i; ev[i]=nev; } sz[n-1]=1; per(i,0,n-1) sz[i]=(sz[i+1]*a[i+1]+1)%mod2; rep(i,1,n) dis[0]=(dis[0]+i*lev[i])%mod2; rep(i,1,n) dis[i]=(dis[i-1]-sz[i]+tot-sz[i])%mod2; rep(i,0,q) { int u,v,w; scanf("%d%d%d",&u,&v,&w); que[w].pb({u,v,i}); ans[i]=(dis[u-1]-(dis[u-1]+dis[v-1]-(tot%2)*(u-w+v-w-2))%mod2/2)%mod; } rep(i,1,n) { if (a[i]%2==0) lab[i]=++m; lenb[m]++; } lab[n]=m+1; set<int> dlen; rep(i,1,m+1) dlen.insert(lenb[i]); int mm=0; memset(rk,-1,sizeof(rk)); for (auto x:dlen) { rk[x]=mm; cc[mm]=x; mm++; } assert(mm<=1000); rep(i,1,n) if (a[i]%2==0) { blk[lab[i]]=blk[lab[i]-1]; assert(rk[lenb[lab[i]]]!=-1); blk[lab[i]].flip(rk[lenb[lab[i]]]); } per(i,0,n) { s00[i]=s00[i+1]; s01[i]=s10[i+1]; s10[i]=s01[i+1]+1; s11[i]=s11[i+1]+1; } for (int w=1;w<=n;w++) { int dd2=w-1+n-1; --d; --rd; --s00; --s01; --s10; --s11; rd[0]=rd[1]; s00[0]=s00[1]+rd[0]; s01[0]=s10[1]+rd[0]; s10[0]=s01[1]+(rd[0]^1); s11[0]=s11[1]+(rd[0]^1); if (a[w]%2==0) { for (int i=1;i<=lenb[lab[w]];i++) d[i]^=1; for (int i=lenb[lab[w]];i>=0;i--) { rd[i]=rd[i+1]^d[i]; s00[i]=s00[i+1]+rd[i]; s01[i]=s10[i+1]+rd[i]; s10[i]=s01[i+1]+(rd[i]^1); s11[i]=s11[i+1]+(rd[i]^1); } } for (auto q:que[w]) { int u=q[0],v=q[1]; if (u>v) swap(u,v); auto mblk=blk[lab[ev[v+1]]-1]^blk[lab[ev[u+1]]-1]; VI plen,qlen; rep(i,0,mm) if (mblk[i]) plen.pb(cc[i]); qlen.pb(ev[u+1]-u); qlen.pb(ev[v+1]-v); qlen.pb(ev[w+1]-w); qlen.pb(w-1); sort(all(qlen)); while (!qlen.empty()&&qlen[0]==0) qlen.erase(qlen.begin()); VI mlen(SZ(plen)+SZ(qlen)); merge(all(plen),all(qlen),mlen.begin()); for (int i=0;i+1<mlen.size();i++) if (mlen[i]==mlen[i+1]) { mlen.erase(mlen.begin()+i); mlen.erase(mlen.begin()+i); i-=1; } reverse(all(mlen)); mlen.pb(0); int pre=dd2,cc=0,rans=0; auto count=[&](int type,int r,int l) { if (type==0) { if (cc==0) rans+=s00[l]-s00[r+1]; else rans+=s11[l]-s11[r+1]; } else { if (cc==0) rans-=s01[r+1]; else rans-=s10[r+1]; cc=(cc+r-l+1)%2; if (cc==0) rans+=s01[l]; else rans+=s10[l]; } }; rep(i,0,SZ(mlen)) { count(i%2,pre,mlen[i]+1); pre=mlen[i]; } rans+=(cc+u-w+v-w+1+rd[0])%2; ans[q[2]]=(ans[q[2]]+rans)%mod; } } rep(i,0,q) { if (ans[i]<0) ans[i]+=mod; printf("%lld\n",ans[i]); } } |