Cod sursa(job #1041650)

Utilizator dan.ghitaDan Ghita dan.ghita Data 25 noiembrie 2013 23:12:32
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.58 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int p, n, x, v[250003], m, e, s[20][250003], lvl[250003];
int main()
{
f>>n>>m;
for(int i=1; i<=n; ++i)
    f>>v[i], lvl[i]=lvl[v[i]]+1;//,cout<<lvl[i]<<' ';cout<<'\n';
for(int i=1; i<=n; ++i){
    x=i;
    s[1][x]=v[i];
    for(p=2; p<=lvl[i]; p<<=1){
        s[p][i]=s[p>>1][s[p>>1][i]];
    }
}

while(m--){
f>>x>>p;
e=1;
while((e<<1)<=p) e<<=1;
while(p){
    if(e<=p){
    x=s[e][x];
    p-=e;}
    e>>=1;
}
g<<x<<'\n';


}

    return 0;
}