根号分治

· · 算法·理论

简述

当直接预处理和直接回答询问的复杂度太高时,可以使用根号分治来使复杂度平衡,一般是 O(n\sqrt n) 。

例题

例1 哈希冲突

我们可以想到两种算法:

发现都过不了,考虑根号分治。

对于模数 <\sqrt n 的情况,按第一种方法做,预处理 O(n\sqrt n) ,单次询问 O(1)

对于模数 >\sqrt n 的情况,按第二种方法做,单次询问查询都是 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 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

与之前一样,有两种算法:

考虑根号分治。

当 k<\sqrt n 的时候,预处理;当 k>\sqrt n 的时候,直接暴力模拟。

复杂度 O(n\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,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

考虑根号分治。

当 k< \sqrt n 时,直接暴力统计,复杂度 O(\sqrt n)

当 k> \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;
}