做法:
首先说明以下都是以1为根的,点的编号是1~n。
考虑最暴力的做法:对于一个询问l, r, z,将1~z的点全部打标记,枚举l~r之间的点u,从1~u的路上找到第一个打标记的点,答案就加上这个点的深度。
然后稍微转化一下,假如把1~z的点权+1,然后对于l~r之间的点u,询问1~u之间的点权和,答案加上这个和。(正确性显然)
再转化,我们把l~r之间的点u,1~u上点权+1,然后最后询问1~z之间的点权和。这个和上面的操作是等价的。(显然)
以上的链加,链查询和都可以用树链剖分做到logn方。
然后对于所有的询问,我们考虑离线操作。
将所有询问排序后枚举1~n中点u,依次将1~u链上的点权+1,相应的询问中更新答案即可。
/*************************************************************
Problem: bzoj 3626 [LNOI2014]LCA
User: fengyuan
Language: C++
Result: Accepted
Time: 1440 ms
Memory: 6188 kb
Submit_Time: 2017-12-05 22:58:13
*************************************************************/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<vector>
#include<map>
#include<queue>
#include<string>
#define rep(i, x, y) for (int i = (x); i <= (y); i ++)
#define down(i, x, y) for (int i = (x); i >= (y); i --)
#define mid ((l+r)/2)
#define lc (o<<1)
#define rc (o<<1|1)
#define pb push_back
#define mp make_pair
#define PII pair<int, int>
#define F first
#define S second
#define B begin()
#define E end()
using namespace std;
typedef long long LL;
//head
const int N = 50010;
const int MOD = 201314;
int n, m, tot, cnt, clk;
int head[N], z[N], ans[N], depth[N], fa[N], sz[N], son[N], top[N], in[N], sum[N<<2], tag[N<<2];
struct Questions{
int p, id, flag;
}q[N<<1];
struct Edge{
int to, nex;
}e[N];
inline void add(int x, int y)
{
e[++ cnt].to = y;
e[cnt].nex = head[x];
head[x] = cnt;
}
const bool cmp(const Questions &x, const Questions &y)
{
return x.p < y.p;
}
inline void dfs(int u, int last, int s)
{
depth[u] = s; fa[u] = last; sz[u] = 1;
for (int i = head[u]; i; i = e[i].nex){
int v = e[i].to; if (v == last) continue;
dfs(v, u, s+1); sz[u] += sz[v];
if (!son[u] || sz[v] > sz[son[u]]) son[u] = v;
}
}
inline void dfs2(int u, int t)
{
top[u] = t; in[u] = ++ clk;
if (son[u]) dfs2(son[u], t);
for (int i = head[u]; i; i = e[i].nex){
int v = e[i].to; if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
inline void pushup(int o)
{
sum[o] = sum[lc] + sum[rc];
}
inline void pushdown(int o, int l, int r)
{
if (tag[o] == 0) return;
tag[lc] += tag[o]; tag[rc] += tag[o];
sum[lc] += tag[o]*(mid-l+1); sum[rc] += tag[o]*(r-mid);
tag[o] = 0;
}
inline void update(int o, int l, int r, int x, int y)
{
if (l == x && r == y){
sum[o] += r-l+1; tag[o] ++;
return;
}
pushdown(o, l, r);
if (y <= mid) update(lc, l, mid, x, y);
else if (x > mid) update(rc, mid+1, r, x, y);
else update(lc, l, mid, x, mid), update(rc, mid+1, r, mid+1, y);
pushup(o);
}
inline void change(int x, int y)
{
while (top[x] != top[y]){
if (depth[top[x]] < depth[top[y]]) swap(x, y);
update(1, 1, n, in[top[x]], in[x]);
x = fa[top[x]];
}
if (depth[x] > depth[y]) swap(x, y);
update(1, 1, n, in[x], in[y]);
}
inline int query(int o, int l, int r, int x, int y)
{
if (l == x && r == y) return sum[o];
pushdown(o, l, r);
if (y <= mid) return query(lc, l, mid, x, y);
else if (x > mid) return query(rc, mid+1, r, x, y);
else return query(lc, l, mid, x, mid) + query(rc, mid+1, r, mid+1, y);
}
inline int solve(int x, int y)
{
int ret = 0;
while (top[x] != top[y]){
if (depth[top[x]] < depth[top[y]]) swap(x, y);
ret = (ret+query(1, 1, n, in[top[x]], in[x]))%MOD;
x = fa[top[x]];
}
if (depth[x] > depth[y]) swap(x, y);
return (ret+query(1, 1, n, in[x], in[y]))%MOD;
}
int main()
{
scanf("%d%d", &n, &m);
rep(i, 2, n){
int x; scanf("%d", &x); x ++;
add(x, i);
}
dfs(1, 0, 0); dfs2(1, 1);
rep(i, 1, m){
int x, y; scanf("%d%d%d", &x, &y, &z[i]);
x ++, y ++, z[i] ++;
q[++ tot].p = x-1; q[tot].id = i; q[tot].flag = 0;
q[++ tot].p = y; q[tot].id = i; q[tot].flag = 1;
}
int now = 0;
sort(q+1, q+1+tot, cmp);
rep(i, 1, tot){
while (now < q[i].p) change(1, ++ now);
int t = z[q[i].id];
if (!q[i].flag) ans[q[i].id] = (ans[q[i].id]-solve(1, t)+MOD)%MOD;
else ans[q[i].id] = (ans[q[i].id]+solve(1, t))%MOD;
}
rep(i, 1, m) printf("%d\n", ans[i]);
return 0;
}