Cod sursa(job #1498004)
| Utilizator | Data | 7 octombrie 2015 21:17:06 | |
|---|---|---|---|
| Problema | Stramosi | Scor | 90 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 0.65 kb |
#include <fstream>
using namespace std;
ofstream fout("stramosi.out");
ifstream fin("stramosi.in");
const int NMAX = 250005;
int n, m, x, y;
int ST[19][NMAX];
int main()
{
fin >> n >> m;
for(int i=1; i<=n; ++i)
fin >> ST[0][i];
int l = 0, p = 1;
while (p <= n) {
p *= 2;
++l;
}
--l;
for(int j=1; j<=l; ++j)
for(int i=1; i<=n; ++i)
ST[j][i] = ST[j-1][ST[j-1][i]];
for(int i=1; i<=m; ++i) {
fin >> x >> y;
for(int j=20; j>-1; --j)
if(y & (1 << j))
x = ST[j][x];
fout << x << '\n';
}
return 0;
}
