分块学习

· · 算法·理论

数列分块入门

数列分块入门 1

一个长为 n 的数组 a_i, q 次区间加法, 单点查值.

  1. 预处理

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

每个块设一个加法 lazytag, 记为 t.

  1. 修改
$l,r$ 不在同一块 > $l$ 所在的块, $r$ 所在的块分别暴力修改 $a$, 最坏 $\mathcal O\left(T\right)$. > > 在 $l,r$ 所在块的中间块修改 $t$, 最坏 $\mathcal O\left(\frac{n}{T}\right)$. > > 和为 $\mathcal O\left(T+\frac{n}{T}\right)$. 综上, 最坏 $\mathcal O\left(T+\frac{n}{T}\right)$.
  1. 查询

直接 a_p 加 p 点所在块的 t 即可, 最坏 \mathcal O\left(1\right).

  1. 总和

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

  1. 求最优 T

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

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

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

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

总和复杂度 \mathcal O\left(n+q\sqrt{n}\right).

:::success[AC code]

// Problem: #6277. 数列分块入门 1
// Contest: LibreOJ
// URL: https://loj.ac/p/6277
// Memory Limit: 256 MB
// Time Limit: 100 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], id[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) {
    id[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 (id[l] == id[r]) {
        for (int i = l; i <= r; ++ i) {
          a[i] += c;
        }
        continue;
      } 
      for (int i = l; i <= R[id[l]]; ++ i) {
        a[i] += c;
      }
      for (int i = L[id[r]]; i <= r; ++ i) {
        a[i] += c;
      }
      for (int i = id[l] + 1; i <= id[r] - 1; ++ i) {
        t[i] += c;
      }
    }
    if (op == 1) {
      cout << a[r] + t[id[r]] << endl;
    }
  }
  return 0;
}

:::

数列分块入门 2

一个长为 n 的数组 a_i, q 次区间加法, 区间查询小于一个数的数的个数.

  1. 预处理

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

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

每个块设一个加法 lazytag, 记为 t.

  1. 修改
$l,r$ 不在同一块 > $l$ 所在的块, $r$ 所在的块分别暴力修改 $a$, 拷贝这两块到 $b$ 再排序这两块, 最坏 $\mathcal O\left(T\log T\right)$. > > 在 $l,r$ 所在块的中间块加对应块 $t$, 不影响顺序, 最坏 $\mathcal O\left(\frac{n}{T}\right)$. > > 和为 $\mathcal O\left(T\log T+\frac{n}{T}\right)$. 综上, 最坏 $\mathcal O\left(T\log T+\frac{n}{T}\right)$.
  1. 查询
$l,r$ 不在同一块 > $l$ 所在的块, $r$ 所在的块分别暴力查询 $a$ 和对应块的 $t$, 最坏 $\mathcal O\left(T\right)$. > > 中间整块直接在 $b$ 中二分查找(不要忘记加对应块的 $t$), 最坏 $\mathcal O\left(\frac{n}{T} \log T\right)$. > > 和为 $\mathcal O\left(T+\frac{n}{T}\log T\right)$. 综上, 最坏 $\mathcal O\left(T+\frac{n}{T}\log T\right)$.
  1. 总和

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

  1. 求最优 T

这里 \log T 可以近似取 \log n.

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

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

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

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

总和复杂度 \mathcal O\left(n\log n+q\sqrt{n} \log{\sqrt {n}}\right), 已经足够通过, 下面代码采用此做法.

  1. 优化

我们在不全部修改一个块的时候, 不修改的与修改的内部的相对顺序不会变, 直接归并即可, 可以做到 \mathcal O\left(T\right), 修改复杂度变为 \mathcal O\left(T + \frac{n}{T}\right), 总复杂度变为 \mathcal O\left(T + \frac{n}{T} \log T\right), 这里 \log T 可以近似取 \log n.

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

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

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

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

总和复杂度 \mathcal O\left(n\log n+q\sqrt{n\log{n}}\right).

:::success[AC code]

