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
| #include<bits/stdc++.h> using namespace std; const int maxn=1e5; const int maxm=5e5; const int B=320; struct Node{ int l,r,p,id; Node(int il,int ir,int ip,int iid){ l=il,r=ir,p=ip,id=iid; } Node(){} }; int n,q; int a[maxn+5]; int ans[maxm+5]; int len[maxm+5]; vector<Node> V[maxn+5]; int f[B+5][B+5]; int id[maxn+5]; int L[B+5],R[B+5]; int c[maxn+5]; int s[B+5]; inline void Build(){ for(int i=1;i<=maxn;i++){ id[i]=i/B; if(!L[id[i]]) L[id[i]]=i; R[id[i]]=i; } } inline void Modify(int x){ for(int i=id[x];i<=B;i++) s[i]++; for(int i=x;i<=R[id[x]];i++) c[i]++; } inline int Query(int x){ return (id[x]?s[id[x]-1]:0)+c[x]; } signed main(){ int l,r,x,y,p; int tmp; scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=q;i++){ scanf("%d%d%d%d%d",&l,&r,&x,&y,&p); x%=p,y%=p; if(x==y) continue; if(x<y) len[i]=r-l+1,swap(x,y); V[l-1].push_back(Node(p-x,p-y-1,p,-i)); V[r].push_back(Node(p-x,p-y-1,p,i)); } Build(); for(int i=1;i<=n;i++){ for(int j=1;j<=B;j++) f[j][a[i]%j]++; Modify(a[i]); for(Node j:V[i]){ tmp=0; if(j.p<=B) for(int k=j.l;k<=j.r;k++) tmp+=f[j.p][k]; else for(int k=0;k+j.l<=maxn;k+=j.p) tmp+=Query(min(k+j.r,maxn))-Query(k+j.l-1); if(j.id<0) ans[-j.id]-=tmp; else ans[j.id]+=tmp; } } for(int i=1;i<=q;i++){ if(len[i]) printf("%d\n",len[i]-ans[i]); else printf("%d\n",ans[i]); } return 0; }
|