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

· · 题解

Luogu P12656 题解

很权威的算法题。

题意 \texttt{Introduction}

二维图上有 N 个箱子,经过第 i 个箱子可以获得 w_i 个道具。汽车行进路线上的起点除外的所有道具都可以被拿到。

给你 Q 个操作,按题意模拟,问最后可以拿到几个道具。

思路 \texttt{Idea}

子任务 1、2

对于子任务 1,开二维数组存储,按题意模拟即可。

恭喜,获得 9 分的高分!

考虑用 map 存点,这样就不会爆空间了。子任务 2 可以这么过。

时间复杂度 O\left(\sum\limits_{i=1}^{Q}v_i\right)。

子任务 3、4、5

不妨考虑一个优化:遍历路径上的所有点太费时间了,考虑预处理每一行和每一列的道具数,然后在操作时调用数据就好了。

分组存储:

为了方便查询,考虑在组内排个序来优化。

现在就是到了查询数量的时候了。

对于查询无修改的区间和,你想到了什么?

线段树! 当然是前缀和了(线段树可以,但我认为有点复杂,并且这题还有“前缀和”的 tag)。

为每个分组构建前缀和数组(xp_a 对应 xg_a,yp_b 对应 yg_b),用于快速计算区间内的道具总和。

以 x 坐标为例:xp_{a,i} 表示 xg_a 中前 i 个箱子的道具总和,区间 (l,r] 的总和可通过

xp_{a,r} - xp_{a,l}

计算,时间复杂度是 O(1) 的。

问题来了:我们在移动时不一定是从箱子开始的,如何快速找到前缀和区间?

请您向上翻这篇文章,看到这句话:

为了方便查询,考虑在组内排个序来优化。

都排序了,不用二分是不是不礼貌了?

通过二分查找,我们可以快速找到这一段路程被哪几个箱子组成的区间覆盖,从而计算道具数量。

二分可以这么办:

:::info[为什么找 l 时要取等?] 因为之前提到的前缀和计算式子:

xp_{a,r} - xp_{a,l}
会减去 l 这个位置的数量,与不计算开头的要求相符。

:::info[两函数的使用说明] 上述两函数可以通过下面的操作来进行二分:

lower_bound(a.begin(), a.end(), k);

表示在序列 a 中找到第一个大于等于 k 的元素位置。

upper_bound(a.begin(), a.end(), k);
表示在序列 a 中找到第一个大于 k 的元素位置。

现在考虑移动过程:(如下表,当前坐标为 (a,b),移动 v 格) | 移动方向 | 固定坐标 | 查询分组 | 变量坐标 | |:-:|:-:|:-:|:-:| | d=0 | y 坐标 | yg_b | x\in (a,a+v] | | d=1 | x 坐标 | xg_a | y\in (b,b+v] | | d=2 | y 坐标 | yg_b | x\in [a-v,a) | | d=3 | x 坐标 | xg_a | y\in [b-v,b) |

总而言之,二分找区间后前缀和查询就可以了。

代码复杂度分析

总结:空间复杂度 O(N),时间复杂度 O(N\log N+Q\log N),是可以过的。

代码 \texttt{Code}

感觉数据有点水,两个函数即使搭配错了也是可以过的。

:::success[\text{AC Code 1}(正确搭配)] AC 记录。

#include<bits/stdc++.h>
using namespace std;
const int MAX=200005;
vector<pair<int,int> > xg[MAX];//xg[x]存储(x,y)的y和w
vector<pair<int,int> > yg[MAX];//yg[y]存储(x,y)的x和w
vector<long long> xp[MAX];//xp[x]对应xg[x]的前缀和
vector<long long> yp[MAX];//yp[y]对应yg[y]的前缀和

