跳到正文

个人记录

士大夫但是是富人

原文

这是 2026年7月16日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年7月16日
更新
2026年7月16日
篇幅
约 1 分钟

版本

  1. v4当前
  2. v3
  3. v2
  4. v1
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5;
int t[N],f[N][31],n,q;
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		cin>>f[i][0];
	}
	for(int i=1;i<=30;i++){
		for(int j=1;j<=n;j++){
			f[j][i]=f[f[j][i-1]][i-1];
		}
	}
	while(q--){
		int x,y;
		cin>>x>>y;
		for(int i=0;i<=30;i++){
			if(y&(1<<i)){
				x=f[x][i];
			}
		}
		cout<<x<<endl;
	} 
	return 0;
}