二进制警报器
给定序列
a_n ,有q 次操作
- 单点加上一个数;
- 给定
S,v ,添加一个监视器,当\sum_{i\in S}a_i\ge v 时输出这个监视器的编号。
大家想必都知道折半警报器:对于一个监视器,记
每个位置要开一个优先队列来存警报器。重构一次
这个东西有点不牛,引入二进制警报器,它的核心思想就是减少警报器的报警阈值种类数。
对于一个监视器,设
可以发现一个位置报警两次后一定增加了
能不能再给力一点,把重构的
这样添加监视器的均摊复杂度就来到了
:::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;
}
:::