Pagini recente » Cod sursa (job #3182493) | Cod sursa (job #2670104) | Cod sursa (job #3182492) | Cod sursa (job #2312793) | Cod sursa (job #3233811)
#include<bits/stdc++.h>
std::ifstream fin("stramosi.in");
std::ofstream fout("stramosi.out");
const int MAX = 2.5e5 + 5;
const int LOG_MAX = 18;
int n, m;
int st[LOG_MAX][MAX];
int main(){
fin >> n >> m;
for(int i = 1; i <= n; ++i)
fin >> st[0][i];
for(int i = 1; (1 << i) <= n; ++i)
for(int j = 1; j <= n; ++j)
st[i][j] = st[i - 1][st[i - 1][j]];
int p, q;
for(;m--;){
fin >> q >> p;
for(int i = 0; (1 << i) <= p; ++i)
if(p & (1 << i))
q = st[i][q];
fout << q << '\n';
}
}