跳到正文

算法·理论

二进制警报器

喵仔牛奶约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年8月20日
收录
2026年7月3日
更新
2026年7月3日
篇幅
约 6 分钟
获赞
6

版本

  1. v1当前

给定序列 ana_n,有 qq 次操作

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

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

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

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

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

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

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

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

[THUPC 2021] 鬼街 的代码

提交记录。

C++
#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;
}
原文
存档评论2 条抓取于