CF1207F Remainder Problem[分塊]

傳送門
題意:有一個大小爲5e5的數組,初始時數組元素全爲0,有兩種操作
1 x y,爲a[x]+=y
2 x y,爲求數組下標%x==y的所有元素之和

題解:分塊,當模數小於T時可以直接預處理出模數爲i餘數爲j的元素和sum[i][j],更新時sum[i][x%i]+=y,更新複雜度爲O(T),而模數大於T時直接暴力求和,時間複雜度爲O(N/T),當T=sqrt(N)時二者複雜度相同,則可以據此進行分塊

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;

const ll mod=1e9+7;
const int maxn=5e5+5;

ll sum[711][711],a[maxn];

int main(){
    int q;
    scanf("%d",&q);
    while(q--){
        int op;
        ll x,y;
        scanf("%d%lld%lld",&op,&x,&y);
        if(op==1){
            for(int i=1;i<711;i++)sum[i][x%i]+=y;
            a[x]+=y;
        }
        else{
            if(x<711){
                printf("%lld\n",sum[x][y]);
            }
            else{
                ll s=0;
                for(int i=y;i<maxn;i+=x){
                    s+=a[i];
                }
                printf("%lld\n",s);
            }
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章