Cod sursa(job #3206995)

Utilizator matei__bBenchea Matei matei__b Data 24 februarie 2024 17:38:11
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define chad char
#define mod 666013
#define dim 100005
#define lim 1000000
#define INF 1000000000
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define piii pair<int,pair<int,int> > 
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define nr_biti __builtin_popcount
using namespace std;
 
ifstream fin("stramosi.in");
ofstream fout("stramosi.out"); 

int st[30][3*dim];
int n,m;

void pre()
{
    for(int i=1; (1<<i)<=n; i++)
        for(int j=1; j<=n; j++)
            st[i][j]=st[i-1][st[i-1][j]];
}

int bl(int nod,int niv)
{
    int z=0;

    while(niv)
    {
        if(niv&1)
            nod=st[z][nod];

        z++;
        niv/=2;
    }

    return nod;
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr); 

    fin >> n >> m;

    for(int i=1; i<=n; i++)
        fin >> st[0][i];

    pre();

    while(m--)
    {
        int x,y;

        fin >> x >> y;

        fout << bl(x,y) << "\n";
    }

    return 0;
}