Cod sursa(job #2812988)
| Utilizator | Data | 5 decembrie 2021 16:28:16 | |
|---|---|---|---|
| Problema | Stramosi | Scor | 90 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva de probleme | Marime | 0.66 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("stramosi.in");
ofstream fout ("stramosi.out");
const int DIM = 250050;
int lvl[DIM], dp[DIM][19];
int n, q, nod, up;
int main (){
fin >> n >> q;
for(int i = 1; i <= n; i++)
fin >> dp[i][0];
for(int j = 1; (1 << j) <= n; j++)
for(int i=1; i<=n; i++)
dp[i][j] = dp[ dp[i][j-1] ][j-1];
int p2;
while(q--){
fin >> nod >> up;
p2 = 0;
while(up != 0){
if(up&1)
nod = dp[nod][p2];
p2++;
up >>= 1;
}
fout << nod << "\n";
}
return 0;
}