// 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], id[N], b[N];
int L[M], R[M], t[M];
signed main() {
  cin >> n;
  for (int i = 1; i <= n; ++ i) {
    cin >> a[i];
    b[i] = a[i];
  }
  len = sqrt(n);
  cnt = (n + len - 1) / len;
  for (int i = 1; i <= n; ++ i) {
    id[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 <= cnt; ++ i) {
    sort(b + L[i], b + R[i] + 1);
  }
  for (int i = 1; i <= n; ++ i) {
    int op, l, r, c;
    cin >> op >> l >> r >> c;
    if (op == 0) {
      if (id[l] == id[r]) {
        for (int i = l; i <= r; ++ i) {
          a[i] += c;
        }
        for (int i = L[id[l]]; i <= R[id[r]]; ++ i) {
          b[i] = a[i];
        }
        sort(b + L[id[l]], b + R[id[r]] + 1);
        continue;
      }
      for (int i = l; i <= R[id[l]]; ++ i) {
        a[i] += c;
      }
      for (int i = L[id[l]]; i <= R[id[l]]; ++ i) {
        b[i] = a[i];
      }
      sort(b + L[id[l]], b + R[id[l]] + 1);
      for (int i = L[id[r]]; i <= r; ++ i) {
        a[i] += c;
      }
      for (int i = L[id[r]]; i <= R[id[r]]; ++ i) {
        b[i] = a[i];
      }
      sort(b + L[id[r]], b + R[id[r]] + 1);
      for (int i = id[l] + 1; i <= id[r] - 1; ++ i) {
        t[i] += c;
      }
    }
    if (op == 1) {
      int res = 0;
      if (id[l] == id[r]) {
        for (int i = l; i <= r; ++ i) {
          res += a[i] + t[id[i]] < c * c;
        }
        cout << res << endl;
        continue;
      } 
      for (int i = l; i <= R[id[l]]; ++ i) {
        res += a[i] + t[id[i]] < c * c;
      }
      for (int i = L[id[r]]; i <= r; ++ i) {
        res += a[i] + t[id[i]] < c * c;
      }
      for (int i = id[l] + 1; i <= id[r] - 1; ++ i) {
        res += lower_bound(b + L[i], b + R[i] + 1, c * c - t[i]) - b - L[i];
      }
      cout << res << endl;
    }
  }
  return 0;
}

:::

数列分块入门 3

一个长为 n 的数组 a_i, q 次区间加法, 区间查询小于一个数的的最大数(前驱).

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

  1. 预处理

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

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

每个块设一个加法 lazytag, 记为 t.

  1. 修改
$l,r$ 不在同一块 > $l$ 所在的块, $r$ 所在的块分别暴力修改 $a$, 拷贝这两块到 $b$ 再排序这两块, 最坏 $\mathcal O\left(T\log T\right)$. > > 在 $l,r$ 所在块的中间块加对应块 $t$, 不影响顺序, 最坏 $\mathcal O\left(\frac{n}{T}\right)$. > > 和为 $\mathcal O\left(T\log T+\frac{n}{T}\right)$. 综上, 最坏 $\mathcal O\left(T\log T+\frac{n}{T}\right)$.
  1. 查询
$l,r$ 不在同一块 > $l$ 所在的块, $r$ 所在的块分别暴力查询 $a$ 和对应块的 $t$, 最坏 $\mathcal O\left(T\right)$. > > 中间整块直接在 $b$ 中二分查找(不要忘记加对应块的 $t$), 最坏 $\mathcal O\left(\frac{n}{T} \log T\right)$. > > 和为 $\mathcal O\left(T+\frac{n}{T}\log T\right)$. 综上, 最坏 $\mathcal O\left(T+\frac{n}{T}\log T\right)$.
  1. 总和

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

  1. 求最优 T

这里 \log T 可以近似取 \log n.

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

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

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

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

总和复杂度 \mathcal O\left(n\log n+q\sqrt{n} \log{\sqrt {n}}\right), 已经足够通过, 下面代码采用此做法.

  1. 优化

我们在不全部修改一个块的时候, 不修改的与修改的内部的相对顺序不会变, 直接归并即可, 可以做到 \mathcal O\left(T\right), 修改复杂度变为 \mathcal O\left(T + \frac{n}{T}\right), 总复杂度变为 \mathcal O\left(T + \frac{n}{T} \log T\right), 这里 \log T 可以近似取 \log n.

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

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

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

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

总和复杂度 \mathcal O\left(n\log n+q\sqrt{n\log{n}}\right).

:::success[AC code]

// 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], id[N], b[N];
int L[M], R[M], t[M];
signed main() {
  cin >> n;
  for (int i = 1; i <= n; ++ i) {
    cin >> a[i];
    b[i] = a[i];
  }
  len = sqrt(n);
  cnt = (n + len - 1) / len;
  for (int i = 1; i <= n; ++ i) {
    id[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 <= cnt; ++ i) {
    sort(b + L[i], b + R[i] + 1);
  }
  for (int i = 1; i <= n; ++ i) {
    int op, l, r, c;
    cin >> op >> l >> r >> c;
    if (op == 0) {
      if (id[l] == id[r]) {
        for (int i = l; i <= r; ++ i) {
          a[i] += c;
        }
        for (int i = L[id[l]]; i <= R[id[r]]; ++ i) {
          b[i] = a[i];
        }
        sort(b + L[id[l]], b + R[id[r]] + 1);
        continue;
      }
      for (int i = l; i <= R[id[l]]; ++ i) {
        a[i] += c;
      }
      for (int i = L[id[l]]; i <= R[id[l]]; ++ i) {
        b[i] = a[i];
      }
      sort(b + L[id[l]], b + R[id[l]] + 1);
      for (int i = L[id[r]]; i <= r; ++ i) {
        a[i] += c;
      }
      for (int i = L[id[r]]; i <= R[id[r]]; ++ i) {
        b[i] = a[i];
      }
      sort(b + L[id[r]], b + R[id[r]] + 1);
      for (int i = id[l] + 1; i <= id[r] - 1; ++ i) {
        t[i] += c;
      }
    }
    if (op == 1) {
      int res = -1;
      if (id[l] == id[r]) {
        for (int i = l; i <= r; ++ i) {
          if (a[i] + t[id[i]] < c) {
            res = max(res, a[i] + t[id[i]]);
          }
        }
        cout << res << endl;
        continue;
      }
      for (int i = l; i <= R[id[l]]; ++ i) {
        if (a[i] + t[id[i]] < c) {
          res = max(res, a[i] + t[id[i]]);
        }
      }
      for (int i = L[id[r]]; i <= r; ++ i) {
        if (a[i] + t[id[i]] < c) {
          res = max(res, a[i] + t[id[i]]);
        }
      }
      for (int i = id[l] + 1; i <= id[r] - 1; ++ i) {
        if (lower_bound(b + L[i], b + R[i] + 1, c - t[i]) != b + L[i]) {
          res = max(res, *(lower_bound(b + L[i], b + R[i] + 1, c - t[i]) - 1) + t[i]);
        }
      }
      cout << res << endl;
    }
  }
  return 0;
}

:::

数列分块入门 4

一个长为 n 的数组 a_i, q 次区间加法, 区间查和(加取模).

  1. 预处理

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

每个块分别设一个总和和加法 lazytag, 记为 s,t.

  1. 修改
$l,r$ 不在同一块 > $l$ 所在的块, $r$ 所在的块分别暴力修改 $a$ 和对应块 $s$, 最坏 $\mathcal O\left(T\right)$. > > 在 $l,r$ 所在块的中间块加对应块 $s, t$, 最坏 $\mathcal O\left(\frac{n}{T}\right)$. > > 和为 $\mathcal O\left(T+\frac{n}{T}\right)$. 综上, 最坏 $\mathcal O\left(T+\frac{n}{T}\right)$.
  1. 查询
$l,r$ 不在同一块 > > $l$ 所在的块, $r$ 所在的块分别暴力查询 $a$ 和该块 $t$, 最坏 $\mathcal O\left(T\right)$. > > 在 $l,r$ 所在块的中间块查询该块 $s$, 最坏 $\mathcal O\left(\frac{n}{T}\right)$. > > 和为 $\mathcal O\left(T+\frac{n}{T}\right)$. 综上, 最坏 $\mathcal O\left(T+\frac{n}{T}\right)$.
  1. 总和

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

  1. 求最优 T

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

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

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

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

总和复杂度 \mathcal O\left(q\sqrt{n}\right).

:::success[AC code]

// 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], id[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) {
    id[i] = (i - 1) / len + 1;
  }
  for (int i = 1; i <= n; ++ i) {
    s[id[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 (id[l] == id[r]) {
        for (int i = l; i <= r; ++ i) {
          a[i] += c;
          s[id[i]] += c;
        }
        continue;
      }
      for (int i = l; i <= R[id[l]]; ++ i) {
        a[i] += c;
        s[id[i]] += c;
      }
      for (int i = L[id[r]]; i <= r; ++ i) {
        a[i] += c;
        s[id[i]] += c;
      }
      for (int i = id[l] + 1; i <= id[r] - 1; ++ i) {
        t[i] += c;
        s[i] += (R[i] - L[i] + 1) * c;
      }
    }
    if (op == 1) {
      int res = 0;
      if (id[l] == id[r]) {
        for (int i = l; i <= r; ++ i) {
          (res += a[i] + t[id[i]]) %= c + 1;
        }
        cout << res << endl;
        continue;
      }
      for (int i = l; i <= R[id[l]]; ++ i) {
        (res += a[i] + t[id[i]]) %= c + 1;
      }
      for (int i = L[id[r]]; i <= r; ++ i) {
        (res += a[i] + t[id[i]]) %= c + 1;
      }
      for (int i = id[l] + 1; i <= id[r] - 1; ++ i) {
        (res += s[i]) %= c + 1;
      }
      cout << res << endl;
    }
  }
  return 0;
}

:::

数列分块入门 5

一个长为 n 的数组 a_i, q 次区间开方, 区间查和(加取模).

  1. 预处理

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

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

设 V 为 a_i 最大值.

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

每个块设一个总和和一个 \max 值, 记为 s, \mathrm{mx}.

  1. 修改
$l,r$ 不在同一块 > $l$ 所在的块与 $r$ 所在的块若 $\mathrm{mx} > 1$, 直接暴力修改 $a$ 和对应块 $s, \mathrm{mx}$, 最坏总复杂度 $\mathcal O\left(n\log\log V+qT\right)$, 最坏均摊 $\mathcal O\left(\frac{n}{q}\log\log V+T\right)$. > > 在 $l,r$ 所在块的中间块若 $\mathrm{mx} > 1$, 直接暴力修改 $a$ 和对应块 $s, \mathrm{mx}$, 最坏总复杂度 $\mathcal O\left(n\log\log V+q\frac{n}{T}\right)$, 最坏均摊 $\mathcal O\left(\frac{n}{q}\log\log V+\frac{n}{T}\right)$. > > 和为 $\mathcal O\left(\frac{n}{q}\log\log V+T+\frac{n}{T}\right)$. 综上, 最坏均摊 $\mathcal O\left(\frac{n}{q}\log\log V+T+\frac{n}{T}\right)$.
  1. 查询
$l,r$ 不在同一块 > $l$ 所在的块, $r$ 所在的块分别暴力加 $a$, 最坏 $\mathcal O\left(T\right)$. > > 在 $l,r$ 所在块的中间块加对应块 $s$, 最坏 $\mathcal O\left(\frac{n}{T}\right)$. > > 和为 $\mathcal O\left(T+\frac{n}{T}\right)$. 综上, 最坏 $\mathcal O\left(T+\frac{n}{T}\right)$.
  1. 总和

预处理 \mathcal O\left(n\right), 修改 \mathcal O\left[q\left(\frac{n}{q}\log\log V+T+\frac{n}{T}\right)\right], 查询 \mathcal O\left[q\left(T+\frac{n}{T}\right)\right]), 总和为 \mathcal O\left[q\left(\frac{n}{q}\log\log V+T+\frac{n}{T}\right)\right], 即 \mathcal O\left[n\log\log V+q\left(T+\frac{n}{T}\right)\right]

  1. 求最优 T

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

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

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

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

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

