| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 
 | ##include <bits/stdc++.h>
 using namespace std;
 
 typedef long long ll;
 typedef long double ld;
 typedef pair<int, int> pdd;
 
 ##define INF 0x3f3f3f3f
 ##define lowbit(x) x & (-x)
 ##define mem(a, b) memset(a , b , sizeof(a))
 ##define FOR(i, x, n) for(int i = x;i <= n; i++)
 
 
 
 
 
 
 
 const int N = 2e5 + 10;
 
 int n;
 
 struct Edge {
 int u, v, next;
 }e[N << 1];
 int idx, head[N << 1];
 int fa[N][55], depth[N], lg[N];
 
 struct Node {
 int l, r;
 int val;
 }hjt[N * 40];
 
 int a[N], root[N], cnt;
 vector<int> vec;
 
 inline void add(int u, int v) {
 e[++idx].v = v;
 e[idx].next = head[u];
 head[u] = idx;
 }
 
 inline int getid(int x) { return lower_bound(vec.begin(), vec.end(), x) - vec.begin() + 1; }
 
 void insert(int pre, int &now, int l, int r, int p) {
 hjt[now = ++cnt] = hjt[pre];
 hjt[now].val++;
 if(l == r) return ;
 int m = (l + r) / 2;
 if(p <= m) insert(hjt[pre].l, hjt[now].l, l, m, p);
 else insert(hjt[pre].r, hjt[now].r, m + 1, r, p);
 }
 
 int query(int ql, int qr, int lca, int falca, int l, int r, int k) {
 if(l == r) return l;
 int m = (l + r) / 2;
 int tmp = hjt[hjt[ql].l].val + hjt[hjt[qr].l].val - hjt[hjt[lca].l].val - hjt[hjt[falca].l].val;
 if(k <= tmp) return query(hjt[ql].l, hjt[qr].l, hjt[lca].l, hjt[falca].l, l, m, k);
 else return query(hjt[ql].r, hjt[qr].r, hjt[lca].r, hjt[falca].r, m + 1, r, k - tmp);
 }
 
 int LCA(int u1, int u2) {
 if(depth[u1] < depth[u2]) swap(u1, u2);
 while(depth[u1] > depth[u2])
 u1 = fa[u1][lg[depth[u1] - depth[u2]] - 1];
 if(u1 == u2) return u1;
 
 for(int i = lg[depth[u1]] - 1;i >= 0; i--) {
 if(fa[u1][i] != fa[u2][i]) {
 u1 = fa[u1][i];
 u2 = fa[u2][i];
 }
 }
 return fa[u1][0];
 }
 
 void LCA_DFS(int u, int pre) {
 fa[u][0] = pre;
 depth[u] = depth[pre] + 1;
 insert(root[pre], root[u], 1, n, getid(a[u]));
 for(int i = 1;i <= lg[depth[u]]; i++) {
 fa[u][i] = fa[fa[u][i - 1]][i - 1];
 }
 for(int i = head[u]; i; i = e[i].next) {
 if(e[i].v != pre)
 LCA_DFS(e[i].v, u);
 }
 }
 
 void solve()
 {
 int m;
 cin >> n >> m;
 for(int i = 1;i <= n; i++) {
 cin >> a[i]; vec.push_back(a[i]);
 }
 sort(vec.begin(), vec.end());
 vec.erase(unique(vec.begin(), vec.end()), vec.end());
 for(int i = 1;i < n; i++) {
 int u, v;
 cin >> u >> v;
 add(u, v);
 add(v, u);
 }
 for(int i = 1; i <= n; ++i)
 lg[i] = lg[i - 1] + (1 << lg[i - 1] == i);
 LCA_DFS(1, 0);
 
 while(m--) {
 int l, r, k;
 cin >> l >> r >> k;
 int lca = LCA(l, r);
 
 cout << vec[query(root[l], root[r], root[lca], root[fa[lca][0]], 1, n, k) - 1] << endl;
 }
 }
 
 signed main() {
 ios_base::sync_with_stdio(false);
 
 
 ##ifdef FZT_ACM_LOCAL
 freopen("in.txt", "r", stdin);
 freopen("out.txt", "w", stdout);
 signed test_index_for_debug = 1;
 char acm_local_for_debug = 0;
 do {
 if (acm_local_for_debug == '$') exit(0);
 if (test_index_for_debug > 20)
 throw runtime_error("Check the stdin!!!");
 auto start_clock_for_debug = clock();
 solve();
 auto end_clock_for_debug = clock();
 cout << "Test " << test_index_for_debug << " successful" << endl;
 cerr << "Test " << test_index_for_debug++ << " Run Time: "
 << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
 cout << "--------------------------------------------------" << endl;
 } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
 ##else
 solve();
 ##endif
 return 0;
 }
 
 |