Pagini recente » Cod sursa (job #635461) | infoarena - comunitate informatica, concursuri de programare | Cod sursa (job #2281235) | Cod sursa (job #2446317) | Cod sursa (job #2956493)
#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;
}
};
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;
std :: ofstream 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;
}