:::success[AC code]

// 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], id[N];
int L[M], R[M], t[M], s[M], mx[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) {
    id[i] = (i - 1) / len + 1;
  }
  for (int i = 1; i <= n; ++ i) {
    s[id[i]] += a[i];
    mx[id[i]] = max(mx[id[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 (id[l] == id[r]) {
        if (mx[id[l]] > 1) {
          for (int i = l; i <= r; ++ i) {
            a[i] = sqrt(a[i]);
          }
          s[id[l]] = 0;
          mx[id[l]] = 0;
          for (int i = L[id[l]]; i <= R[id[r]]; ++ i) {
            s[id[i]] += a[i];
            mx[id[i]] = max(mx[id[i]], a[i]);
          }
        }
        continue;
      }
      if (mx[id[l]] > 1) {
        for (int i = l; i <= R[id[l]]; ++ i) {
          a[i] = sqrt(a[i]);
        }
        s[id[l]] = 0;
        mx[id[l]] = 0;
        for (int i = L[id[l]]; i <= R[id[l]]; ++ i) {
          s[id[i]] += a[i];
          mx[id[i]] = max(mx[id[i]], a[i]);
        }
      }
      if (mx[id[r]] > 1) {
        for (int i = L[id[r]]; i <= r; ++ i) { 
          a[i] = sqrt(a[i]);
        }
        s[id[r]] = 0;
        mx[id[r]] = 0;
        for (int i = L[id[r]]; i <= R[id[r]]; ++ i) {
          s[id[i]] += a[i];
          mx[id[i]] = max(mx[id[i]], a[i]);
        }
      }
      for (int i = id[l] + 1; i <= id[r] - 1; ++ i) {
        if (mx[i] > 1) {
          for (int j = L[i]; j <= R[i]; ++ j) { 
            a[j] = sqrt(a[j]);
          }
          s[i] = 0;
          mx[i] = 0;
          for (int j = L[i]; j <= R[i]; ++ j) {
            s[i] += a[j];
            mx[i] = max(mx[i], a[j]);
          }
        }
      }
    }
    if (op == 1) {
      int res = 0;
      if (id[l] == id[r]) {
        for (int i = l; i <= r; ++ i) {
          res += a[i];
        }
        cout << res << endl;
        continue;
      }
      for (int i = l; i <= R[id[l]]; ++ i) {
        res += a[i];
      }
      for (int i = L[id[r]]; i <= r; ++ i) {
        res += a[i];
      }
      for (int i = id[l] + 1; i <= id[r] - 1; ++ i) {
        res += s[i];
      }
      cout << res << endl;
    }
  }
  return 0;
}

:::

数列分块入门 6

一个长为 n 的数组 a_i, q 次单点插入, 单点查值.

  1. 预处理

考虑分块, 每 T 个元素分一块, 每一块的所有元素放到一个 vector 里, 则有 \frac{n}{T} 块, 然后将所有块放进一个 vector 里, 最坏 \mathcal O\left(n\right).

  1. 修改

暴力跳找到 p 所在的块, 最坏 \mathcal O\left(\frac{nq}{T}\right).

然后将新元素插到 p 前面, 最坏 \mathcal O\left(T\right).

若当前块的长度超过了 2T, 为了后续复杂度不退化, 直接暴力将此块分成两份, 单次复杂度 \mathcal O\left(T + \frac{nq}{T}\right), 分裂的频率为 \frac{1}{T}, 最多分裂 \frac{q}{T} 次, 均摊复杂度 \mathcal O\left(\frac{nq}{T^2}\right).

综上, 均摊复杂度 \mathcal O\left(T + \frac{nq}{T}\right)

  1. 查询

暴力跳找到 p 所在的块, 最坏 \mathcal O\left(\frac{nq}{T}\right).

  1. 总和

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

  1. 求最优 T

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

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

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

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

总和复杂度 \mathcal O\left(n+q\sqrt{qn}\right).

:::success[AC code]

// Problem: #6282. 数列分块入门 6
// Contest: LibreOJ
// URL: https://loj.ac/p/6282
// 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 = 100000 + 7, M = sqrt(N * 2) + 7;
int n, len, cnt;
int a[N];
vector<vector<int>> vec;
signed main() {
  cin >> n;
  for (int i = 1; i <= n; ++ i) {
    cin >> a[i];
  }
  len = sqrt(n * 2);
  cnt = (n + len - 1) / len;
  vec.resize(cnt + 7, vector<int>(1));
  for (int i = 1; i <= cnt; ++ i) {
    int L = (i - 1) * len + 1, R = min(i * len, n);
    for (int j = L; j <= R; ++ j) {
      vec[i].emplace_back(a[j]);
    }
  }
  for (int i = 1; i <= n; ++ i) {
    int op, l, r, c;
    cin >> op >> l >> r >> c;
    if (op == 0) {
      int id, k;
      for (id = 1; id <= cnt; ++ id) {
        k = vec[id].size() - 1;
        if (l <= k) {
          break;
        }
        l -= k;
      }
      vec[id].emplace(vec[id].begin() + l, r);
      ++ k;
      if (k >= 2 * len) {
        int p = k / 2;
        vec.emplace(vec.begin() + id + 1, vector<int>(1));
        for (int i = p + 1; i <= k; ++ i) {
          vec[id + 1].emplace_back(vec[id][i]);
        }
        vec[id].resize(p + 1);
      }
    } else {
      int id, k;
      for (id = 1; id <= cnt; ++ id) {
        k = vec[id].size() - 1;
        if (r <= k) {
          break;
        }
        r -= k;
      }
      cout << vec[id][r] << endl;
    }
  }
  return 0;
}

:::

数列分块入门 7

一个长为 n 的数组 a_i, q 次区间加法, 区间乘法, 单点查值.

  1. 预处理

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

每个块分别设一个乘法, 加法 lazytag, 记为 m, p(先乘后加).

  1. 修改加法
$l,r$ 不在同一块 > $l$ 所在的块, $r$ 所在的块分别暴力将对应块的所有 $a$ 还原, 再暴力修改 $a$, 最坏 $\mathcal O\left(T\right)$. > > 在 $l,r$ 所在块的中间块修改对应块的 $p$(如何修改见下), 最坏 $\mathcal O\left(\frac{n}{T}\right)$. > > > $m'a+p'=\left(ma+p\right) +c=\left(m\right)a+\left(p+c\right)
m'=m,p'=p+c

和为 \mathcal O\left(T+\frac{n}{T}\right).

综上, 最坏 \mathcal O\left(T+\frac{n}{T}\right). 2. 修改乘法

$l,r$ 不在同一块 > $l$ 所在的块, $r$ 所在的块分别暴力将对应块的所有 $a$ 还原, 再暴力修改 $a$, 最坏 $\mathcal O\left(T\right)$. > > 在 $l,r$ 所在块的中间块修改对应块的 $m, p$(如何修改见下), 最坏 $\mathcal O\left(\frac{n}{T}\right)$. > > > $m'a+p'=\left(ma+p\right) \times c=\left(m\times c\right)a+\left(p\times c\right)
m'=m\times c,p'=p\times c

和为 \mathcal O\left(T+\frac{n}{T}\right).

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

  1. 查询

直接 a_p 先乘后加 p 点所在块的 m, p 即可, 最坏 \mathcal O\left(1\right).

  1. 总和

预处理 \mathcal O\left(n\right), 修改加法 \mathcal O\left[q\left(T+\frac{n}{T}\right)\right], 修改乘法 \mathcal O\left[q\left(T+\frac{n}{T}\right)\right], 查询 \mathcal O\left(q\right), 总和为 \mathcal O\left[n+q\left(T+\frac{n}{T}\right)\right].

  1. 求最优 T

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

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

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

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

总和复杂度 \mathcal O\left(n+q\sqrt{n}\right).

:::success[AC code]

// Problem: #6283. 数列分块入门 7
// Contest: LibreOJ
// URL: https://loj.ac/p/6283
// 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 = 100000 + 7, M = sqrt(N) + 7, P = 10007;
int n, len, cnt;
int a[N], id[N];
int L[M], R[M], p[M], m[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) {
    id[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 <= cnt; ++ i) {
    m[i] = 1;
  }
  for (int i = 1; i <= n; ++ i) {
    int op, l, r, c;
    cin >> op >> l >> r >> c;
    if (op == 0) {
      if (id[l] == id[r]) {
        for (int i = L[id[l]]; i <= R[id[r]]; ++ i) {
          (a[i] = a[i] * m[id[i]] + p[id[i]] + (l <= i && i <= r ? c : 0)) %= P;
        }
        p[id[l]] = 0;
        m[id[l]] = 1;
        continue;
      }
      for (int i = L[id[l]]; i <= R[id[l]]; ++ i) {
        (a[i] = a[i] * m[id[i]] + p[id[i]] + (l <= i && i <= r ? c : 0)) %= P;
      }
      p[id[l]] = 0;
      m[id[l]] = 1;
      for (int i = L[id[r]]; i <= R[id[r]]; ++ i) {
        (a[i] = a[i] * m[id[i]] + p[id[i]] + (l <= i && i <= r ? c : 0)) %= P;
      }
      p[id[r]] = 0;
      m[id[r]] = 1;
      for (int i = id[l] + 1; i <= id[r] - 1; ++ i) {
        (p[i] += c) %= P;
      }
    }
    if (op == 1) {
      if (id[l] == id[r]) {
        for (int i = L[id[l]]; i <= R[id[r]]; ++ i) {
          (a[i] = (a[i] * m[id[i]] + p[id[i]]) * (l <= i && i <= r ? c : 1)) %= P;
        }
        p[id[l]] = 0;
        m[id[l]] = 1;
        continue;
      }
      for (int i = L[id[l]]; i <= R[id[l]]; ++ i) {
        (a[i] = (a[i] * m[id[i]] + p[id[i]]) * (l <= i && i <= r ? c : 1)) %= P;
      }
      p[id[l]] = 0;
      m[id[l]] = 1;
      for (int i = L[id[r]]; i <= R[id[r]]; ++ i) {
        (a[i] = (a[i] * m[id[i]] + p[id[i]]) * (l <= i && i <= r ? c : 1)) %= P;
      }
      p[id[r]] = 0;
      m[id[r]] = 1;
      for (int i = id[l] + 1; i <= id[r] - 1; ++ i) {
        (p[i] *= c) %= P;
        (m[i] *= c) %= P;
      }
    }
    if (op == 2) {
      cout << (a[r] * m[id[r]] + p[id[r]]) % P << endl;
    }
  }
  return 0;
}

:::

数列分块入门 8

一个长为 n 的数组 a_i, q 次区间查询等于一个数的个数并区间赋值.

  1. 预处理

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

每个块分别设一个赋值 lazytag, 记为 t.

  1. 查询&修改
$l,r$ 不在同一块 > $l$ 所在的块, $r$ 所在的块分别暴力将对应块的所有 $a$ 还原, 再暴力查询&修改 $a$, 最坏 $\mathcal O\left(T\right)$. > > 在 $l,r$ 所在块的中间块 > > 若没有 $t$ 那么就暴力查询 $a$, 并修改该块的 $t$, 这一步复杂度可以放到前面还原时的复杂度, 这里就不计算. > > > > 若有 $t$, 直接查询&修改该块的 $t

最坏 \mathcal O\left(\frac{n}{T}\right),

和为 \mathcal O\left(T+\frac{n}{T}\right).

  1. 总和

预处理 \mathcal O\left(n\right), 查询&修改 \mathcal O\left[q\left(T+\frac{n}{T}\right)\right], 总和为 \mathcal O\left[n+q\left(T+\frac{n}{T}\right)\right].

  1. 求最优 T

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

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

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

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

总和复杂度 \mathcal O\left(n+q\sqrt{n}\right).

:::success[AC code]

// Problem: #6284. 数列分块入门 8
// Contest: LibreOJ
// URL: https://loj.ac/p/6284
// 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 = 100000 + 7, M = sqrt(N) + 7;
int n, len, cnt;
int a[N], id[N];
int L[N], R[N], t[N];
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) {
    id[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 <= cnt; ++ i) {
    t[i] = -1;
  }
  for (int i = 1; i <= n; ++ i) {
    int op, l, r, c;
    cin >> l >> r >> c;
    int res = 0;
    if (id[l] == id[r]) {
      if (~t[id[l]]) {
        for (int i = L[id[l]]; i <= R[id[r]]; ++ i) {
          a[i] = t[id[i]];
        }
        t[id[l]] = -1;
      }
      for (int i = l; i <= r; ++ i) {
        res += a[i] == c;
      }
      for (int i = l; i <= r; ++ i) {
        a[i] = c;
      }
      cout << res << endl;
      continue;
    }
    if (~t[id[l]]) {
      for (int i = L[id[l]]; i <= R[id[l]]; ++ i) {
        a[i] = t[id[i]];
      }
      t[id[l]] = -1;
    }
    for (int i = l; i <= R[id[l]]; ++ i) {
      res += a[i] == c;
    }
    for (int i = l; i <= R[id[l]]; ++ i) {
      a[i] = c;
    }
    if (~t[id[r]]) {
      for (int i = L[id[r]]; i <= R[id[r]]; ++ i) {
        a[i] = t[id[i]];
      }
      t[id[r]] = -1;
    }
    for (int i = L[id[r]]; i <= r; ++ i) {
      res += a[i] == c;
    }
    for (int i = L[id[r]]; i <= r; ++ i) {
      a[i] = c;
    }
    for (int i = id[l] + 1; i <= id[r] - 1; ++ i) {
      if (!~t[i]) {
        for (int j = L[i]; j <= R[i]; ++ j) {
          res += a[j] == c;
        }
      }
      res += (t[i] == c) * (R[i] - L[i] + 1);
      t[i] = c;
    }
    cout << res << endl;
  }
  return 0;
}

