Cod sursa(job #2283354)

Utilizator I_am_not_a_robotMr Domino I_am_not_a_robot Data 15 noiembrie 2018 14:19:30
Problema Lowest Common Ancestor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.32 kb
#include <fstream>
#include <vector>

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

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

const int N=100000+5;
const int LG=19;

int n,t;
vector<int>g[N];
int p[3*N],y=0;
int h[3*N];

int first[N];
int last[N];

int rmq[3*N][LG];
int who[3*N][LG];

int nownr=0;
int nr[N];

int lg[3*N];

inline void dfs(register int nod)
{
    nr[nod]=nownr++;
    if(g[nod].size()==0)
    {
        p[++y]=nod;
    }
    else
    {
        p[++y]=nod;
        for(auto &nou:g[nod])
        {
            h[nou]=h[nod]+1;
            dfs(nou);
            p[++y]=nod;
        }
    }
}


int main()
{
    InParser fin("lca.in");
    OutParser fout("lca.out");
    register int i;
    for(i=2;i<3*N;i++)
    {
        lg[i]=1+lg[i/2];
    }
    fin>>n>>t;
    register int dad;
    for(i=2;i<=n;i++)
    {
        fin>>dad;
        g[dad].push_back(i);
    }
    dfs(1);
    for(i=1;i<=y;i++)
    {
        rmq[i][0]=h[p[i]];
        who[i][0]=p[i];
        last[p[i]]=i;
        if(first[p[i]]==0)
        {
            first[p[i]]=i;
        }
    }
    for(register int k=1;(1<<k)<=y;k++)
    {
        for(i=1;i+(1<<k)-1<=y;i++)
        {
            if(rmq[i][k-1]<rmq[i+(1<<(k-1))][k-1])
            {
                rmq[i][k]=rmq[i][k-1];
                who[i][k]=who[i][k-1];
            }
            else
            {
                rmq[i][k]=rmq[i+(1<<(k-1))][k-1];
                who[i][k]=who[i+(1<<(k-1))][k-1];
            }
        }
    }
    register int a,b,st,dr,k;
    for(register int tc=1;tc<=t;tc++)
    {
        fin>>a>>b;
        if(nr[a]>nr[b])
        {
            swap(a,b);
        }
        st=first[a];
        dr=last[b];
        k=lg[dr-st+1];
        if(rmq[st][k]<rmq[dr-(1<<k)+1][k])
        {
            fout<<who[st][k]<<"\n";
        }
        else
        {
            fout<<who[dr-(1<<k)+1][k]<<"\n";
        }
    }
    return 0;
}