Solution











Code

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;
const int N = 2e5 + 5;
const ll mod = 1e9 + 7;
const int DEG = 20;
const double PI = acos(-1.0);
const double eps = 1e-12;
const ll inf = 1e18;
static auto faster_iostream = []() {
    std::ios::sync_with_stdio(false);  // turn off sync
    std::cin.tie(nullptr);             // untie in/out streams
    return 0;
}();
int a[N];
struct Edge{
    int to, nextz;
}edge[N << 1];
int tot, head[N];
void addedge(int u, int v){
    edge[tot].to = v;
    edge[tot].nextz = head[u];
    head[u] = tot++;
}
void init(){
    memset(head, -1, sizeof(head));
    tot = 0;
}
int fa[N][DEG];
int deg[N];
bool vis[N];
void dfs(int u, int par){
  deg[u] = deg[par] + 1;
  if(a[u] < a[par]){ //该节点的上一个比他大的是他的父节点
    fa[u][0] = par;
  }
  else {
    int pos = par;
    for(int i = DEG - 1; i >= 0; i--){ //从比父节点还大的节点中找到最近满足要求的
      if(fa[pos][i] && a[u] >= a[fa[pos][i]]){
        pos = fa[pos][i];
      }
    }
    fa[u][0] = fa[pos][0];
  }
  for(int i = 1; i < DEG; i++){
    fa[u][i] = fa[fa[u][i - 1]][i - 1]; // 递推
  }
  for(int i = head[u]; i != -1; i = edge[i].nextz){
    int v = edge[i].to;
    if(v == par) continue;
    dfs(v, u);
  }
}
struct Query{
  int u, v, w;
}query[N];
int main(){
  init();
  int n, q;
  cin >> n >> q;
  for(int i = 1; i <= n; i++){
    cin >> a[i];
  }
  for(int i = 1; i <= n - 1; i++){
    int u, v;
    cin >> u >> v;
    addedge(u, v);
    addedge(v, u); 
  }
  for(int i = 1; i <= q; i++){
    cin >> query[i].u >> query[i].v >> query[i].w;
    addedge(i + n, query[i].u); // 把查询作为新的节点
    addedge(query[i].u, i + n); // 同上
    a[i + n] = query[i].w; // 同上
  }
  dfs(1, 0);
  for(int i = 1; i <= q; i++){
    int u = n + i, v = query[i].v;
    ll ans = 0;
    for(int j = DEG - 1; j >= 0; j--){
      if(deg[fa[u][j]] >= deg[v]){ // 往上跳到深度不小于v的最远位置
        u = fa[u][j];
        ans += (1LL << j);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
/*
5 4
3 5 1 2 4
1 2
1 3
2 4
3 5
4 2 1
4 2 2
4 2 3
5 1 5
*/