Cod sursa(job #1041657)
| Utilizator | Data | 25 noiembrie 2013 23:18:35 | |
|---|---|---|---|
| Problema | Stramosi | Scor | 10 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 0.57 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[1000][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=32;
while(p){
if(e<=p){
x=s[e][x];
p-=e;
}
e>>=1;
}
g<<x<<'\n';
}
return 0;
}
