Pagini recente » Cod sursa (job #3002228) | Cod sursa (job #2248125) | Cod sursa (job #2244457) | Cod sursa (job #2741194) | Cod sursa (job #3123737)
#include <bits/stdc++.h>
using namespace std;
int main() {
ifstream cin("stramosi.in");
ofstream cout("stramosi.out");
int n, m; cin >> n >> m;
vector<int> par(n + 1);
for (int i = 1; i <= n; ++i)
cin >> par[i];
vector<int> qv(m), qk(m);
for (int i = 0; i < m; ++i)
cin >> qv[i] >> qk[i];
for (int d = 0; d < 18; ++d) {
for (int i = 0; i < m; ++i)
if (qk[i] & (1 << d))
qv[i] = par[qv[i]];
for (int i = n; i >= 1; --i)
par[i] = par[par[i]];
}
for (int i = 0; i < m; ++i)
cout << qv[i] << "\n";
return 0;
}