跳到正文

题解

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

XiArk约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年9月29日
收录
2026年9月29日
更新
2026年9月29日
篇幅
约 12 分钟
题目
P12656

版本

  1. v1当前

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

这是一道思路好想但是实现有点让我抓狂的题。(我大概率做复杂了。但毕竟是一种列棋可行的做法)

题意简化:一张横列纵列数都不超过 200000200000 的图上有 NN 个经过就能提供价值的点。将给执行 QQ 次平行于坐标轴的直线移动,求完成后的总收获。

d=0d=0 即对应右移; d=1d=1 即对应上移; d=2d=2 即对应左移; d=3d=3 即对应下移;

做法概括:思路简单但实现复杂。

思维强的或者不想调代码的可以看看其他题解。

第一步

  • 首先,题目提到移动,提到了有一些点可以反复提供价值。但是暴力的话首先存图存不下,其次最差将移动 200000×Q200000 \times Q 的距离,共 4×10104 \times 10^{10} ,显然会TLETLE。

第二步

  • 我们尝试直接对于每次直线移动都一步到位,而非一点点模拟。但是统计就成了问题。我们不能拿二维差分,这显然会 MLEMLE 。
  • 于是我想到了一种极其劣简单的解法。就是那两个 setset 存哪一行(列)有点能提供价值。再用另外两个 setset 存这一行(列)有哪些点能提供价值。这样就能极其费时快速的统计好每一次直线移动提供的价值。我们甚至可以利用 lower_boundlower\_bound 确定边界,以此来优化一下下。
  • 如果你也这么想,恭喜你,你已经想到了一半,并能以 1.5KiB1.5 KiB 的神经代码拿下前三档分,共41分。
  • 评测记录与代码如下
评测记录

代码
C++
#include<bits/stdc++.h>
using namespace std;
const int INF=200005;
int n,Q;
long long ans;
set<int>e1,e2;
set<pair<int,int>>s1[200005],s2[200005];
int main()
{
//	freopen("box.in","r",stdin);
//	freopen("box.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>n>>Q;
	for(int i=1;i<=n;i++)
	{
		int x,y,w;
		cin>>x>>y>>w;
		e1.insert(x);
		s1[x].insert({y,w});
		e2.insert(y);
		s2[y].insert({x,w});
	}
	int sx=1,sy=1;
	while(Q--)
	{
		int d,v;
		cin>>d>>v;//cout<<ans<<"\n";
		if(d==0)
		{
			int ey=sy,ex=sx+v;
			if(s2[sy].empty()){sx=ex,sy=ey;continue;}
			auto it1=s2[sy].lower_bound({sx,INF});
			auto it2=s2[sy].lower_bound({ex,INF});
			while(it1!=it2)
			{
				pair<int,int>p=*(it1++);
				ans+=p.second;
			}
			sx=ex,sy=ey;
		}
		else if(d==2)
		{
			int ey=sy,ex=sx-v;
			if(s2[sy].empty()){sx=ex,sy=ey;continue;}
			auto it1=s2[sy].lower_bound({ex,0});
			auto it2=s2[sy].lower_bound({sx,0});
			while(it1!=it2)
			{
				pair<int,int>p=*(it1++);
				ans+=p.second;
			}
			sx=ex,sy=ey;
		}
		else if(d==1)
		{
			int ex=sx,ey=sy+v;
			if(s1[sx].empty()){sx=ex,sy=ey;continue;}
			auto it1=s1[sx].lower_bound({sy,INF});
			auto it2=s1[sx].lower_bound({ey,INF});
			while(it1!=it2)
			{
				pair<int,int>p=*(it1++);
				ans+=p.second;
			}
			sx=ex,sy=ey;
		}
		else if(d==3)
		{
			int ex=sx,ey=sy-v;
			if(s1[sx].empty()){sx=ex,sy=ey;continue;}
			auto it1=s1[sx].lower_bound({ey,0});
			auto it2=s1[sx].lower_bound({sy,0});
			while(it1!=it2)
			{
				pair<int,int>p=*(it1++);
				ans+=p.second;
			}
			sx=ex,sy=ey;
		}
	}
	cout<<ans;
}

第三步

  • 看着在第四第五档占据半壁江山的TLE,想必你已经知道怎么优化了。没错,就是利用前缀和。
  • 我们只需要对每行每列能提供的价值按坐标从小到大的顺序前缀和,就能轻而易举地减少我们处理每一次直线操作所耗的时间。具体的,从第二步可以知道,我们能轻而易举的求出上下界,自然也能借此求出经过的宝箱所提供的总价值。
  • 那么恭喜你,你已经能去实现这个整整 3KiB3 KiB 的代码了,不过其实很多地方是重复的,复制粘贴就好。
  • 时间复杂度 O((N+Q)logN)O((N + Q) log N) 空间复杂度 O(N)O(N) 不过听大佬说能把setset 换成 vectorvector 优化一下?
  • 评测记录和代码如下
评测记录

