首页 技术 正文
技术 2022年11月15日
0 收藏 460 点赞 2,421 浏览 1894 个字

Description

          给一个长度为n的非负整数序列A1,A2,…,An。现有m个询问,每次询问给出l,r,p,k,问满足l<=i<=r且Ai mod p = k的值i的个数。 

Input

         第一行两个正整数n和m。         第二行n个数,表示A1,A2,…,An。         以下m行,每行四个数分别表示l,r,p,k。满足1<=l<=r<=n。 

Output

         对于每个询问,输出一行,表示可行值i的个数。  

Sample Input

5 2
1 5 2 3 7
1 3 2 1
2 5 3 0

Sample Output

2
1

HINT

数据范围:
 0<n,m<=10^5,任意1<=i<=n满足Ai<=10^4,0<p<=10^4,0<=k<p。

Source

2011福建集训

容斥一下将询问转化成前缀和查询,然后离线添加,分块处理出来p<=sqrt(Ai)的答案,p>sqrt(Ai)的询问暴力即可。时间复杂度O((N+M)sqrt(c))。

#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<cassert>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
return x*f;
}
#define S 1200000
char pf[S],*o1=pf,*o2=pf+S;
#define ot(x) (o1==o2?fwrite(pf,1,S,stdout),*(o1=pf)++=x:*o1++=x)
inline void print(int x){static char s[10],*b;b=s;if(!x)*b++=48;for(;x;*b++=x%10+48,x/=10);for(;b--!=s;ot(*b));}
const int maxn=100010;
const int maxm=10010;
const int SIZE=50;
int n,m,A[maxn],ans[maxn];
struct Query {
int x,p,k,id;
bool operator < (const Query& ths) const {return x<ths.x;}
}Q[maxn<<1];
int cnt[maxm],B[55][55];
void Add(int v) {
cnt[v]++;
rep(i,1,SIZE) B[i][v%i]++;
}
int calc(int p,int k) {
int ans=0;
while(k<=10000) {
ans+=cnt[k];
k+=p;
}
return ans;
}
int main() {
n=read();m=read();
rep(i,1,n) A[i]=read();
rep(i,1,m) {
int l=read(),r=read(),p=read(),k=read();
Q[i]=(Query){l-1,p,k,-i};Q[i+m]=(Query){r,p,k,i};
}
sort(Q+1,Q+2*m+1);int cur=0;
rep(i,1,2*m) {
while(cur<Q[i].x) Add(A[++cur]);
int res=Q[i].p<=SIZE?B[Q[i].p][Q[i].k]:calc(Q[i].p,Q[i].k);if(Q[i].id<0) res=-res,Q[i].id=-Q[i].id;
ans[Q[i].id]+=res;
}
rep(i,1,m) print(ans[i]),ot('\n');
fwrite(pf,1,o1-pf,stdout);
return 0;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,117
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,590
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,435
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,206
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,842
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,927