给定序列 ,有 次操作
- 单点加上一个数;
- 给定 ,添加一个监视器,当 时输出这个监视器的编号。
大家想必都知道折半警报器:对于一个监视器,记 ,如果一个监视器管了 个位置,那么在这 个位置放一个警报器,某个数 大于等于 就重构。
每个位置要开一个优先队列来存警报器。重构一次 变为原来的 ,一次重构复杂度为 ,因此复杂度为 。
这个东西有点不牛,引入二进制警报器,它的核心思想就是减少警报器的报警阈值种类数。
对于一个监视器,设 为最大的满足 的整数,每个位置放一个警报器,当权值 经过 倍数时报警重构(认为 时它经过了 )。
可以发现一个位置报警两次后一定增加了 ,所以报警不超过 次后 一定减小,因此总报警次数是 的,同折半警报器一样。由于不需要优先队列,复杂度优化到 。
能不能再给力一点,把重构的 优化掉?发现 可以等到 可能在不报警的情况下 再更新,而这样的更新一定减小 ,故只有 次,每次可以暴力 重构。维护 表示当前状态的不报警的情况下 可能达到的最小可能值,每次报警 更新 ,判断是否需要减小 即可。
这样添加监视器的均摊复杂度就来到了 ,同时修改的复杂度是 。修改可以通过压位优化到 ,但看起来没有什么必要。
[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;
}