跳到正文

题解

题解:P18116 [NOIP2026] 勇者 / brave(民间数据)

Wyh_dailyAC约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年11月28日
收录
2026年11月28日
更新
2026年11月28日
篇幅
约 11 分钟
题目
P18116

版本

  1. v1当前

前言

我是场下选手,心态较好,所以十分钟左右氢凇想出了这题。

image.png

image.png

image.png

但是代码写了一坨屎,写了一小时,感觉自己就是唐,写出来一堆大便。

Sol

首先联想到 P10077,然并没有什么联系。

两者的一个区别在于,P10077 可以做跳过的行为,而这题不行,所以这题干掉的魔物一定是呈现为一个连续段的。

再考虑暴力的开销在于,有很多的魔物其实本身可以被一起干掉,而我们选择了一个一个模拟地去干掉。

此时,不难去考虑,当我们可以干掉一个魔物时,我们一定能跟着干掉的有哪些。

考虑在走某一侧时,当我们在当前攻击力为 bb 时遇到了 ii 号魔物,此时如果 ii 魔物能被干掉,那么有 b≥aib\ge a_i 且 b←b+aib\leftarrow b+a_i。

此时,前方一切的 ≤2ai\le2a_i 的魔物都可以被干掉,因此我们可以把这些魔物全部吸纳进来并跳过,也就是直接跳到下一个 >2ai>2a_i 的魔物。

就这么跳跳跳,如果一直能干,那么 bb 的量级将会分别达到 2ai,4ai,8ai,⋯2a_i,4a_i,8a_i,\cdots,这显然是不断相较于我们遇到的 aia_i 翻倍的。

否则,如果有一次跳后终于干不掉了,我们记这个魔物是第 pp 个,那么此时我们就折返回去。

如果我们又一次折返了回去,遇到了 pp,且干掉了 pp,那么我们依旧又相对于这个 apa_p 翻了一倍,而 apa_p 又是我们第一次遇到的 aia_i 的 2k2^k 倍多些,因此我们在一侧这么多次跳的量级是最多 log⁡\log 的;否则,如果没干掉,那么就得特判退出去,因为两头堵卡死了。

用前缀和去算每次一跳能干掉多少,那么就是线性对数的了。如果用了线段树就大了。

至于对于左侧 / 右侧最近的 >2ai>2a_i 的位置,可以先把魔物按照 aia_i 降序,从前往后扫,然后双指针把目前 >2ai>2a_i 的位置全塞 set 里,再去二分就可以了。


Code
C++
#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();
}
原文