Cod sursa(job #3297442)
Utilizator | Data | 22 mai 2025 17:02:45 | |
---|---|---|---|
Problema | Stramosi | Scor | 80 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva de probleme | Marime | 0.53 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in("stramosi.in");
ofstream out("stramosi.out");
int str[250006][22];
int main() {
int n,m;
in>>n>>m;
for (int i=1;i<=n;i++) {
in>>str[i][0];
}
for (int j=1;(1<<j)<=n;j++) {
for (int i=1;i<=n;i++)
str[i][j]=str[str[i][j-1]][j-1];
}
for (int i=1;i<=m;i++) {
int nod,h;
in>>nod>>h;
for (int j=0;j<=20;j++) {
if (h&(1<<j)) {
nod=str[nod][j];
h-=(1<<j);
}
}
out<<nod<<" "<<endl;
}
}