题解:P12656 [KOI 2023 Round 1] 道具获取
违规用户名1448886 · · 题解
Luogu P12656 题解
很权威的算法题。
题意 \texttt{Introduction}
二维图上有
给你
思路 \texttt{Idea}
子任务 1、2
对于子任务 1,开二维数组存储,按题意模拟即可。
恭喜,获得 9 分的高分!
考虑用 map 存点,这样就不会爆空间了。子任务 2 可以这么过。
时间复杂度
子任务 3、4、5
不妨考虑一个优化:遍历路径上的所有点太费时间了,考虑预处理每一行和每一列的道具数,然后在操作时调用数据就好了。
分组存储:
- 按
x 坐标分组:用xg_a 存储所有x 坐标为a 的箱子,每个元素为(b,w) (y 坐标和道具数); - 按
y 坐标分组:用yg_b 存储所有y 坐标为b 的箱子,每个元素为(a,w) (x 坐标和道具数)。
为了方便查询,考虑在组内排个序来优化。
现在就是到了查询数量的时候了。
对于查询无修改的区间和,你想到了什么?
线段树! 当然是前缀和了(线段树可以,但我认为有点复杂,并且这题还有“前缀和”的 tag)。
为每个分组构建前缀和数组(
以
计算,时间复杂度是
问题来了:我们在移动时不一定是从箱子开始的,如何快速找到前缀和区间?
请您向上翻这篇文章,看到这句话:
为了方便查询,考虑在组内排个序来优化。
都排序了,不用二分是不是不礼貌了?
通过二分查找,我们可以快速找到这一段路程被哪几个箱子组成的区间覆盖,从而计算道具数量。
二分可以这么办:
- 用
upper_bound找到分组中第一个大于等于区间左边界的元素位置l ; - 用
lower_bound找到分组中第一个大于区间右边界的元素位置r 。
:::info[为什么找
| 会减去 |
|---|
:::info[两函数的使用说明] 上述两函数可以通过下面的操作来进行二分:
lower_bound(a.begin(), a.end(), k);
表示在序列
upper_bound(a.begin(), a.end(), k);
| 表示在序列 |
|---|
现在考虑移动过程:(如下表,当前坐标为
总而言之,二分找区间后前缀和查询就可以了。
代码复杂度分析
- 空间复杂度
- 预处理和前缀和使用的是
vector,只有O(N) 的空间复杂度。
- 预处理和前缀和使用的是
- 时间复杂度
- 预处理阶段
- 分组存储:
O(N) ; - 排序:每个箱子最多参与两次(横竖各一次),为
O(N\log N) ; - 前缀和:每个箱子最多算两次,为
O(N) 。
- 分组存储:
- 查询阶段
- 每查询一次要二分一次,为
O(\log M) ,其中M 为组内箱子数,有M\le N ; - 前缀和一次:
O(1) ; - 不妨认为
M=N ,这部分时间复杂度为O(Q \log N) 。
- 每查询一次要二分一次,为
- 预处理阶段
总结:空间复杂度
代码 \texttt{Code}
感觉数据有点水,两个函数即使搭配错了也是可以过的。
:::success[
#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[
#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。