跳到正文

题解

solution-p3988

flyfeather☆约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P3988

版本

  1. v2当前
  2. v1

随机跳题,看了一圈大家都是平衡树和线段树。

感觉分块应该可以啊?

把数列分块,每次先跳大块,再跳零散点。记录下每个块的大小以及第一个点,每次跳的时候可以先回退到当前块的第一个点(细节好写一点)。

复杂度 O(nn)O(n\sqrt n),吸氧过了。

C++
#include <bits/stdc++.h>
const int N=700005;
int n,nxt[N],pre[N],fir[N],siz[N],x;
int read(){
	int t=0;
	char ch=getchar();
	while (ch<'0' || ch>'9') ch=getchar();
	while (ch>='0' && ch<='9') t=t*10+ch-'0',ch=getchar();
	return t;
}
int main(){
	n=read();
	int block=(int)sqrt(2*n);
	for (int i=1;i<n;i++) nxt[i]=i+1,pre[i+1]=i;
	nxt[n]=1,pre[1]=n;
	int tot=0;
	for (int l=1,r=block;l<=n;l+=block,r+=block,tot++){
		if (r>n) r=n;
		siz[tot]=r-l+1;
		fir[tot]=l;
	}
	int p=1,now=0;
	for (int i=1,res=n;i<=n;i++,res--){
		x=read();
		x=x%res;
		int t=(p-1)/block;
		x+=now;
		while (x>=siz[t]){
			x-=siz[t];
			t=(t+1)%tot;
		}
		now=0;
		p=fir[t];
		while (x){
			p=nxt[p];
			now++,x--;
		}
		printf("%d\n",p);
		nxt[pre[p]]=nxt[p];
		pre[nxt[p]]=pre[p];
		siz[t]--;
		if (p==fir[t]) fir[t]=nxt[fir[t]];
	}
}
原文