前记:来源于 做题记录 #2
考虑我暴力维护这个东西。首先可以简化一下题意,默认每次一定会减掉一个 ,如果我真能加就加上 ,最后求答案的时候把答案减去 即可。那么对应到它给的礼物,第 个礼物的价值要提高 ,这样才能正确比较。这样我的过程还拥有了当前心情值不减的优美性质。不妨设 。
那么有了这个性质,我们会很想要一个递减的礼物序列。如果礼物序列递减,那么上一个礼物不能买,下一个礼物将也不能买,具有着状态单调性。这样可以二分,找到一个最后一个能买的位置,然后前面全都能买,即得出答案。但是对于相邻的两个礼物 怎么办?
讨论一下这样的对产生的贡献:一开始 的数会被加两次 , 的数会被加一次,其他的根本不加。因此这个操作 等价于 。此时的两次操作是互不干扰的!因此可以调换顺序,变为 。此时右边是比左边小的,做到了改变大小关系。
那么我现在暴力去做这个操作,去维护一个递减的、与原序列等价的序列 ,每次插入 相当于找到第一个 的 ,将其左边插进 ,再找到最后一个 的 ,然后 Assign([i, j], x), Add([j + 1, end], a + b)。此时可以大力平衡树直接 爆掉,但是我不太会平衡树所以没写,当然我们不需要这么优秀的复杂度,写个平衡树还是太困难了(除非你没想到其他东西)。
但是这个我们其实可以做到快速合并两个有序序列!有左右相邻的两个已经变得有序的区间,可以归并地做这个操作:考虑把右侧序列一个一个插入到左侧,而且右侧序列它也是有序的,我做操作是多出来一段 ,所以到 的时候它的 也不会到这个里面,所以可以双指针维护,简单合并即可。合并复杂度是 的。
于是可以二进制分组,查询暴力枚举块然后二分即可,查询为瓶颈,复杂度 。
#pragma GCC optimize("Ofast,unroll-loops")
// STOOOOOOOOOOOOOOOOOOOOOOOOO hzt CCCCCCCCCCCCCCCCCCCCCCCORZ
#include <algorithm>
#include <cassert>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using LL = long long;
using Block = vector<LL>;
using PII = pair<int, int>;
constexpr int kN = 2e5 + 1;
int n, q;
LL A, B, d;
inline void operator+=(Block &a, Block &b) {
int i = 0, j = 0, cnt = 0;
Block ret;
for (; j < b.size(); j++) {
for (; i < a.size() && a[i] + cnt * d >= b[j]; i++)
ret.push_back(a[i] + cnt * d);
cnt++;
for (; i < a.size() && a[i] + cnt * d >= b[j]; i++)
ret.push_back(b[j]);
ret.push_back(b[j]);
}
for (; i < a.size(); ret.push_back(a[i++] + cnt * d));
a.swap(ret), ret.clear();
}
vector<Block> b;
inline void Insert(LL w) {
b.emplace_back(1, w);
for (int sz = b.size() - 1; sz > 0 && b[sz].size() == b[sz - 1].size();) {
b[sz - 1] += b[sz];
b[sz].clear(), b[sz].shrink_to_fit();
b.pop_back(), sz--;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> n >> A >> B, d = A + B;
for (int i = 1, p; i <= n; i++)
cin >> p, Insert(p + (i - 1) * B);
cin >> q;
for (LL op, x; q--;) {
cin >> op >> x;
if (op == 2) {
for (auto &vb : b) {
int l = 0, r = vb.size() - 1, ans = 0;
for (int mid; l <= r;) {
mid = (l + r) / 2;
if (vb[mid] >= x + mid * d)
l = mid + 1, ans = mid + 1;
else
r = mid - 1;
}
x += ans * d;
}
cout << x - 1ll * n * B << '\n';
} else
Insert(x + n * B), n++;
}
return 0;
}Update on 2026.3.7
省选倒闭了,完成一下遗愿。
#pragma GCC optimize("Ofast,unroll-loops")
#include <algorithm>
#include <cassert>
#include <iostream>
#include <numeric>
#include <vector>
#include <chrono>
#include <ctime>
#include <random>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
constexpr int kN = 2e5 + 1;
mt19937 Rand(chrono::steady_clock().now().time_since_epoch().count());
int n, q, A, B;
LL d;
struct Node {
unsigned long key;
int sz, cnt, ls, rs;
LL val, add;
} t[2 * kN];
void PushUp(int x) {
if (x == 0) return;
t[x].sz = t[t[x].ls].sz + t[t[x].rs].sz + t[x].cnt;
}
void PushTag(int x, LL w) { t[x].val += w, t[x].add += w; }
void PushDown(int x) {
if (!t[x].add) return;
PushTag(t[x].ls, t[x].add);
PushTag(t[x].rs, t[x].add), t[x].add = 0;
}
void Split(int x, LL w, int &L, int &R) {
if (!x)
return L = R = 0, void();
PushDown(x);
if (w >= t[x].val)
Split(t[x].ls, w, L, t[R = x].ls);
else
Split(t[x].rs, w, t[L = x].rs, R);
PushUp(x);
}
int Merge(int x, int y) {
if (!x || !y)
return x + y;
if (t[x].key < t[y].key) {
PushDown(y), t[y].ls = Merge(x, t[y].ls);
return PushUp(y), y;
}
PushDown(x), t[x].rs = Merge(t[x].rs, y);
return PushUp(x), x;
}
int rt, CNT, tot;
void Insert(LL x) {
x += 1ll * CNT * B, CNT++;
int p, q, r;
Split(rt, x + 1, p, q), Split(q, x - d, q, r);
int cnt = t[q].sz + 1;
t[++tot] = {Rand(), cnt, cnt, 0, 0, x, 0};
PushTag(r, d);
rt = Merge(Merge(p, tot), r);
}
LL Solve(int x, LL w) {
if (!x)
return w;
PushDown(x);
if (w + t[t[x].ls].sz * d > t[x].val)
return Solve(t[x].ls, w);
w += t[t[x].ls].sz * d;
if (w + t[x].cnt * d > t[x].val)
return w + ((t[x].val - w) / d + 1) * d;
return Solve(t[x].rs, w + t[x].cnt * d);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> n >> A >> B;
d = A + B;
for (int i = 1, x; i <= n; i++)
cin >> x, Insert(x);
cin >> q;
for (LL o, x; q--;) {
cin >> o >> x;
if (o == 1)
Insert(x);
else
cout << Solve(rt, x) - 1ll * CNT * B << '\n';
}
cerr << clock() << '\n';
return 0;
}但是因为二进制分组归并常数真的太小了,俩 跑的比平衡树一个 快一倍,不知道是不是我平衡树写太丑了导致的/hsh