题解:P12656 [KOI 2023 Round 1] 道具获取

· · 题解

第一眼看到题目的数据范围,发现 x、y 的取值范围很大,查询次数也达到了 3 \times 10^5。每次查询的时间复杂度要控制在 O(\log n) 以内。

观察题目可以发现一个重要特征:汽车的移动方向始终限定在水平方向或竖直方向。

所以问题可以转化为:如何快速统计水平或竖直区间上的点权之和。

虽然坐标范围很大,但由于只有 n 个箱子,我们可以用 map 按行和按列分别存储箱子:在行 y 中存储所有箱子的 (x, w),在列 x 中存储所有箱子的 (y, w)。

map<int, vector<pair<int, int>>> mp1, mp2;

对每一行和每一列中的箱子按坐标排序,并构造权值前缀和数组。这样,一旦确定区间两端对应的下标,就能在 O(1) 时间内求出这次移动能获得的道具数量。

由于坐标已经排好序,可以用二分查找确定区间两端对应的下标。每次查询的时间复杂度为 O(\log n)。

注意,起点位置的箱子不计入,只统计终点及途中经过位置的箱子。各个方向的查询区间见代码注释。

实现代码

#include <iostream>
#include <map>
#include <vector>
#include <climits>
#include <algorithm>
#define int long long
using namespace std;

int ans = 0;
map<int, vector<long long>> pre1, pre2;
map<int, vector<pair<int,int>>> mp1, mp2;

bool cmp ( pair<int,int> a, pair<int,int> b ) {
    return a.first < b.first;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    for ( int i = 1; i <= n; i++ ) {
        int x, y, w;
        cin >> x >> y >> w;
        mp1[x].push_back({y, w});
        mp2[y].push_back({x, w});
    }
    for ( auto &kv : mp1 ) {
        int key = kv.first;
        auto &v = kv.second;
        sort(v.begin(), v.end(), cmp);
        auto &pf = pre1[key];
        pf.assign(v.size() + 1, 0LL);
        for ( size_t i = 0; i < v.size(); i++ )
            pf[i + 1] = pf[i] + v[i].second;
    }
    for ( auto &kv : mp2 ) {
        int key = kv.first;
        auto &v = kv.second;
        sort(v.begin(), v.end(), cmp);
        auto &pf = pre2[key];
        pf.assign(v.size() + 1, 0LL);
        for ( size_t i = 0; i < v.size(); i++ )
            pf[i + 1] = pf[i] + v[i].second;
    }
    int sx = 1, sy = 1;
    for ( int i = 1; i <= q; i++ ) {
        int d, v;
        cin >> d >> v;
        if ( d == 0 ) { 
            auto &now = mp2[sy];
            if ( !now.empty() ) {
                int L = sx + 1, R = sx + v;
                if ( L <= R ) {
                    int l = lower_bound(now.begin(), now.end(), make_pair(L, -1), cmp) - now.begin();
                    int r = upper_bound(now.begin(), now.end(), make_pair(R, INT_MAX), cmp) - now.begin();
                    auto &pf = pre2[sy];
                    ans += pf[r] - pf[l];
                }
            }
            sx += v;
        }
        else if ( d == 1 ) { 
            auto &now = mp1[sx];
            if ( !now.empty() ) {
                int L = sy + 1, R = sy + v;
                if ( L <= R ) {
                    int l = lower_bound(now.begin(), now.end(), make_pair(L, -1), cmp) - now.begin();
                    int r = upper_bound(now.begin(), now.end(), make_pair(R, INT_MAX), cmp) - now.begin();
                    auto &pf = pre1[sx];
                    ans += pf[r] - pf[l];
                }
            }
            sy += v;
        }
        else if ( d == 2 ) { 
            auto &now = mp2[sy];
            if ( !now.empty() ) {
                int L = sx - v, R = sx - 1;
                if ( L <= R ) {
                    int l = lower_bound(now.begin(), now.end(), make_pair(L, -1), cmp) - now.begin();
                    int r = upper_bound(now.begin(), now.end(), make_pair(R, INT_MAX), cmp) - now.begin();
                    auto &pf = pre2[sy];
                    ans += pf[r] - pf[l];
                }
            }
            sx -= v;
        }
        else { 
            auto &now = mp1[sx];
            if ( !now.empty() ) {
                int L = sy - v, R = sy - 1;
                if ( L <= R ) {
                    int l = lower_bound(now.begin(), now.end(), make_pair(L, -1), cmp) - now.begin();
                    int r = upper_bound(now.begin(), now.end(), make_pair(R, INT_MAX), cmp) - now.begin();
                    auto &pf = pre1[sx];
                    ans += pf[r] - pf[l];
                }
            }
            sy -= v;
        }
    }
    cout << ans << '\n';
    return 0;
}