传送门
文章目录
- 题意:
- 思路:
题意:
给你一个长度为nnn的数组aaa,每次操作有两种:
(1)(1)(1) 给出x,yx,yx,y,问aaa中下表在模xxx的时候,模数等于yyy的位置aia_iai之和。
(2)(2)(2) 给出x,yx,yx,y,代表令ax=ya_x=yax=y。
思路:
这个题的第一个询问就是从yyy下标开始,让后每次递增xxx,将经过的位置都算入答案,这样的复杂度显然不能接受。
考虑每次递增的xxx,如果我们能保证x>nx>\sqrt nx>n,那么递增的次数不会超过nn=n\frac{n}{\sqrt n}=\sqrt nnn=n,那么对于x≤nx\le \sqrt nx≤n的情况,我们考虑预处理,定义ans[x][y]ans[x][y]ans[x][y]表示在模数为xxx的情况下,下标模xxx为yyy的答案,这个显然可以nnn\sqrt nnn预处理。
考虑修改,我们只需要枚举n\sqrt nn的模数,让后将他们ansansans的值修改一下即可,复杂度n\sqrt nn。
代码还是很好写的。
复杂度O(mn)O(m\sqrt n)O(mn)
// Problem: P3396 哈希冲突
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3396
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=150010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n,m;
int val[N],block;
LL ans[600][600];int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);scanf("%d%d",&n,&m); block=sqrt(n);for(int i=1;i<=n;i++) scanf("%d",&val[i]);for(int i=1;i<=n;i++) {for(int j=1;j<=block;j++) ans[j][i%j]+=val[i];}while(m--) {char op[2];int a,b;scanf("%s%d%d",op+1,&a,&b);if(op[1]=='A') {if(b>=a) {puts("0");continue;}if(a<=block) printf("%lld\n",ans[a][b]);else {LL ans=0;for(int i=b;i<=n;i+=a) ans+=val[i];printf("%lld\n",ans);}} else {for(int i=1;i<=block;i++) {ans[i][a%i]+=b-val[a];}val[a]=b;}}return 0;
}
/**/