分块学习

· · 算法·理论

本文章以后不再更新(也不会删除,留个纪念),请到 https://www.luogu.com.cn/article/179jgytf

数列分块

数列分块入门 1

一个长为 n 的序列, 要支持区间加法,单点查值.

  1. 预处理

    考虑分块, 每 T 个元素分一块 , 则有 \left \lceil \frac{n}{T} \right \rceil 块, 最坏 O(n).

  2. 修改

1.1. l, r 在同一块, 直接暴力修改, 最坏 O(T).

1.2. l, r 不在同一块, l 所在的块, r 在的块分别暴力, 最坏 O(T), 在 l, r 所在块的中间块加 \text{lazytag}, 最坏O\left(\frac{n}{T}+T\right).

综上, 最坏 O\left(\frac{n}{T}+T\right)

  1. 查询

直接初值加该点所在区间的 \text{lazytag} 即可, 最坏 O(1).

  1. 综合

预处理 O(n), 修改 O\left[q\left(\frac{n}{T}+T\right)\right], 查询 O(q),综合为O\left[q\left(\frac{n}{T}+T\right)\right]

  1. 求最优 T

可以利用均值不等式 \frac{a+b}{2} \ge \sqrt{ab}(a>0,b>0), 当且仅当 a=b 时取等.

变形 a+b\ge2\sqrt{ab}

代入\frac{n}{T}+T\ge2\sqrt{n}

当且仅当 T=\sqrt n 时, \frac{n}{T}+T 取得 \min 值 2\sqrt{n}.

总体复杂度 O(q\sqrt{n}).

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
const int N=50000+7,M=sqrt(N)+7;
int n,len,cnt;
int a[N],b[N];
int L[M],R[M],t[M];
signed main(){
  cin>>n;
  for(int i=1;i<=n;++i){
    cin>>a[i];
  }
  len=sqrt(n);
  cnt=(n+len-1)/len;
  for(int i=1;i<=n;++i){
    b[i]=(i-1)/len+1;
  }
  for(int i=1;i<=cnt;++i){
    L[i]=(i-1)*len+1;
    R[i]=min(i*len,n);
  }
  for(int i=1;i<=n;++i){
    int op,l,r,c;
    cin>>op>>l>>r>>c;
    if(op==0){
      if(b[l]==b[r]){
        for(int i=l;i<=r;++i){
          a[i]+=c;
        }
      }else{
        for(int i=l;i<=R[b[l]];++i){
          a[i]+=c;
        }
        for(int i=L[b[r]];i<=r;++i){
          a[i]+=c;
        }
        for(int i=b[l]+1;i<=b[r]-1;++i){
          t[i]+=c;
        }
      }
    }
    if(op==1){
      cout<<a[r]+t[b[r]]<<endl;
    }
  }
  return 0;
}

数列分块入门 2

一个长为 n 的序列, 要支持区间加法,区间查询小于一个数的数的个数.

  1. 预处理

考虑分块, 每 T 个元素分一块 , 则有 \left \lceil \frac{n}{T} \right \rceil 块.

为方便查询, 将 a 拷贝一份 d, d 的每块内分别排序, 查询时二分, 最坏 O(n \log n).

  1. 修改

1.1. l, r 在同一块, 直接暴力修改, 最坏 O(T \log T), 拷贝此块到 d ,再排序此块.

1.2. l, r 不在同一块, l 所在的块, r 在的块分别暴力, 拷贝这两块到 d 再排序这两块,最坏 O(T\log T), 在 l, r 所在块的中间块加 \text{lazytag}, 不影响顺序, 最坏O\left(\frac{n}{T}+T\log T\right).

综上, 最坏 O\left(\frac{n}{T}+T\log T\right).

  1. 查询

2.1. l, r 在同一块, 直接暴力查询, 最坏 O(T).

2.2. l, r 不在同一块, l 所在的块, r 在的块分别暴力查询, 最坏 O(T), 中间整块直接二分查找 最坏 O\left(\frac{n}{T} \log T\right)

综上, 最坏 O\left(T+\frac{n}{T}\log T\right).

  1. 综合

预处理 O(n\log n), 修改 O\left[q\left(\frac{n}{T}+T\log T\right)\right], 查询 O\left[q\left(T+\frac{n}{T}\log T\right)\right], 综合为O\left[q\left(\frac{n}{T}+T\right)\log T\right].

  1. 求最优 T

