给出一颗含有n个节点的树,每个节点有个权值。问u到v的路径上第k小的权值是多大。
做法就是主席树+LCA,每个节点建立一颗从根(默认为1)到它的线断树,那么u->v路径的线断树就等于T[u] + T[v] - 2*LCA(u, v) + (l <= a[LCA] && a[LCA] <= r)。
/*****************************************
Author :Crazy_AC(JamesQi)
Time :2016
File Name :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
#define lson rt << 1
#define rson rt << 1 | 1
#define bug cout << "BUG HERE\n"
#define debug(x) cout << #x << " = " << x << endl
#define ALL(v) (v).begin(), (v).end()
#define lowbit(x) ((x)&(-x))
#define Unique(x) sort(ALL(x)); (x).resize(unique(ALL(x)) - (x).begin())
#define BitOne(x) __builtin_popcount(x)
#define showtime printf("time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)
#define Rep(i, l, r) for (int i = l;i <= r;++i)
#define Rrep(i, r, l) for (int i = r;i >= l;--i)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-8;
const double pi = 4 * atan(1);
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int nCase = 0;
//精度正负、0的判断
int dcmp(double x){if (fabs(x) < eps) return 0;return x < 0?-1:1;}
template<class T> inline bool read(T &n){
T x = 0, tmp = 1;
char c = getchar();
while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if(c == EOF) return false;
if(c == '-') c = getchar(), tmp = -1;
while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
n = x*tmp;
return true;
}
template <class T> inline void write(T n){
if(n < 0){putchar('-');n = -n;}
int len = 0,data[20];
while(n){data[len++] = n%10;n /= 10;}
if(!len) data[len++] = 0;
while(len--) putchar(data[len]+48);
}
LL QMOD(LL x, LL k) {
LL res = 1LL;
while(k) {if (k & 1) res = res * x % MOD;k >>= 1;x = x * x % MOD;}
return res;
}
const int maxn = 1e5 + 123;
int n, m, q;
int a[maxn], Hash[maxn];
int head[maxn], pnt[maxn*2], nxt[maxn*2], ecnt;
struct _LCA {
int dep[maxn];
const static int LOGN = 20;
int fa[LOGN][maxn];
void init() {
// memset(fa, -1, sizeof fa);
}
void dfs(int u,int pre, int depth) {
fa[0][u] = pre; dep[u] = depth;
for (int i = head[u];~i;i = nxt[i]) {
if (pnt[i] == pre) continue;
dfs(pnt[i], u, depth + 1);
}
}
inline void Build(int n) {
for (int k = 0;k < LOGN - 1;++k) {
for (int u = 1;u <= n;++u) {
if (fa[k][u] == -1) fa[k + 1][u] = -1;
else fa[k + 1][u] = fa[k][ fa[k][u] ];
}
}
}
inline int upslope(int u,int p) {
for (int k = 0;k < LOGN - 1;++k)
if ((p>>k) & 1) u = fa[k][u];
return u;
}
inline int LCA(int u,int v) {
if (dep[u] < dep[v]) swap(u, v);
u = upslope(u, dep[u] - dep[v]);
if (u == v) return u;
for (int k = LOGN - 1;k >= 0;--k) {
if (fa[k][u] != fa[k][v])
u = fa[k][u], v = fa[k][v];
}
return fa[0][u];
}
}lca;
int root[maxn], ls[maxn*20], rs[maxn*20], sum[maxn*20], tot;
void build(int&rt, int l, int r) {
rt = ++tot;
sum[rt] = 0;
if (l == r) return ;
int mid = (l + r) >> 1;
build(ls[rt], l, mid);
build(rs[rt], mid + 1, r);
}
void updata(int last,int&rt,int l,int r,int pos, int v) {
rt = ++tot;
ls[rt] = ls[last];
rs[rt] = rs[last];
sum[rt] = sum[last] + v;
if (l == r) return ;
int mid = (l + r) >> 1;
if (pos <= mid) updata(ls[last], ls[rt], l, mid, pos, v);
else updata(rs[last], rs[rt], mid + 1, r, pos, v);
}
void dfs_build(int u,int pre) {
updata(root[pre], root[u], 1, m, a[u], 1);
for (int i = head[u];~i;i = nxt[i]) {
if (pnt[i] == pre) continue;
dfs_build(pnt[i], u);
}
}
int find(int u_rt, int v_rt, int lca,int k) {
int lca_rt = root[lca];
int pos = a[lca];
int l = 1, r = m;
while(l < r) {
int mid = (l + r) >> 1;
int temp = sum[ls[u_rt]] + sum[ls[v_rt]] - 2*sum[ls[lca_rt]] + (l <= pos && pos <= mid);
if (temp >= k) {
u_rt = ls[u_rt];
v_rt = ls[v_rt];
lca_rt = ls[lca_rt];
r = mid;
}else {
k -= temp;
u_rt = rs[u_rt];
v_rt = rs[v_rt];
lca_rt = rs[lca_rt];
l = mid + 1;
}
}
return l;
}
void init() {
memset(head, -1, sizeof head), ecnt = 0, tot = 0;
}
int main(int argc, const char * argv[])
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// ios::sync_with_stdio(false);
// cout.sync_with_stdio(false);
// cin.sync_with_stdio(false);
// cout << (1 << 20) - 1 << endl;
while(~scanf("%d%d", &n, &q)) {
init();
Rep(i, 1, n) {scanf("%d", &a[i]); Hash[i] = a[i];}
sort(Hash + 1, Hash + 1 + n);
m = unique(Hash + 1, Hash + 1 + n) - Hash - 1;
Rep(i, 1, n) a[i] = lower_bound(Hash + 1, Hash + 1 + m, a[i]) - Hash;
Rep(i, 1, n - 1) {
int u, v;scanf("%d%d", &u, &v);
pnt[ecnt] = v, nxt[ecnt] = head[u], head[u] = ecnt++;
pnt[ecnt] = u, nxt[ecnt] = head[v], head[v] = ecnt++;
}
lca.dfs(1, -1, 0);
lca.Build(n);
build(root[0], 1, m);//m种数
dfs_build(1, 0);
while(q--) {
int u, v, k;scanf("%d%d%d", &u, &v, &k);
// printf("[u = %d, v = %d, lca = %d]\n", u, v, lca.LCA(u, v));
printf("%d\n", Hash[find(root[u], root[v], lca.LCA(u, v), k)]);
}
}
// showtime;
return 0;
}