跳到正文

题解

P5397 [Ynoi2018] 天降之物 题解

401rk8约 5 分钟
原文
文章信息与版本

文章信息

发布
2022年2月7日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 5 分钟
获赞
22
题目
P5397

版本

  1. v1当前

题面

做法仍是根号分治,相比其他题解避免了冗余的分类讨论,使得思路和代码都简洁许多

记一个数 xx 的出现次数为 siz[x]siz[x]

使用数组 mp 建立真实数值与代码中数值的映射(值 xx 在代码中应为 mp[x]mp[x]),这允许我们在操作 11 中令 siz[x]≤siz[y]siz[x]\le siz[y](若不然,swap(mp[x],mp[y]) 即可等价于将 yy 变成 xx)

设定阈值 BB,把每个数出现的位置分为两部分:主要集合 SS 和附属集合 PP,其中 ∣S∣>B,∣P∣≤B|S|>B,|P|\le B。称 S≠∅S\ne\varnothing 的数为大数,否则为小数

容易发现大数只有 nB\frac{n}{B} 个,可以使用二维数组 ans 保存每个大数只考虑 SS 集对其他数的答案,这部分时空复杂度为 O(n2B)O(\frac{n^{2}}{B})。再使用 vector 保存每个数的 PP 集

修改时先更新每个大数对 yy 的答案,然后合并 PP 集,分两类讨论:

  • xx 为大数:O(n)O(n) 修改所有 xx 为 yy,重构 yy。这种情况不会出现超过大数个数次,复杂度为 O(n2B)O(\frac{n^{2}}{B})
  • xx 为小数:通过 PP 修改所有 xx 为 yy,若 ∣Py∣>B|P_{y}|>B 则重构 yy。复杂度为 O(nB)O(nB)

询问时先考虑 Px,PyP_{x},P_{y} 中位置的答案,若 x,yx,y 中有大数再用 ansans 更新答案

由于常数影响,BB 应略大于 n\sqrt{n},时空复杂度 O(nn)O(n\sqrt{n})

并不卡常因此偷懒用了 std::merge

C++
const int N = 1e5+5, B = 500, BN = N/B+5;
int n,m,ind,lstans,a[N],mp[N],id[N],ans[BN][N];
VI p[N];

void build(int x) { // calculate ans, clear p[x]
	if( !id[x] ) id[x] = ++ind; int u = id[x];
	// the value is x, the index of x is u
	memset(ans[u],0x3f,sizeof ans[u]), ans[u][x] = 0;
	For(i,1,n, len = N)
		if( a[i] == x ) len = 0;
		else ckmin(ans[u][a[i]],++len);
	rFor(i,n,1, len = N)
		if( a[i] == x ) len = 0;
		else ckmin(ans[u][a[i]],++len);
	VI().swap(p[x]);
}

void merge(VI &res,const VI &a,const VI &b) {
	VI c; c.resize(sz(a)+sz(b)); merge(all(a),all(b),c.begin());
	res = c;
}
void mdf(int &x,int &y) {
	if( x == y || !x ) return;
	if( !y ) return y = x, x = 0, void();
	if( id[x] ) swap(x,y);
	For(i,1,ind) ckmin(ans[i][y],ans[i][x]);
	if( id[x] ) { // x is a big number
		For(i,1,n) if( a[i] == x ) a[i] = y;
		build(y);
	} else {
		for(int i : p[x]) a[i] = y;
		merge(p[y],p[x],p[y]);
		if( sz(p[y]) > B ) build(y);
	}
	VI().swap(p[x]), x = 0;
}

int qry(const int &x,const int &y) {
	if( !x || !y ) return -1; if( x == y ) return 0;
	int res = N;
	VI pp; merge(pp,p[x],p[y]);
	Rep(i,1,sz(pp)) if( a[pp[i]] != a[pp[i-1]] ) ckmin(res, pp[i]-pp[i-1]);
	// better method: www.luogu.com.cn/blog/123384/solution-p5397
	// in function `merge_(int,int)`
	if( id[x] ) ckmin(res,ans[id[x]][y]);
	if( id[y] ) ckmin(res,ans[id[y]][x]);
	return res;
}

signed main() {
	read(n,m); For(i,1,n) read(a[i]), mp[a[i]] = a[i], p[a[i]].pb(i);
	For(i,1,n) if( sz(p[i]) > B ) build(i);
	while( m-- ) {
		int op,x,y; read(op,x,y); x^=lstans,y^=lstans;
		if( op == 1 ) mdf(mp[x],mp[y]);
		else {
			lstans = qry(mp[x],mp[y]);
			if( ~lstans ) write(lstans); else lstans = 0, put("Ikaros");
		}
	}
	return ocl();
}
原文
存档评论3 条抓取于