Pagini recente » Cod sursa (job #1730071) | Cod sursa (job #2119721) | Cod sursa (job #2210675) | Cod sursa (job #2803513) | Cod sursa (job #2231196)
#include <fstream>
using namespace std;
constexpr int maxn = 250000 + 10;
constexpr int maxq = 300000 + 10;
int tt[18][maxn];
int main(){
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int n, m;
f >> n >> m;
for(int i = 1; i <= n; ++i)
f >> tt[0][i];
for(int i = 1; i < 18; ++i)
for(int j = 1; j <= n; ++j)
tt[i][j] = tt[i-1][tt[i-1][j]];
for(int i = 0, q, p; i < m; ++i){
f >> q >> p;
while(p){
const int niv = __builtin_ctz(p);
q = tt[niv][q];
p ^= (1<<niv); }
g << q << '\n'; } }