:::

数列分块入门 9

一个长为 n 的数组 a_i, q 次区间查询众数.

  1. 预处理

值域较大, 先离散化, 最坏 \mathcal O\left(n\log n\right).

将每个值 i 出现的位置 p_i 存起来, 最坏 \mathcal O\left(n\right).

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

维护出每一段块的众数 f_{l, r}, 最坏 \mathcal O\left(n\times\frac{n}{T}\right).

和为 \mathcal O\left(n\log n+n\times\frac{n}{T}\right).

  1. 查询

若 l,r 之间没有整块, 直接每个元素在 p 中二分查出出现次数, 最坏 O(T\log n).

若 l,r 之间有整块, 众数只可能在 l 所在的块, r 所在的块, 或 l,r 中间的这一段块的众数 f, 直接每个元素在 p 中二分查出出现次数, 最坏 O(T\log n).

和为 O(T\log n).

  1. 总和

预处理 \mathcal O\left(n\log n+n\times\frac{n}{T}\right), 查询 \mathcal O\left(q\times T\log n\right), 总和为 \mathcal O\left(n\log n+n\times\frac{n}{T}+q\times T\log n\right).

  1. 求最优 T

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

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

代入 q\times T\log n+n\times\frac{n}{T}\ge2n\sqrt{q\log n}

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

