# RMQ求LCA

rmq求LCA，interesting。

## RMQ求LCA

$dfn[i]$:第$i$个节点位置的时间戳

$id[i][j]$：在欧拉序中$i$$i + 2^j - 1$这段区间内深度最小的节点编号

$dep[i]$：第$i$个节点的深度

## 时空复杂度

$T = 2 * n - 1$

// luogu-judger-enable-o2
#include<bits/stdc++.h>
const int MAXN = 1e6 + 10;
using namespace std;
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, Q, S, tot, dfn[MAXN], rev[MAXN], dep[MAXN], id[MAXN][21], lg2[MAXN], rd[MAXN];
vector<int> v[MAXN];
void dfs(int x, int fa) {
dfn[x] = ++tot; dep[x] = dep[fa] + 1; id[tot][0] = x;
for(int i = 0, to; i < v[x].size(); i++) {
if((to = v[x][i]) == fa) continue;
dfs(to, x);
id[++tot][0] = x;
}
}
void RMQ() {
for(int i = 2; i <= tot; i++) lg2[i] = lg2[i >> 1] + 1;
for(int j = 1; j <= 20; j++) {
for(int i = 1; (i + (1 << j) - 1) <= tot; i++) {
int r = i + (1 << (j - 1));
id[i][j] = dep[id[i][j - 1]] < dep[id[r][j - 1]] ? id[i][j - 1] : id[r][j - 1];
}
}
}
int Query(int l, int r) {
if(l > r) swap(l, r);
int k = lg2[r - l + 1];
return dep[id[l][k]] < dep[id[r - (1 << k) + 1][k]] ? id[l][k] : id[r - (1 << k) + 1][k];
}
int main() {
freopen("a.in", "r", stdin);
for(int i = 1; i <= N - 1; i++) {
v[x].push_back(y); v[y].push_back(x);
}
dfs(S, 0);
RMQ();
while(Q--) {