Pagini recente » Cod sursa (job #1049673) | Cod sursa (job #723708) | Cod sursa (job #2405730) | Cod sursa (job #1995992) | Cod sursa (job #2671385)
#include <iostream>
#include <fstream>
using namespace std;
class InParser {
private:
static const int buffSZ = (1 << 15);
ifstream File;
int buffPos;
char buff[buffSZ];
void _advance() {
if (++buffPos == buffSZ) {
File.read(buff, buffSZ);
buffPos = 0;
}
}
public:
InParser(const char *FileName) {
buffPos = buffSZ - 1;
File.open(FileName);
}
InParser& operator >>(int &no) {
while (!isdigit(buff[buffPos]))
_advance();
no = 0;
while (isdigit(buff[buffPos])) {
no = no * 10 + buff[buffPos] - '0';
_advance();
}
return *this;
}
};
InParser fin("stramosi.in");
ofstream fout("stramosi.out");
int n, q, stra[250005][20];
int main()
{
fin >> n >> q;
for(int i=1; i<=n; i++)
fin >> stra[i][0];
for(int i=1; i<=17; i++)
for(int j=2; j<=n; j++)
stra[j][i]=stra[stra[j][i-1]][i-1];
for(int i=1; i<=q; i++)
{
int x, k, num=0;
fin >> x >> k;
while(k)
{
if(k%2) x=stra[x][num];
k/=2;
num++;
}
fout << x << "\n";
}
return 0;
}