Cod sursa(job #3344375)

Utilizator robertcosacCosac Robert-Mihai robertcosac Data 1 martie 2026 21:29:19
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int tata[300009], str[22][300009], p[22];
int query (int x, int y)
{
    int ans=x, bit=0;
    while (y)
    {
        if (y%2)
            ans=str[bit][ans];
        bit++;
        y/=2;
    }
    return ans;
}
signed main ()
{
    p[0]=1;
    for (int i=1; i<=20; i++)
        p[i]=2*p[i-1];
    int n, q;
    f >> n >> q;
    for (int i=1; i<=n; i++)
        f >> str[0][i];
    for (int i=1; p[i]<=n; i++)
        for (int j=1; j<=n; j++)
            str[i][j]=str[i-1][str[i-1][j]];
    while (q--)
    {
        int x, y;
        f >> x >> y;
        g << query (x, y) << '\n';
    }
}