BZOJ 2506 分块
//By SiriusRen #include <bits/stdc++.h> using namespace std; const int N=100005; int a[N],n,m,f[105][105],g[N],tmp=1; struct Node{int l,r,p,k,ans,id;}ask[N]; bool cmp(Node a,Node b){return a.l<b.l;} bool cmp2(Node a,Node b){return a.id<b.id;} int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=m;i++) scanf("%d%d%d%d",&ask[i].l,&ask[i].r,&ask[i].p,&ask[i].k),ask[i].id=i,ask[i].r++; sort(ask+1,ask+1+m,cmp); for(int i=1;i<=m;i++){ while(tmp<ask[i].l){ for(int i=1;i<=100;i++)f[i][a[tmp]%i]++; g[a[tmp]]++; tmp++; } if(ask[i].p<=100)ask[i].ans-=f[ask[i].p][ask[i].k]; else{ for(int i=0;i*ask[i].p+ask[i].k<=10000;i++)ask[i].ans-=g[i*ask[i].p+ask[i].k]; } }tmp=1; memset(f,0,sizeof(f)),memset(g,0,sizeof(g)); for(int i=1;i<=m;i++){ while(tmp<ask[i].r){ for(int i=1;i<=100;i++)f[i][a[tmp]%i]++; g[a[tmp]]++; tmp++; } if(ask[i].p<=100)ask[i].ans+=f[ask[i].p][ask[i].k]; else{ for(int i=0;i*ask[i].p+ask[i].k<=10000;i++)ask[i].ans+=g[i*ask[i].p+ask[i].k]; } } sort(ask+1,ask+1+m,cmp2); for(int i=1;i<=m;i++)printf("%d\n",ask[i].ans); }