Mai intai trebuie sa te autentifici.

Cod sursa(job #1819468)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 30 noiembrie 2016 17:00:15
Problema Lowest Common Ancestor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.37 kb
#include <bits/stdc++.h>

using namespace std;

const int NRCHAR = 131072;
class Parsare
{
private:
    FILE *fin;
    char *buff;
    int sp;

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

public:
    Parsare(const char *nume)
    {
        fin = fopen(nume, "r");
        buff = new char[NRCHAR]();
        sp = NRCHAR - 1;
    }
    template<class T>
    Parsare &operator >> (T &n)
    {
        char c;
        while(!isdigit(c = read_ch()));
        n = c - '0';

        while(isdigit(c = read_ch()))
            n = n * 10 + c - '0';
        return *this;
    }
};
Parsare fin("lca.in");

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

OutParser fout("lca.out");

vector <int> v[100005];
int position[200005];
int first[100005];
int depth[100005];
int rmq[18][200005];
int lg2[200005];

inline int mini(int i, int j){
    if(depth[i] < depth[j]){
        return i;
    }
    return j;
}

inline void dfs(int node, int cdepth, int& pos){
    depth[node] = cdepth;
    position[pos] = node;
    first[node] = pos;
    pos++;
    for(auto it : v[node]){
        dfs(it, cdepth + 1, pos);
        position[pos] = node;
        pos++;
    }
}

void init(int n){
    int i,j;
    lg2[2] = 1;
    for(i = 3;i <= n;i++){
        lg2[i] = lg2[i>>1] + 1;
    }
    for(i = 1;i <= n;i++){
        rmq[0][i] = position[i];
    }
    for(j = 1;j <= lg2[n];j++){
        int limit = n - ((1<<j)-1);
        for(i = 1;i <= limit;i++){
            rmq[j][i] = mini(rmq[j-1][i], rmq[j-1][i + (1<<(j-1))]);
        }
    }
}

inline int query(int& lf, int& rg){
    if(lf > rg){
        swap(lf, rg);
    }
    int pos = lg2[rg - lf + 1];
    return mini(rmq[pos][lf], rmq[pos][rg - (1<<pos) + 1]);
}

int main(){
    int n,q,i,x,y,j;
    fin>>n>>q;
    for(i = 1;i < n;i++){
        fin>>x;
        v[x].push_back(i + 1);
    }
    int pos = 1;
    dfs(1, 0, pos);
    init(pos - 1);
    for(i = 1;i <= q;i++){
        fin>>x>>y;
        fout<<query(first[x], first[y])<<'\n';
    }
    return 0;
}