总和复杂度 \mathcal O\left(n\log n+n\sqrt{q\log n}\right).

:::success[AC code]

// Problem: #6285. 数列分块入门 9
// Contest: LibreOJ
// URL: https://loj.ac/p/6285
// 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 * __lg(N)) * 2 + 7;
int n, len, cnt;
int a[N], b[N], id[N];
int L[M], R[M];
int f[M][M];
int t[N];
vector<int> p[N];
signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; ++ i) {
    cin >> a[i];
    b[i] = a[i];
  }
  len = sqrt(n / (__lg(n) + 1));
  cnt = (n + len - 1) / len;
  for (int i = 1; i <= n; ++ i) {
    id[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(b + 1, b + n + 1);
  int m = unique(b + 1, b + n + 1) - b - 1;
  for (int i = 1; i <= n; ++ i) {
    a[i] = lower_bound(b + 1, b + m + 1, a[i]) - b;
    p[a[i]].emplace_back(i);
  }
  for (int i = 1; i <= cnt; ++ i) {
    for (int j = 1; j <= m; ++ j) {
      t[j] = 0;
    }
    int res = 0, mx = 0;
    for (int j = L[i]; j <= n; ++ j) {
      ++ t[a[j]];
      if (t[a[j]] > mx || t[a[j]] == mx && a[j] < res) {
        res = a[j];
        mx = t[a[j]];
      }
      if (j == R[id[j]]) {
        f[i][id[j]] = res;
      }
    }
  }
  for (int i = 1; i <= n; ++ i) {
    int op, l, r, c;
    cin >> l >> r;
    int res = 0, mx = 0;
    if (id[l] + 1 > id[r] - 1) {
      for (int i = l; i <= r; ++ i) {
        int tmp = upper_bound(p[a[i]].begin(), p[a[i]].end(), r) - lower_bound(p[a[i]].begin(), p[a[i]].end(), l);
        if (tmp > mx || tmp == mx && a[i] < res) {
          res = a[i];
          mx = tmp;
        }
      }
      cout << b[res] << endl;
      continue;
    }
    for (int i = l; i <= R[id[l]]; ++ i) {
      int tmp = upper_bound(p[a[i]].begin(), p[a[i]].end(), r) - lower_bound(p[a[i]].begin(), p[a[i]].end(), l);
      if (tmp > mx || tmp == mx && a[i] < res) {
        res = a[i];
        mx = tmp;
      }
    }
    for (int i = L[id[r]]; i <= r; ++ i) {
      int tmp = upper_bound(p[a[i]].begin(), p[a[i]].end(), r) - lower_bound(p[a[i]].begin(), p[a[i]].end(), l);
      if (tmp > mx || tmp == mx && a[i] < res) {
        res = a[i];
        mx = tmp;
      }
    }
    int tmp = upper_bound(p[f[id[l] + 1][id[r] - 1]].begin(), p[f[id[l] + 1][id[r] - 1]].end(), r) - lower_bound(p[f[id[l] + 1][id[r] - 1]].begin(), p[f[id[l] + 1][id[r] - 1]].end(), l);
    if (tmp > mx || tmp == mx && f[id[l] + 1][id[r] - 1] < res) {
      res = f[id[l] + 1][id[r] - 1];
      mx = tmp;
    }
    cout << b[res] << endl;
  }
  return 0;
}

:::