Cod sursa(job #1495803)
| Utilizator | Data | 3 octombrie 2015 17:33:46 | |
|---|---|---|---|
| Problema | Stramosi | Scor | 50 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 0.61 kb |
#include <fstream>
#include <cmath>
using namespace std;
ofstream fout("stramosi.out");
ifstream fin("stramosi.in");
const int NMAX = 250050;
int n, m;
int ST[20][NMAX];
int main()
{
fin >> n >> m;
for(int i=1; i<=n; i++)
fin >> ST[0][i];
int log = log2(n); // lol
for(int j=1; j<=log-1; j++)
for(int i=1; i<=n; i++)
ST[j][i] = ST[j-1][ST[j-1][i]];
for(int i=1, x, y; i<=m; i++) {
fin >> x >> y;
for(int i=20; i>=0; i--)
if(y & (1 << i))
x = ST[i][x];
fout << x << '\n';
}
return 0;
}
