二进制警报器

· · 算法·理论

给定序列 a_n,有 q 次操作

  • 单点加上一个数;
  • 给定 S,v,添加一个监视器,当 \sum_{i\in S}a_i\ge v 时输出这个监视器的编号。

大家想必都知道折半警报器:对于一个监视器,记 d_x=v_x-\sum_{j\in S_i}a_j,如果一个监视器管了 k 个位置,那么在这 k 个位置放一个警报器,某个数 x 大于等于 a_i+\frac{v_i}{k} 就重构。

每个位置要开一个优先队列来存警报器。重构一次 d_x 变为原来的 \frac{k-1}{k},一次重构复杂度为 \mathcal O(k\log q),因此复杂度为 \mathcal O(k\log q\times\log_{\frac{k-1}{k}}V)=\mathcal O(k^2\log q\log V)。

这个东西有点不牛,引入二进制警报器,它的核心思想就是减少警报器的报警阈值种类数。

对于一个监视器,设 p 为最大的满足 k(2^p-1)<d_x 的整数,每个位置放一个警报器,当权值 a_x 经过 2^p 倍数时报警重构(认为 a_x\gets a_x+y 时它经过了 (a_x,a_x+y])。

可以发现一个位置报警两次后一定增加了 2^p,所以报警不超过 2k 次后 p 一定减小,因此总报警次数是 \mathcal O(k\log V) 的,同折半警报器一样。由于不需要优先队列,复杂度优化到 \mathcal O(k^2\log V)。

能不能再给力一点,把重构的 \mathcal O(k) 优化掉?发现 p 可以等到 d_x 可能在不报警的情况下 <0 再更新,而这样的更新一定减小 p,故只有 \log V 次,每次可以暴力 \mathcal O(k) 重构。维护 s_x 表示当前状态的不报警的情况下 d_x 可能达到的最小可能值,每次报警 \mathcal O(1) 更新 s_x,判断是否需要减小 p 即可。

这样添加监视器的均摊复杂度就来到了 \mathcal O(k\log V),同时修改的复杂度是 \mathcal O(\log V)。修改可以通过压位优化到 \mathcal O(1),但看起来没有什么必要。

:::info[[THUPC 2021] 鬼街 的代码] 提交记录。

#include <bits/stdc++.h>
#define REP(i, l, r) for (int i = (l); i <= (r); ++ i)
#define DEP(i, r, l) for (int i = (r); i >= (l); -- i)
#define fi first
#define se second
#define pb emplace_back
#define mems(x, v) memset((x), (v), sizeof(x))
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define ppc(x) __builtin_popcount(x)
using namespace std;
namespace Milkcat {
    typedef long long LL;
    typedef pair<LL, LL> pii;
    const int N = 1e5 + 5;
    LL n, q, o, x, y, ct, lst, al[N], a[N], b[N], h[N];
    vector<int> rs, fc[N], g[N], s[N][31];
    LL nxt(LL x, int y) { return x + (1LL << y) - (x & ((1LL << y) - 1)); }
    LL gap(LL x, int y) { return nxt(x, y) - x - 1; }
    void rbd(int x) {
        b[x] = al[x];
        for (int i : g[x]) b[x] -= a[i];
        if (b[x] <= 0) { h[x] = -1, rs.pb(x); return; }
        while (((1LL << h[x]) - 1) * SZ(g[x]) >= b[x]) h[x] --;
        for (int i : g[x])
            b[x] -= gap(a[i], h[x]), s[i][h[x]].pb(x);
    }
    void inc(int x, LL y) {
        if (!y) return;
        int t = __lg(a[x] ^ (a[x] + y));
        vector<int> p; a[x] += y;
        REP(i, 0, t) {
            for (int z : s[x][i])
                if (h[z] == i) p.pb(z);
            s[x][i].clear();
        }
        for (int i : p) {
            b[i] -= a[x] - nxt(a[x] - y, h[i]) + 1;
            LL w = gap(a[x], h[i]);
            if (b[i] > w) b[i] -= w, s[x][h[i]].pb(i);
            else rbd(i);
        }
    }
    void add(int x, LL y, auto &s) {
        g[x] = s, al[x] = y, h[x] = 30;
        for (int i : s) al[x] += a[i];
        rbd(x);
    }
    int main() {
        cin >> n >> q;
        REP(i, 1, n) {
            int t = i;
            REP(j, 2, t) {
                if (j * j > t) break;
                if (t % j) continue;
                fc[i].pb(j);
                while (t % j == 0) t /= j;
            }
            if (t > 1) fc[i].pb(t);
        }
        REP(te, 1, q) {
            cin >> o >> x >> y, y ^= lst;
            if (o == 1) add(++ ct, y, fc[x]);
            else {
                for (int z : fc[x]) inc(z, y);
                sort(ALL(rs));
                cout << (lst = SZ(rs)) << ' ';
                for (int x : rs) cout << x << ' ';
                cout << '\n', rs.clear();
            }
        }
        return 0;
    }
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int T = 1;
    while (T --) Milkcat::main();
    return 0;
}

:::