根号分治
简述
当直接预处理和直接回答询问的复杂度太高时,可以使用根号分治来使复杂度平衡,一般是
例题
例1 哈希冲突
我们可以想到两种算法:
-
直接预处理在
\mod x 意义下为y 的答案,预处理O(n^2) 单次询问O(1) 修改O(n) -
直接回答询问,单次询问
O(n) 修改O(1)
发现都过不了,考虑根号分治。
对于模数
对于模数
代码:
#include<bits/stdc++.h>//根号分治
#define f(i,l,r) for(int i=l;i<=r;++i)
#define F(i,r,l) for(int i=r;i>=l;--i)
#define LL long long
#define ULL unsigned long long
#define read(n) {int _x=0,_ty=1;char _c=getchar();while(!isdigit(_c)){if(_c=='-')_ty=-1;_c=getchar();}while(isdigit(_c))_x=10*_x+_c-'0',_c=getchar();n=_x*_ty;}
using namespace std;
const int N=150005,B=400;
int n,m,a[N],block,sum[N][B];
int main(){
read(n);
read(m);
block=sqrt(n);
f(i,1,n){
read(a[i]);
f(j,1,block)
sum[j][i%j]+=a[i];
}
while(m--){
int x,y,ans=0;
char c=getchar();
while(c!='A'&&c!='C')
c=getchar();
read(x);
read(y);
if(c=='A'){
if(x<block)
printf("%d\n",sum[x][y%x]);
else{
for(int i=y;i<=n;i+=x)
ans+=a[i];
printf("%d\n",ans);
}
}
else{
f(i,1,block)
sum[i][x%i]+=y-a[x];
a[x]=y;
}
}
return 0;
}
例2 Array Queries
与之前一样,有两种算法:
-
预处理所有
p,k 的答案,预处理O(n^2) ,单次询问O(1) -
直接模拟,单次询问
O(n)
考虑根号分治。
当
复杂度
代码:
#include<bits/stdc++.h>
#define f(i,l,r) for(int i=l;i<=r;++i)
#define F(i,r,l) for(int i=r;i>=l;--i)
#define LL long long
#define ULL unsigned long long
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#define read(n) {int _x=0,_ty=1;char _c=getchar();while(!isdigit(_c)){if(_c=='-')_ty=-1;_c=getchar();}while(isdigit(_c))_x=10*_x+_c-'0',_c=getchar();n=_x*_ty;}
char buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
const int N=100005,B=350;
int n,q,dp[N][B],a[N],block;
int main(){
read(n);
block=sqrt(n);
f(i,1,n)
read(a[i]);
F(i,n,1)
f(j,1,block){
if(a[i]+i+j>n)
dp[i][j]=1;
else
dp[i][j]=dp[i+a[i]+j][j]+1;
}
read(q);
while(q--){
int p,k,ans=0;
read(p);
read(k);
if(k<block)
printf("%d\n",dp[p][k]);
else{
for(;p<=n;p+=a[p]+k)
++ans;
printf("%d\n",ans);
}
}
return 0;
}
例3 Till I Collapse
-
答案不会超过
\frac{n}{k} ,因此不同答案的个数为O(\sqrt n) 。 -
答案具有单调性,相同的答案一定连续。
考虑根号分治。
当
当
代码:
#include<bits/stdc++.h>
#define f(i,l,r) for(int i=l;i<=r;++i)
#define F(i,r,l) for(int i=r;i>=l;--i)
#define LL long long
#define ULL unsigned long long
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#define read(n) {int _x=0,_ty=1;char _c=getchar();while(!isdigit(_c)){if(_c=='-')_ty=-1;_c=getchar();}while(isdigit(_c))_x=10*_x+_c-'0',_c=getchar();n=_x*_ty;}
char buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
const int N=100005;
int n,a[N],ans,block;
bool vis[N];
bool check(int x){
memset(vis,0,sizeof(vis));
int sum=0,tot=1,cnt=0;
f(i,1,n){
if(!vis[a[i]]){
vis[a[i]]=1;
++cnt;
}
if(cnt>x){
++sum;
cnt=1;
f(j,tot,i-1)
vis[a[j]]=0;
vis[a[i]]=1;
tot=i;
}
}
if(cnt)
++sum;
return sum<ans;
}
int main(){
read(n);
block=sqrt(n);
f(i,1,n)
read(a[i]);
f(k,1,n){
ans=0;
memset(vis,0,sizeof(vis));
int tot=1,cnt=0;
f(i,1,n){
if(!vis[a[i]]){
vis[a[i]]=1;
++cnt;
}
if(cnt>k){
++ans;
cnt=1;
f(j,tot,i-1)
vis[a[j]]=0;
vis[a[i]]=1;
tot=i;
}
}
if(cnt)
++ans;
if(k<block)
printf("%d ",ans);
else{
int l=k,r=n;
while(l<=r){
int mid=l+r>>1;
if(check(mid))
r=mid-1;
else
l=mid+1;
}
f(i,k,l-1)
printf("%d ",ans);
k=l-1;
}
}
return 0;
}