Cod sursa(job #2956489)

Utilizator Mircea08Tomita Mircea Stefan Mircea08 Data 19 decembrie 2022 17:11:36
Problema Stramosi Scor 0
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 2.24 kb
#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("test.in");
int tree[19][250005];
int main() {
    //std :: ios_base :: sync_with_stdio(0);
    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("test.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;
}