Pagini recente » Cod sursa (job #1418725) | Cod sursa (job #497923) | Cod sursa (job #531031) | Cod sursa (job #2401052) | Cod sursa (job #3297633)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int RMQ[250001][18];
int query(int x, int d) {
int p=0;
while(d) {
if(d&1) x=RMQ[x][p];
d>>=1;
p++;
}
return x;
}
int main(){
int n,m;
fin>>n>>m;
int col=0,cn=n;
while(cn)
{
cn>>=1;
col++;
}
for(int i=1;i<=n;i++)
fin>>RMQ[i][0];
for(int j=1;j<col;j++)
for(int i=1;i<=n;i++)
RMQ[i][j]=RMQ[RMQ[i][j-1]][j-1];
int x,y;
for(int i=1;i<=m;i++){
fin>>x>>y;
fout<<query(x,y)<<'\n';
}
return 0;
}