#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
bool be;
constexpr int N = 1e5 + 5, Q = 1e6 + 5;
int n, m, b, rt, fa[N], dfn[N], dep[N], cnt, st[N][17], p[N], in[N], pre[N], nxt[N], ans[Q], lca[N];
vector<int>e[N];
struct Query {
int l, r, x, id;
}q[Q];
struct ds {
int a[N], sum[350], in[N], b;
void clear()
{
memset(a + 1, 0, n * sizeof(int));
memset(sum, 0, sizeof sum);
}
void init()
{
b = sqrt(n);
for (int i = 1; i <= n; ++i) in[i] = (i + b - 1) / b;
}
void add(int x, int v)
{
a[x] += v;
sum[in[x]] += v;
}
int query(int x)
{
int ans = 0;
for (int i = 1; i < in[x]; ++i) ans += sum[i];
for (int i = (in[x] - 1) * b + 1; i <= x; ++i) ans += a[i];
return ans;
}
}d;
void dfs(int u)
{
dfn[u] = ++cnt;
st[cnt][0] = fa[u];
dep[u] = dep[fa[u]] + 1;
for (int v : e[u]) {
dfs(v);
}
}
int get(int u, int v)
{
return dfn[u] < dfn[v] ? u : v;
}
int LCA(int u, int v)
{
if (u == v) return u;
if (dfn[u] > dfn[v]) swap(u, v);
int k = __lg(dfn[v] - dfn[u]);
return get(st[dfn[u] + 1][k], st[dfn[v] - (1 << k) + 1][k]);
}
void work(int x, int y, int z)
{
if (y == 0) return;
if (x == 0 || dep[x] != dep[y]) d.add(dep[y], z);
else d.add(dep[x] - dep[lca[y]], z);
}
void del(int x)
{
work(pre[x], x, -1);
work(x, nxt[x], -1);
lca[nxt[x]] = pre[x] && nxt[x] ? LCA(pre[x], nxt[x]) : 0;
work(pre[x], nxt[x], 1);
pre[nxt[x]] = pre[x];
nxt[pre[x]] = nxt[x];
}
void add(int x)
{
work(pre[x], x, 1);
work(pre[x], nxt[x], -1);
lca[nxt[x]] = nxt[x] ? LCA(x, nxt[x]) : 0;
work(x, nxt[x], 1);
pre[nxt[x]] = x;
nxt[pre[x]] = x;
}
bool en;
int main()
{
#ifdef IAKIOI
cerr << (&be - &en) / 1024.0 / 1024 << " MB\n----------------------------" << endl;
freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> fa[i];
if (fa[i] == 0) rt = i;
else e[fa[i]].emplace_back(i);
}
dfs(rt);
for (int j = 1; j <= __lg(n); ++j) {
for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
st[i][j] = get(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
d.init();
b = max(1, 0.8 * (int)(n / sqrt(m)));
for (int i = 1; i <= n; ++i) {
in[i] = (i + b - 1) / b;
}
iota(p + 1, p + 1 + n, 1);
sort(p + 1, p + 1 + n, [](int x, int y) {
if (dep[x] != dep[y]) return dep[x] < dep[y];
return dfn[x] < dfn[y];
});
for (int i = 1; i <= n; ++i) {
pre[p[i]] = p[i - 1];
nxt[p[i]] = p[i + 1];
}
for (int i = 1; i <= m; ++i) {
cin >> q[i].l >> q[i].r >> q[i].x;
q[i].id = i;
}
sort(q + 1, q + 1 + m, [](Query a, Query b) {
if (in[a.l] != in[b.l]) return a.l < b.l;
return a.r > b.r;
});
int l = 0, r = -1;
for (int i = 1; i <= m; ++i) {
if (in[l] != in[q[i].l]) {
d.clear();
memset(pre + l, 0, (n - l + 1) * sizeof(int));
memset(nxt + l, 0, (n - l + 1) * sizeof(int));
l = (in[q[i].l] - 1) * b + 1;
r = q[i].r;
int lst = 0;
for (int j = 1; j <= n; ++j) {
if (l <= p[j] && p[j] <= r) {
lca[p[j]] = lst ? LCA(lst, p[j]) : 0;
work(lst, p[j], 1);
pre[p[j]] = lst;
nxt[lst] = p[j];
lst = p[j];
}
}
}
while (q[i].r < r) del(r--);
int pl = l;
while (q[i].l > l) del(l++);
ans[q[i].id] = r - l + 1 - d.query(q[i].x);
while (pl < l) add(--l);
}
for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';
return 0;
}