Cod sursa(job #2649534)
| Utilizator | Data | 15 septembrie 2020 09:06:25 | |
|---|---|---|---|
| Problema | Stramosi | Scor | 90 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva de probleme | Marime | 0.65 kb |
#include <fstream>
#include <cmath>
using namespace std;
ifstream cin("stramosi.in");
ofstream cout("stramosi.out");
int N, str[250100][18], lim;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int M,x,Q,P;
cin>>N>>M;
lim=log2(N);
for(int i=1; i<=N; ++i)
cin>>str[i][0];
for(int exp=1; exp<=lim; ++exp)
for(int j=1; j<=N; ++j)
str[j][exp]=str[str[j][exp-1]][exp-1];
while(M--){
cin>>Q>>P;
x=0;
while(P) {
if(P&1)
Q=str[Q][x];
P>>=1; ++x;
}
cout<<Q<<'\n';
}
return 0;
}
