Pagini recente » Cod sursa (job #684908) | Cod sursa (job #1146720) | Cod sursa (job #3240529) | Cod sursa (job #570205) | Cod sursa (job #2543342)
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
class Parser {
private:
static const int SIZE = 1e5;
char str[SIZE];
int ptr;
FILE *fin;
char getChar() {
if (ptr == SIZE) {
fread(str, sizeof(char), SIZE, fin);
ptr = 0;
}
return str[ptr++];
}
int getInt() {
char chr = getChar();
while (!isdigit(chr) && chr != '-')
chr = getChar();
int sgn = +1;
if (chr == '-') {
sgn = -1;
chr = getChar();
}
int nr = 0;
while (isdigit(chr)) {
nr = nr * 10 + chr - '0';
chr = getChar();
}
return nr * sgn;
}
public:
Parser(const char* str) :
ptr(SIZE), fin(fopen(str, "r")) { }
~Parser() {
fclose(fin);
}
friend Parser& operator>>(Parser& in, int& nr) {
nr = in.getInt();
return in;
}
};
Parser fin("stramosi.in");
ofstream fout("stramosi.out");
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
typedef pair<int,int> pii;
typedef long long int ll;
typedef long double ld;
const int DMAX = 25e4+10;
int dp[DMAX][20];
int n,m;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int i,j;
int node,put,p,cate;
fin>>n>>m;
for(i=1;i<=n;i++)
fin>>dp[i][0];
for(i=1;i<=17;i++)
for(j=1;j<=n;j++)
dp[j][i]=dp[dp[j][i-1]][i-1];
while(m--){
fin>>node>>p;
for(i=17;i>=0;i--)
if((1<<i)<=p){
p-=(1<<i);
node=dp[node][i];
}
fout<<node<<'\n';
}
return 0;
}