Pagini recente » Cod sursa (job #1752473) | Cod sursa (job #2774168) | Cod sursa (job #2468392) | Cod sursa (job #1197939) | Cod sursa (job #2252787)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#define CHECK(x) if(!(x)) return false;
#define CHECKRET(x, y) if(!(x)) return (y);
#define SKIP(x) if((x)) continue;
#ifdef INFOARENA
#define ProblemName "stramosi"
#endif
#define MCONCAT(A, B) A B
#ifdef ProblemName
#define InFile MCONCAT(ProblemName, ".in")
#define OuFile MCONCAT(ProblemName, ".out")
#else
#define InFile "fis.in"
#define OuFile "fis.out"
#endif
const int MAXBUF = 30000000;
char parseBuf[MAXBUF];
char *head;
bool isDigit[255];
void parseInit() {
int a = fread(parseBuf, 1, MAXBUF, stdin);
parseBuf[a] = 0;
head = parseBuf;
memset(isDigit, 0, sizeof isDigit);
for (int i = '0'; i <= '9'; ++i)
isDigit[i] = true;
}
int nextInt() {
int ans = 0;
for (; !isDigit[*head]; ++head);
for (; isDigit[*head]; ++head)
ans = ans * 10 + (*head) - '0';
return ans;
}
const int MAXN = 250010;
const int MAXLOG = 20;
int p[MAXLOG][MAXN];
int N;
int main() {
assert(freopen(InFile, "r", stdin));
assert(freopen(OuFile, "w", stdout));
parseInit();
N = nextInt();
int Q = nextInt();
memset(p, 0xFF, sizeof p);
for (int i = 0; i < N; ++i) {
p[0][i] = nextInt();
--p[0][i];
}
for (int l = 1; l < MAXLOG; ++l)
for (int i = 0; i < N; ++i) {
SKIP(p[l - 1][i] < 0);
p[l][i] = p[l - 1][p[l - 1][i]];
}
while (Q--) {
int x = nextInt(), nr = nextInt();
--x;
for (int i = MAXLOG - 1; i >= 0 && x >= 0; --i) {
int pw = (1 << i);
SKIP(pw > nr);
nr -= pw;
x = p[i][x];
}
printf("%d\n", x + 1);
}
return 0;
}