P3396 哈希冲突(思维+方块)
题目
做法
预处理模数\([1,\sqrt{n}]\)的内存池,\(O(n\sqrt{n})\)
查询模数在范围里则直接输出,否则模拟\(O(m\sqrt{n})\)
修改则遍历模数\([1,\sqrt{n}]\),复杂度\(O(m\sqrt{n})\)
Code
#include<bits/stdc++.h>
typedef int LL;
const LL maxn=2e5+9;
inline LL Read(){
LL x(0),f(1); char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0' && c<='9'){
x=(x<<3ll)+(x<<1ll)+c-'0'; c=getchar();
}return x*f;
}
LL n,m;
LL a[maxn],sum[509][509];
int main(){
n=Read(); m=Read();
for(LL i=1;i<=n;++i) a[i]=Read();
LL up(sqrt(n));
for(LL i=1;i<=n;++i)
for(LL j=1;j<=up;++j) sum[j][i%j]+=a[i];
while(m--){
char c; scanf(" %c",&c);
LL x(Read()),y(Read());
if(c=='A'){
if(x<=up) printf("%d\n",sum[x][y]);
else{
LL ret(0);
for(LL i=y;i<=n;i+=x) ret+=a[i];
printf("%d\n",ret);
}
}else{
for(LL j=1;j<=up;++j) sum[j][x%j]-=a[x],sum[j][x%j]+=y;
a[x]=y;
}
}
return 0;
}