题目链接:https://www.spoj.com/problems/COT/en/
题意: 给你一颗树,问你u,v结点这条路径上第k大是多少。
思路:就是说先将无根树转化为有根树,然后对每一条链建立一个主席树再相减查询
#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define hash Hash
#define next Next
#define count Count
#define pb push_back
#define f first
#define s second
using namespace std;
const int N = 1e5+10, mod = 10007;
const double eps = 1e-10;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
inline LL read()
{
LL f=1,s=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')s=(s<<1)+(s<<3)+ch-'0',ch=getchar();
return s*f;
}
int n,m;
int a[N];
struct node {
int to, next;
}e[N << 2];
int head[N], cnt;
int f[N][20], depth[N];
struct Tree{
int l, r, cnt;
}tr[N * 40];
int root[N], idx;
vector<int> ys;
inline int getid(int x)
{
return lower_bound(ys.begin(),ys.end(),x) - ys.begin() + 1;
}
inline void add(int from, int to)
{
e[cnt] = {to,head[from]};
head[from] = cnt ++;
}
inline int build(int l, int r)
{
int now = ++ idx;
if(l == r) return now;
tr[now].l = build(l,mid);
tr[now].r = build(mid+1,r);
return now;
}
inline int insert(int l, int r, int pre, int pos)
{
int now = ++ idx;
tr[now] = tr[pre];
if(l == r)
{
tr[now].cnt ++;
return now;
}
if(pos <= mid)
tr[now].l = insert(l,mid,tr[pre].l,pos);
else tr[now].r = insert(mid+1,r,tr[pre].r,pos);
tr[now].cnt = tr[tr[now].l].cnt + tr[tr[now].r].cnt;
return now;
}
inline void dfs(int u, int fa, int d)
{
depth[u] = d; f[u][0] = fa;
// cout << u << " " << fa << " " << getid(a[u]) << endl;
root[u] = insert(1,ys.size(),root[fa],getid(a[u]));
for(int i = head[u]; ~i; i = e[i].next)
{
int v = e[i].to;
if(v != fa) dfs(v,u,d+1);
}
}
inline void init()
{
root[0] = build(1,ys.size());
dfs(1,0,0);
for(int j = 0; (1 << (j + 1)) < n; j ++)
{
for(int i = 1; i <= n; i ++)
if(f[i][j] == 0) f[i][j + 1] = 0;
else f[i][j + 1] = f[f[i][j]][j];
}
}
inline int LCA(int l, int r)
{
if(depth[l] < depth[r]) swap(l,r);
int temp = depth[l] - depth[r];
for(int i = 0; (1 << i) <= temp; i ++)
if((1 << i) & temp) l = f[l][i];
if(l == r) return l;
for(int i = log2(n); i >= 0; i --)
if(f[l][i] != f[r][i])
{
l = f[l][i]; r = f[r][i];
}
return f[l][0];
}
inline int query(int l, int r, int ltree, int rtree, int lcatree, int lcafa, int k)
{
if(l == r) return l;
int Eps = tr[tr[ltree].l].cnt + tr[tr[rtree].l].cnt - tr[tr[lcatree].l].cnt - tr[tr[lcafa].l].cnt;
if(k <= Eps) return query(l,mid,tr[ltree].l,tr[rtree].l,tr[lcatree].l,tr[lcafa].l,k);
else return query(mid+1,r,tr[ltree].r,tr[rtree].r,tr[lcatree].r,tr[lcafa].r,k - Eps);
}
void debug(int rt, int l, int r)
{
cout << tr[tr[rt].l].cnt << " " << tr[tr[rt].r].cnt << endl;
cout << endl;
if(l == r) return;
debug(tr[rt].l,l,mid);
debug(tr[rt].r,mid+1,r);
}
int main()
{
ms(head,-1);
n = read(), m = read();
_for(i,1,n+1) ys.pb(a[i] = read());
sort(ys.begin(),ys.end());
ys.erase(unique(ys.begin(),ys.end()),ys.end());
_for(i,0,n-1)
{
int l, r;
l = read(), r = read();
add(l,r);
add(r,l);
}
init();
while(m --)
{
int l, r, k;
l = read(), r = read(), k = read();
int lca = LCA(l,r);
printf("%d\n",ys[query(1,ys.size(),root[l],root[r],root[lca],root[f[lca][0]],k) - 1]);
}
return 0;
}