Question 2: skateboard shoes (walk.cpp / walk.in/ walk.out)
background
My skateboard shoes fashion fashion's most stylish
way home I could not help
rubbing friction
in this smooth friction on the ground
in the moonlight I saw his shadow sometimes close and sometimes very far
to feel a driving force in my footsteps
have dark skateboard shoes We are not afraid
description
You buy a pair of skateboard shoes fashion charm are, you are very excited to friction everywhere!
Ningning would like to ask a question: according to the way you act, you can step from one node K friction (move) to the destination.
This is obviously a very simple question, but always ask Ningning konjac non-stop, so you decided to write a program to answer his questions.
Input Format
The first line of the two numbers n, m represents a number of nodes and the number of interrogation
next n lines, a number of i-th number of a [i] denotes the i-th word you node, moved to the next will be a [i] nodes
Subsequently m lines of two numbers t, k, konjac hzwer you asked if the t-th node, then the first k steps to several nodes you
Output Format
m behavior of each result of the inquiry
SAMPLE INPUT
3 2
2
3
2
1 2
2 4
Sample Output
3
2
Remark
A total of 10 test points, the data size of each test point is as follows
1.n ^ 2 = 10, m = n-, K <2 ^ 10 =
2.n. 3 ^ = 10, m = n-, K <= 10 ^ . 3
3.n ^ = 10. 4, m =. 1, K <= 10 ^. 9
4.n. 5 ^ = 10, m =. 1, K <= 10 ^. 9
5.n ^. 5 = 10, m =. 1, K <= 10 ^ 12 is
6.n ^. 5 = 10, m =. 1, K <10 ^ 15 =
7.n. 5 ^ = 10, m =. 1, K <= 10 ^ 18 is
8. N ^ = 10. 5, m n-=, K <= 10 ^ 12 is
9 .n ^. 5 = 10, m = n-, K <= 10 ^ 15
10.n ^. 5 = 10, m = n-, K <= 10 ^ 18 is
#include<cstdio>
#include<cmath>
#define int long long
using namespace std;
inline void input(int &x){
int ans=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
ans=ans*10+c-'0';
c=getchar();
}
x=ans*f;
}
inline void output(int x){
if(x<0)putchar('-'),x=-x;
if(x>9)output(x/10);
putchar(x%10+'0');
}
inline void writeln(int x){
output(x);
putchar('\n');
}
int n,m,a[100005],f[100005][65],bin[65];
signed main(){
freopen("walk.in","r",stdin);
freopen("walk.out","w",stdout);
input(n);input(m);
for(int i=1;i<=n;i++){
input(a[i]);
f[i][0]=a[i];
}bin[0]=1;
for(int i=1;i<=62;i++){
bin[i]=bin[i-1]<<1;
for(int j=1;j<=n;j++){
f[j][i]=f[f[j][i-1]][i-1];
}
}
while(m--){
int t,k,ans=0;
input(t);input(k);
for(int i=62;i>=0;i--){
if(k&bin[i])t=f[t][i];
}
writeln(t);
}
}
Notes: 1. Note that the operational position with k & bin [i] judged way position
2. deposit bin 1 << i, otherwise get out too
3. longlong to open to 62 (2 ^ 63-1), int to 30 (2 ^ 31-1), unsignedlonglong to 63 (2 ^ 64-1)