可以利用均值不等式 \frac{a+b}{2} \ge \sqrt{ab}(a>0,b>0), 当且仅当 a=b 时取等.

变形 a+b\ge2\sqrt{ab}

代入\frac{n}{T}+T\ge2\sqrt{n}

当且仅当 T=\sqrt n 时, \frac{n}{T}+T 取得 \min 值 2\sqrt{n}.

总体复杂度 O(q\sqrt{n} \log{\sqrt n}).

// Problem: #6278. 数列分块入门 2
// Contest: LibreOJ
// URL: https://loj.ac/p/6278
// Memory Limit: 256 MB
// Time Limit: 500 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
const int N=50000+7,M=sqrt(N)+7;
int n,len,cnt;
int a[N],b[N],d[N];
int L[M],R[M],t[M];
signed main(){
  cin>>n;
  for(int i=1;i<=n;++i){
    cin>>a[i];
    d[i]=a[i];
  }
  len=sqrt(n);
  cnt=(n+len-1)/len;
  for(int i=1;i<=n;++i){
    b[i]=(i-1)/len+1;
  }
  for(int i=1;i<=cnt;++i){
    L[i]=(i-1)*len+1;
    R[i]=min(i*len,n);
    sort(d+L[i],d+R[i]+1);
  }
  for(int i=1;i<=n;++i){
    int op,l,r,c;
    cin>>op>>l>>r>>c;
    if(op==0){
      if(b[l]==b[r]){
        for(int i=l;i<=r;++i){
          a[i]+=c;
        }
        for(int i=L[b[l]];i<=R[b[r]];++i){
          d[i]=a[i];
        }
        sort(d+L[b[l]],d+R[b[r]]+1);
      }else{
        for(int i=l;i<=R[b[l]];++i){
          a[i]+=c;
        }
        for(int i=L[b[l]];i<=R[b[l]];++i){
          d[i]=a[i];
        }
        sort(d+L[b[l]],d+R[b[l]]+1);
        for(int i=L[b[r]];i<=r;++i){
          a[i]+=c;
        }
        for(int i=L[b[r]];i<=R[b[r]];++i){
          d[i]=a[i];
        }
        sort(d+L[b[r]],d+R[b[r]]+1);
        for(int i=b[l]+1;i<=b[r]-1;++i){
          t[i]+=c;
        }
      }
    }
    if(op==1){
      int res=0;
      if(b[l]==b[r]){
        for(int i=l;i<=r;++i){
          res+=a[i]+t[b[i]]<c*c;
        }
      }else{
        for(int i=l;i<=R[b[l]];++i){
          res+=a[i]+t[b[i]]<c*c;
        }
        for(int i=L[b[r]];i<=r;++i){
          res+=a[i]+t[b[i]]<c*c;
        }
        for(int i=b[l]+1;i<=b[r]-1;++i){
          res+=lower_bound(d+L[i],d+R[i]+1,c*c-t[i])-(d+L[i]);
        }
      }
      cout<<res<<endl;
    }
  }
  return 0;
}

数列分块入门 3

一个长为 n 的序列, 要支持区间加法,区间查询小于一个数的的最大数(前驱).

找一个数的前驱等价于找小于一个数的数的个数(数列分块入门 2).

  1. 预处理

考虑分块, 每 T 个元素分一块 , 则有 \left \lceil \frac{n}{T} \right \rceil 块.

为方便查询, 将 a 拷贝一份 d, d 的每块内分别排序, 查询时二分, 最坏 O(n \log n).

  1. 修改

1.1. l, r 在同一块, 直接暴力修改, 最坏 O(T \log T), 拷贝此块到 d ,再排序此块.

1.2. l, r 不在同一块, l 所在的块, r 在的块分别暴力, 拷贝这两块到 d 再排序这两块,最坏 O(T\log T), 在 l, r 所在块的中间块加 \text{lazytag}, 不影响顺序, 最坏O\left(\frac{n}{T}+T\log T\right).

综上, 最坏 O\left(\frac{n}{T}+T\log T\right).

  1. 查询

2.1. l, r 在同一块, 直接暴力查询, 最坏 O(T).

