【bzoj2506】calc

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。

题解
采用离线思想与差分思想,对询问排序,记f1[i][j]表示对i取模余数是j的数的个数,考虑到p < =10000, 对p分块,p <= 100 的 采用 f1[i][j], p > 100 的直接记f2[i]++。

代码

#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdio>
#include<cmath>
#define inf 1000000000
#define ll long long
#define mod 1000000
#define N 500005
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
struct node{int q,id,p,k;bool f;}q[200005];
int n,m,tot,ans[100005][2],a[100005];
int f1[105][105],f2[10005];
bool cmp(node a,node b){return a.q<b.q;}
int main()
{
    n=read();m=read();
    for (int i=1;i<=n;i++) a[i]=read();
    for (int i=1;i<=m;i++)
    {
        int l=read(),r=read(),p=read(),k=read();
        q[++tot].q=l-1;q[tot].f=1;q[tot].id=i;q[tot].p=p;q[tot].k=k;
        q[++tot].q=r;q[tot].id=i;q[tot].p=p;q[tot].k=k;
    }
    sort(q+1,q+tot+1,cmp);
    int now=0;
    for (int i=1;i<=tot;i++)
    {
        while (now<q[i].q)
        {
            now++;
            for (int j=1;j<=100;j++)
                f1[j][a[now]%j]++;
            f2[a[now]]++;
        }
        if (q[i].p<=100) ans[q[i].id][q[i].f]=f1[q[i].p][q[i].k];
        else
        {
            for (int j=q[i].k;j<=10000;j+=q[i].p)
                ans[q[i].id][q[i].f]+=f2[j];
        }
    }
    for (int i=1;i<=m;i++)
        printf("%d\n",ans[i][0]-ans[i][1]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值