Pagini recente » Cod sursa (job #2140247) | Cod sursa (job #2279072) | Cod sursa (job #2009001) | Cod sursa (job #536628) | Cod sursa (job #2956492)
#include <bits/stdc++.h>
class InParser {
private:
FILE *fin;
char *buff;
inline char get_ch() {
++ pos;
if (pos == 4096) {
pos = 0;
fread(buff, 1, 4096, fin);
}
return buff[pos];
}
public:
short pos = 4095;
InParser(const char *file) {
fin = fopen(file, "r");
buff = new char[4096]();
}
InParser& operator >> (int &x) {
char ch = get_ch();
while (!isdigit(ch) && ch != '-')
ch = get_ch();
x = ch - '0';
ch = get_ch();
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = get_ch();
}
return *this;
}
};
class OutParser {
private:
FILE *fout;
char *buff;
short pos = 0;
inline void write_ch(char ch) {
if (pos == 50000) {
pos = 0;
fwrite(buff, 1, 50000, fout);
}
buff[pos ++] = ch;
}
public:
OutParser(const char *file) {
fout = fopen(file, "w");
buff = new char[50000]();
}
~OutParser() {
fwrite(buff, 1, pos, fout);
fclose(fout);
}
OutParser& operator << (int x) {
if (x < 10)
write_ch(x + '0');
else {
(*this) << (x / 10);
write_ch(x % 10 + '0');
}
return *this;
}
OutParser& operator << (char x) {
write_ch(x);
return *this;
}
};
InParser fin("stramosi.in");
int tree[19][250005];
int main() {
int n, Q;
fin >> n >> Q;
int i;
for (i = 1; i <= n; ++ i)
fin >> tree[0][i];
short j;
for (j = 1; (1 << j) <= n; ++ j)
for (i = 1; i <= n; ++ i)
tree[j][i] = tree[j - 1][tree[j - 1][i]];
int stramos, last_bit, ans;
OutParser fout("stramosi.out");
while (Q) {
-- Q;
fin >> ans >> stramos;
while (stramos) {
last_bit = std :: __lg((stramos & (stramos * (-1))));
ans = tree[last_bit][ans];
stramos -= (1 << last_bit);
if (ans == 0)
break;
}
fout << ans << '\n';
}
return 0;
}