2.2. l, r 不在同一块, l 所在的块, r 在的块分别暴力查询, 最坏 O(T), 中间整块直接二分查找 最坏 O\left(\frac{n}{T} \log T\right)

综上, 最坏 O\left(T+\frac{n}{T}\log T\right).

  1. 综合

预处理 O(n\log n), 修改 O\left[q\left(\frac{n}{T}+T\log T\right)\right], 查询 O\left[q\left(T+\frac{n}{T}\log T\right)\right], 综合为O\left[q\left(\frac{n}{T}+T\right)\log T\right].

  1. 求最优 T

可以利用均值不等式 \frac{a+b}{2} \ge \sqrt{ab}(a>0,b>0), 当且仅当 a=b 时取等.

变形 a+b\ge2\sqrt{ab}

代入\frac{n}{T}+T\ge2\sqrt{n}

当且仅当 T=\sqrt n 时, \frac{n}{T}+T 取得 \min 值 2\sqrt{n}.

总体复杂度 O(q\sqrt{n} \log{\sqrt n}).

// Problem: #6279. 数列分块入门 3
// Contest: LibreOJ
// URL: https://loj.ac/p/6279
// Memory Limit: 256 MB
// Time Limit: 1500 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
const int N=100000+7,M=sqrt(N)+7;
int n,len,cnt;
int a[N],b[N],d[N];
int L[M],R[M],t[M];
signed main(){
  cin>>n;
  for(int i=1;i<=n;++i){
    cin>>a[i];
    d[i]=a[i];
  }
  len=sqrt(n);
  cnt=(n+len-1)/len;
  for(int i=1;i<=n;++i){
    b[i]=(i-1)/len+1;
  }
  for(int i=1;i<=cnt;++i){
    L[i]=(i-1)*len+1;
    R[i]=min(i*len,n);
    sort(d+L[i],d+R[i]+1);
  }
  for(int i=1;i<=n;++i){
    int op,l,r,c;
    cin>>op>>l>>r>>c;
    if(op==0){
      if(b[l]==b[r]){
        for(int i=l;i<=r;++i){
          a[i]+=c;
        }
        for(int i=L[b[l]];i<=R[b[r]];++i){
          d[i]=a[i];
        }
        sort(d+L[b[l]],d+R[b[r]]+1);
      }else{
        for(int i=l;i<=R[b[l]];++i){
          a[i]+=c;
        }
        for(int i=L[b[l]];i<=R[b[l]];++i){
          d[i]=a[i];
        }
        sort(d+L[b[l]],d+R[b[l]]+1);
        for(int i=L[b[r]];i<=r;++i){
          a[i]+=c;
        }
        for(int i=L[b[r]];i<=R[b[r]];++i){
          d[i]=a[i];
        }
        sort(d+L[b[r]],d+R[b[r]]+1);
        for(int i=b[l]+1;i<=b[r]-1;++i){
          t[i]+=c;
        }
      }
    }
    if(op==1){
      int res=-1;
      if(b[l]==b[r]){
        for(int i=l;i<=r;++i){
          if(a[i]+t[b[i]]<c){
            res=max(res,a[i]+t[b[i]]);
          }
        }
      }else{
        for(int i=l;i<=R[b[l]];++i){
          if(a[i]+t[b[i]]<c){
            res=max(res,a[i]+t[b[i]]);
          }
        }
        for(int i=L[b[r]];i<=r;++i){
          if(a[i]+t[b[i]]<c){
            res=max(res,a[i]+t[b[i]]);
          }
        }
        for(int i=b[l]+1;i<=b[r]-1;++i){
          if(lower_bound(d+L[i],d+R[i]+1,c-t[i])!=d+L[i]){
            res=max(res,*(lower_bound(d+L[i],d+R[i]+1,c-t[i])-1)+t[i]);
          }
        }
      }
      cout<<res<<endl;
    }
  }
  return 0;
}

数列分块入门 4

一个长为 n 的序列, 要支持区间加法,区间查和(加取模).

  1. 预处理

    考虑分块, 每 T 个元素分一块 , 则有 \left \lceil \frac{n}{T} \right \rceil 块, 最坏 O(n).

    加一个和数组,统计块的和.

  2. 修改

1.1. l, r 在同一块, 直接暴力修改, 最坏 O(T).

