Cod sursa(job #3301301)

Utilizator bogdan1479Luca Bogdan Alexandru bogdan1479 Data 24 iunie 2025 13:17:43
Problema Lowest Common Ancestor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.04 kb
#include <fstream>
#include <vector>

using namespace std;

const int NMAX = 2e5 + 1, SQRT = 21;

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;
    }
};

class OutParser
{
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch)
    {
        if(sp == 50000)
        {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        }
        else
        {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name)
    {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser()
    {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32)
    {
        if(vu32 <= 9)
        {
            write_ch(vu32 + '0');
        }
        else
        {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64)
    {
        if(vu64 <= 9)
        {
            write_ch(vu64 + '0');
        }
        else
        {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch)
    {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch)
    {
        while(*ch)
        {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};

InParser fin("lca.in");
OutParser fout("lca.out");

int n, m, niv[NMAX], ind[NMAX],
    lg[NMAX], t[SQRT][NMAX];
vector<int> G[NMAX], E;

void citire()
{
    fin >> n >> m;
    for(int i = 2, x; i <= n; ++i)
    {
        fin >> x;
        G[x].push_back(i);
    }
}

void dfs(int x)
{
    E.push_back(x);
    ind[x] = E.size();
    for(const auto& y : G[x])
    {
        niv[y] = niv[x] + 1;
        dfs(y);
        E.push_back(x);
    }
}

void genRmq()
{
    int k = E.size();
    lg[0] = -1;
    for(int i = 1; i <= k; ++i)
    {
        lg[i] = lg[i >> 1] + 1;
        t[0][i] = E[i - 1];
    }
    lg[0] = 0;
    for(int i = 1; (1 << i) <= k; ++i)
        for(int j = 1; j + (1 << i) - 1 <= k; ++j)
        {
            t[i][j] = t[i - 1][j];
            int j2 = j + (1 << (i - 1));
            if(niv[t[i][j]] > niv[t[i - 1][j2]]) t[i][j] = t[i - 1][j2];
        }
}

int lca(int x, int y)
{
    x = ind[x], y = ind[y];
    if(x > y) swap(x, y);
    int k = lg[y - x + 1],
        sol = t[k][x],
        j = y - (1 << k) + 1;
    if(niv[sol] > niv[t[k][j]]) sol = t[k][j];
    return sol;
}

int main()
{
    int x, y;
    citire();
    dfs(1);
    genRmq();
    while(m--)
    {
        fin >> x >> y;
        fout << lca(x, y) << '\n';
    }
    return 0;
}