E-找贝壳 vector

这里是引用链接:https://ac.nowcoder.com/acm/contest/13504/E
来源:牛客网

小明来到一片海滩上,他很喜欢捡贝壳,但他只喜欢质量为x的倍数的贝壳。

#include<iostream>
#include<vector>
using namespace std;
#define int long long
const int maxn=1e5+10;
int arr[maxn];
vector<int>vec[maxn];
int read() {
    
    
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {
    
      // ch 不是数字时
        if (ch == '-') w = -1;        // 判断是否为负
        ch = getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {
    
      // ch 是数字时
        x = x * 10 + (ch - '0');  // 将新读入的数字’加’在 x 的后面
        // x 是 int 类型,char 类型的 ch 和 ’0’ 会被自动转为其对应的
        // ASCII 码,相当于将 ch 转化为对应数字
        // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10
        ch = getchar();  // 继续读入
    }
    return x * w;  // 数字 * 正负号 = 实际数值
}

signed main()
{
    
    
    int n,q,mi=-1,ma=0;
    cin>>n>>q;
    for(int i=1;i<=n;i++){
    
    
//        cin>>arr[i];
        arr[i]=read();
        mi=min(mi,arr[i]);
        ma=max(ma,arr[i]);
        vec[arr[i]].push_back(i);
    }
    while(q--){
    
    
        int l,r,x;
        cin>>l>>r>>x;
        
        //对于数字x sum[r]-sum[l-1]
        
        int fi = x;
        while(fi<mi){
    
    
            fi+=x;
        }
        if(fi>ma){
    
    
            cout<<0<<endl;
            continue;
        }
        //assert mi <= fi <= ma
        int ans = 0;
        while(fi<=ma){
    
    
            int sz=vec[fi].size();
            for(int i=0;i<sz;i++){
    
    
                int pos = vec[fi][i];
                if(pos>=l&&pos<=r){
    
    
                    ans++;
                }
            }
            fi+=x;
        }
        cout<<ans<<endl;
    }
}



猜你喜欢

转载自blog.csdn.net/weixin_56336619/article/details/115268507