题解:P18116 [NOIP2026] 勇者 / brave(民间数据)
Wyh_dailyAC · · 题解
前言
我是场下选手,心态较好,所以十分钟左右氢凇想出了这题。
但是代码写了一坨屎,写了一小时,感觉自己就是唐,写出来一堆大便。
Sol
首先联想到 P10077,然并没有什么联系。
两者的一个区别在于,P10077 可以做跳过的行为,而这题不行,所以这题干掉的魔物一定是呈现为一个连续段的。
再考虑暴力的开销在于,有很多的魔物其实本身可以被一起干掉,而我们选择了一个一个模拟地去干掉。
此时,不难去考虑,当我们可以干掉一个魔物时,我们一定能跟着干掉的有哪些。
考虑在走某一侧时,当我们在当前攻击力为
此时,前方一切的
就这么跳跳跳,如果一直能干,那么
否则,如果有一次跳后终于干不掉了,我们记这个魔物是第
如果我们又一次折返了回去,遇到了
用前缀和去算每次一跳能干掉多少,那么就是线性对数的了。如果用了线段树就大了。
至于对于左侧 / 右侧最近的 set 里,再去二分就可以了。
:::success[Code]{open}
#include <bits/extc++.h>
#define sfr return cerr << "safe\n", 0;
#define sfv return cerr << "safe\n", void();
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using i128 = __int128;
using pr = pair<int, int>;
using prt = tuple<int, int>;
using tpt = tuple<int, int, int>;
using rbT = tree<pr, null_type, less<pr>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e9 + 10, Q = 6e5 + 10;
// constexpr auto lg2(int x) -> int {
// int res = 0;
// for (--x; x; x >>= 1) ++res;
// return res;
// }
// const int M = 2 * Q * lg2(N) + 10;
// struct SegTree {
// ll c[M], add[M];
// int ls[M], rs[M], tot;
// #define lc(u) ls[(u)]
// #define rc(u) rs[(u)]
// auto pushtag(int u, int l, int r, int k) {
// c[u] += (r - l + 1) * k;
// add[u] += k;
// }
// auto pushdown(int u, int l, int r) {
// if (l == r) return;
// int mid = l + ((r - l) >> 1);
// if (!lc(u)) lc(u) = ++tot;
// if (!rc(u)) rc(u) = ++tot;
// pushtag(lc(u), l, mid, add[u]);
// pushtag(rc(u), mid + 1, r, add[u]);
// add[u] = 0;
// }
// auto modify(int u, int nl, int nr, int tl, int tr, int k) {
// if (tl <= nl && nr <= tr) {
// pushtag(u, nl, nr, k);
// return;
// }
// pushdown(u, nl, nr);
// int mid = nl + ((nr - nl) >> 1);
// if (tl <= mid) modify(lc(u), nl, mid, tl, tr, k);
// if (tr > mid) modify(rc(u), mid + 1, nr, tl, tr, k);
// c[u] = c[lc(u)] + c[rc(u)];
// }
// auto query(int u, int nl, int nr, int tl, int tr) {
// if (tl <= nl && nr <= tr) return c[u];
// pushdown(u, nl, nr);
// int mid = nl + ((nr - nl) >> 1); ll ans = 0;
// if (tl <= mid) ans += query(lc(u), nl, mid, tl, tr);
// if (tr > mid) ans += query(rc(u), mid + 1, nr, tl, tr);
// return ans;
// }
// } Seg;
int feiwu, T, n, q, a[Q], x[Q], lst[Q], nxt[Q], b, y; ll r; int px[Q], zy;
ll ans, cnt, S[Q];
auto sol() {
// if (!n) return n = 1, void();
cin >> n >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> x[i], S[i] = S[i - 1] + a[i]; // Seg.modify(1, -1e9, 1e9, x[i], x[i], a[i]);
iota(px + 1, px + n + 1, 1);
sort(px + 1, px + n + 1, [&](const int &u, const int &v) { return a[u] > a[v]; });
set<int> tmp; tmp.emplace(0), tmp.emplace(n + 1);
for (int ii = 1, i, pos = 1; ii <= n; ++ii) {
i = px[ii];
while (pos < n && a[px[pos]] > 2 * a[i]) tmp.emplace(px[pos++]);
auto it = tmp.lower_bound(i);
lst[i] = *prev(it), nxt[i] = *it;
}
while (q--) {
cin >> b >> y >> r;
auto it = lower_bound(x + 1, x + n + 1, y) - x;
// if (it > r) { cout << "0\n"; continue; }
// cnt += x[it] - y;
int L = it - 1, R = it;
ans = b, zy = 1;
while (cnt <= r) {
int i = zy ? R : L;
if (i < 1 || i > n) break;
int dlt = abs(x[i] - y);
if (cnt + dlt > r) break;
cnt += dlt, y = x[i];
if (ans < a[i]) {
int j = zy ? L : R;
if (j < 1 || j > n || ans < a[j]) break;
zy ^= 1;
continue;
}
int j = zy ? nxt[i] - 1 : lst[i] + 1, k = j;
if (zy) {
if (cnt + x[j] - y > r) j = upper_bound(x + i, x + j + 1, y + (r - cnt)) - x - 1;
ans += S[j] - S[i - 1] /* Seg.query(1, -1e9, 1e9, x[i], x[j]) */, R = j + 1;
} else {
if (cnt + y - x[j] > r) j = lower_bound(x + j, x + i + 1, y - (r - cnt)) - x;
ans += S[i] - S[j - 1] /* Seg.query(1, -1e9, 1e9, x[j], x[i]) */, L = j - 1;
}
cnt += abs(x[j] - y), y = x[j];
if (j != k) break;
}
cout << ans << " \n"[q == 0];
cnt = 0;
}
// for (int i = 1; i <= n; ++i) Seg.modify(1, -1e9, 1e9, x[i], x[i], -a[i]);
}
auto main() -> signed {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
// ++Seg.tot;
cin >> feiwu >> T;
while (T--) sol();
}
:::