luogu P3396 哈希冲突(分块?)

我们可以维护一个\(f[i][j]\)代表%\(i\)意义下得\(j\)的答案。然后维护就炸了。
先设\(x=\sqrt{n}\)然后我们发现,当\(i>x\)时我们直接暴力复杂度为\(O(x)\),然后我们对\(i\leq{x}\)的i维护\(f[i][j]\),这样询问复杂度\(O(1)\),维护复杂度\(O(x)\)。就可以通过此题了。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=200100;
int n,m,a[N],f[500][500],Block;
int read(){
    int sum=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){sum=sum*10+ch-'0';ch=getchar();}
    return sum*f;
}
int main(){
    n=read(),m=read();
    Block=sqrt(n);
    for(int i=1;i<=n;i++){
        a[i]=read();
        for(int j=1;j<=Block;j++){
            f[j][i%j]+=a[i];
        }
    }
    char s[3];
    while(m--){
        scanf("%s",s);
        int x=read(),y=read();
        if(s[0]=='A'){
            if(x>Block){
                int tmp=0;
                for(int i=y;i<=n;i+=x)tmp+=a[i];
                printf("%d\n",tmp);
            }
            else printf("%d\n",f[x][y]);    
        }
        else{
            for(int i=1;i<=Block;i++)
                f[i][x%i]-=a[x],f[i][x%i]+=y;
            a[x]=y;
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Xu-daxia/p/10124223.html
今日推荐