1.2. l, r 不在同一块, l 所在的块, r 在的块分别暴力, 最坏 O(T), 在 l, r 所在块的中间块加 \text{lazytag}, 最坏O\left(\frac{n}{T}+T\right).

综上, 最坏 O\left(\frac{n}{T}+T\right)

  1. 查询

2.1. l, r 在同一块, 直接暴力查询, 最坏 O(T).

1.2. l, r 不在同一块, l 所在的块, r 在的块分别暴力, 最坏 O(T), 在 l, r 所在块的中间块加 和数组的值 + len \times \text{lazytag}, 最坏O\left(\frac{n}{T}+T\right).

综上, 最坏 O\left(\frac{n}{T}+T\right)

  1. 综合

预处理 O(n), 修改 O\left[q\left(\frac{n}{T}+T\right)\right], 查询 O(q),综合为O\left[q\left(\frac{n}{T}+T\right)\right]

  1. 求最优 T

可以利用均值不等式 \frac{a+b}{2} \ge \sqrt{ab}(a>0,b>0), 当且仅当 a=b 时取等.

变形 a+b\ge2\sqrt{ab}

代入\frac{n}{T}+T\ge2\sqrt{n}

当且仅当 T=\sqrt n 时, \frac{n}{T}+T 取得 \min 值 2\sqrt{n}.

总体复杂度 O(q\sqrt{n}).

// Problem: #6280. 数列分块入门 4
// Contest: LibreOJ
// URL: https://loj.ac/p/6280
// Memory Limit: 256 MB
// Time Limit: 500 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
const int N=50000+7,M=sqrt(N)+7;
int n,len,cnt;
int a[N],b[N];
int L[M],R[M],s[M],t[M];
signed main(){
  cin>>n;
  for(int i=1;i<=n;++i){
    cin>>a[i];
  }
  len=sqrt(n);
  cnt=(n+len-1)/len;
  for(int i=1;i<=n;++i){
    b[i]=(i-1)/len+1;
  }
  for(int i=1;i<=n;++i){
    s[b[i]]+=a[i];
  }
  for(int i=1;i<=cnt;++i){
    L[i]=(i-1)*len+1;
    R[i]=min(i*len,n);
  }
  for(int i=1;i<=n;++i){
    int op,l,r,c;
    cin>>op>>l>>r>>c;
    if(op==0){
      if(b[l]==b[r]){
        for(int i=l;i<=r;++i){
          a[i]+=c;
          s[b[i]]+=c;
        }
      }else{
        for(int i=l;i<=R[b[l]];++i){
          a[i]+=c;
          s[b[i]]+=c;
        }
        for(int i=L[b[r]];i<=r;++i){
          a[i]+=c;
          s[b[i]]+=c;
        }
        for(int i=b[l]+1;i<=b[r]-1;++i){
          t[i]+=c;
        }
      }
    }
    if(op==1){
      int res=0;
      if(b[l]==b[r]){
        for(int i=l;i<=r;++i){
          (res+=a[i]+t[b[i]])%=c+1;
        }
      }else{
        for(int i=l;i<=R[b[l]];++i){
          (res+=a[i]+t[b[i]])%=c+1;
        }
        for(int i=L[b[r]];i<=r;++i){
          (res+=a[i]+t[b[i]])%=c+1;
        }
        for(int i=b[l]+1;i<=b[r]-1;++i){
          (res+=s[i]+(R[i]-L[i]+1)*t[i])%=c+1;
        }
      }
      cout<<res<<endl;
    }
  }
  return 0;
}

数列分块入门 5

一个长为 n 的序列, 要支持区间开方,区间查和(加取模).

  1. 预处理

    考虑分块, 每 T 个元素分一块 , 则有 \left \lceil \frac{n}{T} \right \rceil 块, 最坏 O(n).

    加一个和数组,统计块的和.

    设 V 为 a_i 最大值.

    每个数最多会在 \log\log V 次开方后变成 0 或 1.

    开一个 \max 数组记录每段最大.

  2. 修改

1.1. l, r 在同一块, 若\max > 1, 直接暴力修改, 最坏 O(T).

1.2. l, r 不在同一块, l 所在的块若\max > 1, 直接暴力修改, r 在的块若\max > 1, 直接暴力修改, 最坏 O(T), 在 l, r 所在块的中间块若\max > 1, 直接暴力修改, 最坏O(T\log\log V).

