fastle 垆边人似月 皓腕凝霜雪
• 把1的父亲设成了1导致debug半天
/*

*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#define ll long long
#define mmp make_pair
#define M 100010
using namespace std;
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
int a[M], b[M], c[M], rt[M], fa[M], top[M], sz[M], deep[M], son[M], n, q, cnt, ans;
vector<int> to[M];

int ls[M * 44], rs[M * 44], t[M * 44];

void dfs(int now, int f) {
fa[now] = f;
sz[now] = 1;
deep[now] = deep[f] + 1;
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
if(vj == f) continue;
dfs(vj, now);
sz[now] += sz[vj];
if(sz[son[now]] < sz[vj]) son[now] = vj;
}
}

void modify(int l, int r, int last, int &now, int k) {
now = ++cnt;
t[now] = t[last] + 1;
ls[now] = ls[last];
rs[now] = rs[last];
if(l == r) return;
int mid = (l + r) >> 1;
if(k <= mid) modify(l, mid, ls[last], ls[now], k);
else modify(mid + 1, r, rs[last], rs[now], k);
}

int query(int l, int r, int rt1, int rt2, int rt3, int rt4, int k) {
if(l == r) return l;
int mid = (l + r) >> 1;
int v = t[ls[rt3]] + t[ls[rt4]] - t[ls[rt1]] - t[ls[rt2]];
if(k > v) return query(mid + 1, r, rs[rt1], rs[rt2], rs[rt3], rs[rt4], k - v);
else return query(l, mid, ls[rt1], ls[rt2], ls[rt3], ls[rt4], k);
}

void dfs(int now) {
modify(1, n, rt[fa[now]], rt[now], a[now]);
if(son[now]) {
top[son[now]] = top[now];
dfs(son[now]);
}
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
if(vj == fa[now] || vj == son[now]) continue;
top[vj] = vj;
dfs(vj);
}
}

int lca(int a, int b) {
for(; top[a] != top[b]; a = fa[top[a]]) {
if(deep[top[a]] < deep[top[b]]) swap(a, b);
}
if(deep[a] > deep[b]) swap(a, b);
return a;
}

int main() {
for(int i = 1; i <= n; i++) a[i] = b[i] = read();
sort(b + 1, b + n + 1);
for(int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;
for(int i = 1; i < n; i++) {
to[vi].push_back(vj);
to[vj].push_back(vi);
}
dfs(1, 0);
top[1] = 1;
dfs(1);
while(q--) {
int l = lca(vi, vj);
ans = b[query(1, n, rt[l], rt[fa[l]], rt[vi], rt[vj], k)];
cout << ans << "\n";
}
return 0;
}
/*
8 1
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
1 4 4
*/


posted on 2019-04-17 08:10  fastle  阅读(95)  评论(0编辑  收藏  举报