P3396 哈希冲突 根号分治
生活随笔
收集整理的這篇文章主要介紹了
P3396 哈希冲突 根号分治
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傳送門
文章目錄
- 題意:
- 思路:
題意:
給你一個長度為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 nn?n?=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; } /**/總結
以上是生活随笔為你收集整理的P3396 哈希冲突 根号分治的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用惠普笔记本电脑惠普电脑如何打开电脑
- 下一篇: 飞鱼星路由器的设置是什么飞鱼星双wan口