综上, 最坏 O(T\log\log V)

  1. 查询

2.1. l, r 在同一块, 直接暴力查询, 最坏 O(T).

1.2. l, r 不在同一块, l 所在的块, r 在的块分别暴力, 最坏 O(T), 在 l, r 所在块的中间块加 和数组的值 + len \times \text{lazytag}, 最坏O\left(\frac{n}{T}+T\right).

综上, 最坏 O\left(\frac{n}{T}+T\right)

  1. 综合

预处理 O(n), 修改 O(qnT\log\log V), 查询 O\left[q\left(\frac{n}{T}+T\right)\right]),综合为O\left[q\left(T\log\log V+\frac{n}{T}+T\right)\right]

  1. 求最优 T

可以利用均值不等式 \frac{a+b}{2} \ge \sqrt{ab}(a>0,b>0), 当且仅当 a=b 时取等.

变形 a+b\ge2\sqrt{ab}

总体复杂度 O\left(q\sqrt{n\log\log V}\right).

// Problem: #6281. 数列分块入门 5
// Contest: LibreOJ
// URL: https://loj.ac/p/6281
// Memory Limit: 256 MB
// Time Limit: 500 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
const int N=50000+7,M=sqrt(N)+7;
int n,len,cnt;
int a[N],b[N];
int L[M],R[M],t[M],s[M],maxx[M];
signed main(){
  cin>>n;
  for(int i=1;i<=n;++i){
    cin>>a[i];
  }
  len=sqrt(n);
  cnt=(n+len-1)/len;
  for(int i=1;i<=n;++i){
    b[i]=(i-1)/len+1;
  }
  for(int i=1;i<=n;++i){
    s[b[i]]+=a[i];
    maxx[b[i]]=max(maxx[b[i]],a[i]);
  }
  for(int i=1;i<=cnt;++i){
    L[i]=(i-1)*len+1;
    R[i]=min(i*len,n);
  }
  for(int i=1;i<=n;++i){
    int op,l,r,c;
    cin>>op>>l>>r>>c;
    if(op==0){
      if(b[l]==b[r]){
        if(maxx[b[l]]>1){
          for(int i=l;i<=r;++i){
            a[i]=sqrt(a[i]);
          }
          s[b[l]]=0;
          maxx[b[l]]=0;
          for(int i=L[b[l]];i<=R[b[r]];++i){
            s[b[i]]+=a[i];
            maxx[b[i]]=max(maxx[b[i]],a[i]);
          }
        }
      }else{
        if(maxx[b[l]]>1){
          for(int i=l;i<=R[b[l]];++i){
            a[i]=sqrt(a[i]);
          }
          s[b[l]]=0;
          maxx[b[l]]=0;
          for(int i=L[b[l]];i<=R[b[l]];++i){
            s[b[i]]+=a[i];
            maxx[b[i]]=max(maxx[b[i]],a[i]);
          }
        }
        if(maxx[b[r]]>1){
          for(int i=L[b[r]];i<=r;++i){ 
            a[i]=sqrt(a[i]);
          }
          s[b[r]]=0;
          maxx[b[r]]=0;
          for(int i=L[b[r]];i<=R[b[r]];++i){
            s[b[i]]+=a[i];
            maxx[b[i]]=max(maxx[b[i]],a[i]);
          }
        }
        for(int i=b[l]+1;i<=b[r]-1;++i){
          if(maxx[i]>1){
            for(int j=L[i];j<=R[i];++j){ 
              a[j]=sqrt(a[j]);
            }
            s[i]=0;
            maxx[i]=0;
            for(int j=L[i];j<=R[i];++j){
              s[i]+=a[j];
              maxx[i]=max(maxx[i],a[j]);
            }
          }
        }
      }
    }
    if(op==1){
      int res=0;
      if(b[l]==b[r]){
        for(int i=l;i<=r;++i){
          res+=a[i];
        }
      }else{
        for(int i=l;i<=R[b[l]];++i){
          res+=a[i];
        }
        for(int i=L[b[r]];i<=r;++i){
          res+=a[i];
        }
        for(int i=b[l]+1;i<=b[r]-1;++i){
          res+=s[i];
        }
      }
      cout<<res<<endl;
    }
  }
  return 0;
}