Cod sursa(job #1559933)

Utilizator tudorcomanTudor Coman tudorcoman Data 31 decembrie 2015 20:37:01
Problema Lowest Common Ancestor Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 3.14 kb
#include <cstdio>
#include <cstring>
#include <vector>
#include <cctype>
#include <algorithm>

using namespace std;

const int maxN = 100000;
const int maxNd = maxN << 1;
const int maxSize = 1 << 20;

class ReadStream {
private:
  int cursor;
  char buf[maxSize];
  FILE *f;
  inline void move() {
    ++ cursor;
    if(cursor == maxSize) {
      cursor = 0;
      fread(buf, maxSize, 1, f);
    }
  }
  inline int todig(char c) {
    return c - '0';
  }
  
public:
  ReadStream() {}
  ReadStream(const char *fname) {
    f = fopen(fname, "r");
    cursor = 0;
    fread(buf, maxSize, 1, f);
  }
  
  ReadStream &operator >> (int& N) {
    char sign = '+';
    while(!isdigit(buf[cursor])) {
      sign = buf[cursor];
      move();
    }
    
    N = 0;
    while(isdigit(buf[cursor])) {
      N = N * 10 + todig(buf[cursor]);
      move();
    }
    
    if(sign == '-')
      N = -N;
    return *this;
  }
  
  ReadStream &operator >> (float& N) {
    char sign = '+';
    while(!isdigit(buf[cursor])) {
      sign = buf[cursor];
      move();
    }
    N = 0;
    while(isdigit(buf[cursor])) {
      N = N * 10 + todig(buf[cursor]);
      move();
    }
    
    if(buf[cursor] == '.') {
      float p = 0.1;
      move();
      while(isdigit(buf[cursor])) {
        N = N + p * todig(buf[cursor]);
        p *= 0.1;
        move();
      }
    }
    
    if(sign == '-')
      N = -N;
    return *this;
  }
};

char output[maxSize];
int pos;

inline void putch(char c) {
  output[pos ++] = c;
  if(pos == maxSize) {
    pos = 0;
    fwrite(output, maxSize, 1, stdout);
  }
}

inline void putnr(int n) {
  int aux = 0;
  do {
    aux = aux * 10 + (n % 10);
    n /= 10;
  }while(n);
  
  n = aux;
  
  do {
    putch('0' + (n % 10));
    n /= 10;
  }while(n);
  putch('\n');
}

int N;
bool viz[1 + maxN];
vector<int> sons[1 + maxN];

int id[1 + maxN], ac;
int euler[maxNd], where[1 + maxN], e;

int rmq[20][maxNd << 1];
int Log[maxNd + 1];

void rmqCompute(int v[], int N) {
  for(int i = 0; i < N; ++ i)
    rmq[0][i] = v[i];
  for(int i = 1; (1 << i) <= N; ++ i)
    Log[(1 << i)] = 1;
  for(int i = 1; i <= N; ++ i)
    Log[i] += Log[i - 1];
  
  int p = 2;
  for(int step = 1; p <= N; ++ step, p <<= 1) {
    int put = p >> 1;
    for(int i = 0; i + put < N; ++ i)
      rmq[step][i] = min(rmq[step - 1][i], rmq[step - 1][i + put]);
  }
}

int rmQuery(int x, int y) {
  int l = y - x + 1;
  int step = Log[l];
  return min(rmq[step][x], rmq[step][y - (1 << step) + 1]);
}


void dfs(int node = 1) {
  if(!viz[node]) {
    viz[node] = true;
    int nodeAc = ac;
    id[ac ++] = node;
    where[node] = e;
    euler[e ++] = nodeAc;
    for (int son: sons[node]) {
      dfs(son);
      euler[e ++] = nodeAc;
    }
  }
}

int lca(int x, int y) {
  x = where[x];
  y = where[y];
  return id[rmQuery(min(x, y), max(x, y))];
}

int main() {
  ReadStream in("lca.in");
  freopen("lca.out", "w", stdout);
  
  int M;
  in >> N >> M;
  for(int i = 2; i <= N; ++ i) {
    int fatherI;
    in >> fatherI;
    sons[fatherI].push_back(i);
  }
  
  dfs();
  rmqCompute(euler, e);
  
  while(M --) {
    int x, y;
    in >> x >> y;
    putnr(lca(x, y));
  }
  
  if(pos)
    fwrite(output, pos, 1, stdout);
  return 0;
}