Cod sursa(job #2075420)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 25 noiembrie 2017 13:47:19
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.23 kb
#include<bits/stdc++.h>
using namespace std;
class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
}f("stramosi.in");
ofstream g("stramosi.out");
int n,m,q,p,nr[250002];
int ancestors[250002][20];
vector<int>gul[250002];
void dfs(int dad, int nod)
{
    ancestors[nod][0]=dad;
    int lvl=0;
    int fth=dad;
    while(ancestors[fth][lvl])
    {
        ancestors[nod][lvl+1]=ancestors[fth][lvl];
        ++lvl;
        fth=ancestors[nod][lvl];
    }
    for(int i=0;i<gul[nod].size();++i)
        dfs(nod,gul[nod][i]);
}
int main()
{
    f>>n>>m;
    for(int i=1;i<=n;++i)
    {
        f>>nr[i];
        if(nr[i]!=0)
            gul[nr[i]].push_back(i);
    }
    for(int i=1;i<=n;++i)
        if(!nr[i])
            dfs(0,i);
    for(int i=1;i<=m;++i)
    {
        f>>q>>p;
        int p2=1,lvl=0;
        while(p2*2<=p)
            p2*=2,lvl++;
        int father=q;
        while(p && father!=0)
        {
            p-=p2;
            father=ancestors[father][lvl];
            while(p<p2)
                p2/=2,--lvl;
        }
        g<<father<<'\n';
    }
    return 0;
}