代码
C++
#include<bits/stdc++.h>
using namespace std;
const int INF=200005;int n,Q;long long ans;vector<long long>val1[200005],val2[200005];//前缀和
set<pair<int,int>>s1[200005],s2[200005],pos1[200005],pos2[200005];//行列中的点,行列中能提供价值的点在行列中的位置(按坐标从小到大排列,方便前缀和和查询)
set<int>e1,e2;//能提供价值的行列。
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>n>>Q;
	for(int i=1;i<=n;i++)
	{
		int x,y,w;
		cin>>x>>y>>w;
		e1.insert(x);//存哪一行有
		s1[x].insert({y,w});//存该行有什么
		e2.insert(y);//存哪一列有
		s2[y].insert({x,w});//存该列有什么
	}
	for(int x:e1)
	{
		long long now=0,cnt=0;
		for(pair<int,int>p:s1[x])
		{
			now+=p.second;val1[x].push_back(now);//前缀和
			pos1[x].insert({p.first,cnt++});//存对应位置方便查询
		}
	}
	for(int x:e2)//同上
	{
		long long now=0,cnt=0;
		for(pair<int,int>p:s2[x])
		{
			now+=p.second;val2[x].push_back(now);
			pos2[x].insert({p.first,cnt++});
		}
	}
	int sx=1,sy=1;
	while(Q--)
	{
		int d,v;
		cin>>d>>v;//cout<<ans<<"\n";
    //四份其实处理大差不差。
		if(d==0)
		{
			int ey=sy,ex=sx+v;
			if(pos2[sy].empty()){sx=ex,sy=ey;continue;}
			auto it1=pos2[sy].lower_bound({sx,INF});//线段起始点不提供价值,所以我们直接利用pair的比较模式跳过起点
			auto it2=pos2[sy].lower_bound({ex,INF});
			if(it1==pos2[sy].end()){sx=ex,sy=ey;continue;}
			if(it2==pos2[sy].begin()){sx=ex,sy=ey;continue;}
      //经过直线不存在能提供价值的点
			it2--;//上面调成了{ex,INF}会多出一个,要捡回来。同上一行一起检测是否存在能提供价值的点,防止误判。——考场上急了就将就打成这样了
			if(it1==pos2[sy].begin())
			{
				pair<int,int>p2=*it2;
				if(p2.first>ex){sx=ex,sy=ey;continue;}
				ans+=val2[sy][p2.second];
				sx=ex,sy=ey;continue;
			}
			pair<int,int>p1=*(--it1),p2=*it2;
			int l=p1.second,r=p2.second;
			ans+=val2[sy][r]-val2[sy][l];
			sx=ex,sy=ey;
		}
		else if(d==2)//后面大差不差了。慢慢调一下就好。
		{
			int ey=sy,ex=sx-v;
			if(pos2[sy].empty()){sx=ex,sy=ey;continue;}
			auto it1=pos2[sy].lower_bound({ex,0});//包含终点
			auto it2=pos2[sy].lower_bound({sx,0});//这样的话会包含起点,所以待会要--
			if(it1==pos2[sy].end()){sx=ex,sy=ey;continue;}
			if(it2==pos2[sy].begin()){sx=ex,sy=ey;continue;}
			it2--;
			if(it1==pos2[sy].begin())
			{
				pair<int,int>p2=*it2;
				ans+=val2[sy][p2.second];
				sx=ex,sy=ey;continue;
			}
			pair<int,int>p1=*(--it1),p2=*it2;
			int l=p1.second,r=p2.second;
			ans+=val2[sy][r]-val2[sy][l];
			sx=ex,sy=ey;
		}
		else if(d==1)//纵向移动和横向移动基本一致
		{
			int ex=sx,ey=sy+v;
			if(pos1[sx].empty()){sx=ex,sy=ey;continue;}
			auto it1=pos1[sx].lower_bound({sy,INF});
			auto it2=pos1[sx].lower_bound({ey,INF});
			if(it1==pos1[sx].end()){sx=ex,sy=ey;continue;}
			if(it2==pos1[sx].begin()){sx=ex,sy=ey;continue;}
			it2--;
			if(it1==pos1[sx].begin())
			{
				pair<int,int>p2=*it2;
				if(p2.first>ey){sx=ex,sy=ey;continue;}
				ans+=val1[sx][p2.second];
				sx=ex,sy=ey;continue;
			}
			pair<int,int>p1=*(--it1),p2=*it2;
			int l=p1.second,r=p2.second;
			ans+=val1[sx][r]-val1[sx][l];
			sx=ex,sy=ey;
		}
		else if(d==3)
		{
			int ex=sx,ey=sy-v;
			if(pos1[sx].empty()){sx=ex,sy=ey;continue;}
			auto it1=pos1[sx].lower_bound({ey,0});
			auto it2=pos1[sx].lower_bound({sy,0});
			if(it1==pos1[sx].end()){sx=ex,sy=ey;continue;}
			if(it2==pos1[sx].begin()){sx=ex,sy=ey;continue;}
      it2--;
			if(it1==pos1[sx].begin())
			{
				pair<int,int>p2=*it2;
				ans+=val1[sx][p2.second];
				sx=ex,sy=ey;continue;
			}
			pair<int,int>p1=*(--it1),p2=*it2;
			int l=p1.second,r=p2.second;
			ans+=val1[sx][r]-val1[sx][l];
			sx=ex,sy=ey;
		}
	}
	cout<<ans;
}

总结

呼,打完啦。虽然本篇题解的做法极其容易理解,但是其实现细节极其多。思维强的或者不想调代码的可以看看其他题解。若在按本篇题解的思路打代码时遇到问题,可以随时私信。

原文