long long qry(const vector<pair<int,int>>& g,const vector<long long>& p,int L,int R)
{
    if(g.empty()) return 0;
    int l=lower_bound(g.begin(),g.end(),make_pair(L,0))-g.begin();
    int r=upper_bound(g.begin(),g.end(),make_pair(R,INT_MAX))-g.begin();
    if(l>=r) return 0;
    return p[r]-p[l];
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N,Q;
    cin>>N>>Q;

    for(int i=0;i<N;i++)
    {
        int x,y,w;
        cin>>x>>y>>w;
        xg[x].emplace_back(y,w);
        yg[y].emplace_back(x,w);
    }

    //处理x组排序和前缀和
    for(int x=1;x<MAX;x++)
    {
        if(xg[x].empty()) continue;
        sort(xg[x].begin(),xg[x].end());
        int len=xg[x].size();
        xp[x].resize(len+1,0);
        for(int i=0;i<len;i++)
          xp[x][i+1]=xp[x][i]+xg[x][i].second;
    }

    //处理y组排序和前缀和
    for(int y=1;y<MAX;y++)
    {
        if(yg[y].empty()) continue;
        sort(yg[y].begin(),yg[y].end());
        int len=yg[y].size();
        yp[y].resize(len+1,0);
        for(int i=0;i<len;i++)
          yp[y][i+1]=yp[y][i]+yg[y][i].second;
    }

    int cx=1,cy=1;
    long long tot=0;
    while(Q--)
    {
        int d,v;
        cin>>d>>v;
        if(d==0)
        {
            int ex=cx+v;
            tot+=qry(yg[cy],yp[cy],cx+1,ex);
            cx=ex;
        }
        else if(d==1)
        {
            int ey=cy+v;
            tot+=qry(xg[cx],xp[cx],cy+1,ey);
            cy=ey;
        }
        else if(d==2)
        {
            int ex=cx-v;
            tot+=qry(yg[cy],yp[cy],ex,cx-1);
            cx=ex;
        }
        else if(d==3)
        {
            int ey=cy-v;
            tot+=qry(xg[cx],xp[cx],ey,cy-1);
            cy=ey;
        }
    }

    cout<<tot<<endl;
    return 0;
}

::: :::success[\text{AC Code 2}(错误搭配)] AC 记录。

#include<bits/stdc++.h>
using namespace std;
const int MAX=200005;
vector<pair<int,int> > xg[MAX];//xg[x]存储(x,y)的y和w
vector<pair<int,int> > yg[MAX];//yg[y]存储(x,y)的x和w
vector<long long> xp[MAX];//xp[x]对应xg[x]的前缀和
vector<long long> yp[MAX];//yp[y]对应yg[y]的前缀和

long long qry(const vector<pair<int,int>>& g,const vector<long long>& p,int L,int R)
{
    if(g.empty()) return 0;
    int l=upper_bound(g.begin(),g.end(),make_pair(L,0))-g.begin();
    int r=lower_bound(g.begin(),g.end(),make_pair(R,INT_MAX))-g.begin();
    if(l>=r) return 0;
    return p[r]-p[l];
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N,Q;
    cin>>N>>Q;

    for(int i=0;i<N;i++)
    {
        int x,y,w;
        cin>>x>>y>>w;
        xg[x].emplace_back(y,w);
        yg[y].emplace_back(x,w);
    }

    //处理x组排序和前缀和
    for(int x=1;x<MAX;x++)
    {
        if(xg[x].empty()) continue;
        sort(xg[x].begin(),xg[x].end());
        int len=xg[x].size();
        xp[x].resize(len+1,0);
        for(int i=0;i<len;i++)
          xp[x][i+1]=xp[x][i]+xg[x][i].second;
    }

    //处理y组排序和前缀和
    for(int y=1;y<MAX;y++)
    {
        if(yg[y].empty()) continue;
        sort(yg[y].begin(),yg[y].end());
        int len=yg[y].size();
        yp[y].resize(len+1,0);
        for(int i=0;i<len;i++)
          yp[y][i+1]=yp[y][i]+yg[y][i].second;
    }

    int cx=1,cy=1;
    long long tot=0;
    while(Q--)
    {
        int d,v;
        cin>>d>>v;
        if(d==0)
        {
            int ex=cx+v;
            tot+=qry(yg[cy],yp[cy],cx+1,ex);
            cx=ex;
        }
        else if(d==1)
        {
            int ey=cy+v;
            tot+=qry(xg[cx],xp[cx],cy+1,ey);
            cy=ey;
        }
        else if(d==2)
        {
            int ex=cx-v;
            tot+=qry(yg[cy],yp[cy],ex,cx-1);
            cx=ex;
        }
        else if(d==3)
        {
            int ey=cy-v;
            tot+=qry(xg[cx],xp[cx],ey,cy-1);
            cy=ey;
        }
    }

    cout<<tot<<endl;
    return 0;
}

:::

后记 \texttt{End}

去问了下 ruo 怎么写,他说是离散化,我当场石化,差点把代码全删了。

很不错的题目,但有点吃函数的积累。不知道 lower_bound 和 upper_bound 的可能会被手写二分肘飞。

感谢管理员的细心审核,以及能看到这的您。

能点个赞吗?QWQ。