Cod sursa(job #2754960)
| Utilizator | Data | 26 mai 2021 18:19:37 | |
|---|---|---|---|
| Problema | Plantatie | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva de probleme | Marime | 0.65 kb |
#include <iostream>
#include <fstream>
using namespace std;
const int NMAX = 25e4;
const int LOGMAX = 17;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int a[LOGMAX + 1][NMAX + 1];
int main() {
int n, Q, q, p, j;
fin>>n>>Q;
for(int i = 1; i <= n; ++i)
fin >> a[0][i];
for(int i = 1; (1 << i) <= n; ++i) {
for (j = 1; j <= n; ++j) {
a[i][j] = a[i - 1][a[i - 1][j]];
}
}
while(Q--){
fin >> q >> p;
j = 0;
while(p){
if (p % 2 == 1)
q = a[j][q];
j++;
p /= 2;
}
fout<<q<<'\n';
}
return 0;
}