/* I will wait for you*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <string>
#define m_p make_pair
#define p_b push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int maxn = 100010;
const int maxm = 1010;
const int maxs = 26;
const int inf = 0x3f3f3f3f;
const int P = 201314;
const double error = 1e-9;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch <= 47 || ch >= 58)
f = (ch == 45 ? -1 : 1), ch = getchar();
while (ch >= 48 && ch <= 57)
x = x * 10 + ch - 48, ch = getchar();
return x * f;
}
struct node
{
ll sum, add, key, size;
node *fa, *son[2];
int isroot() {
return !fa || this != fa -> son[0] && this != fa -> son[1];
}
int dir() {
return this == fa -> son[1];
}
} no[maxn];
void pushdown(node* o)
{
if (o -> add) {
for (int i = 0; i < 2; i++)
if (o -> son[i])
o -> son[i] -> add += o -> add;
(o -> sum += o -> size * o -> add) %= P;
(o -> key += o -> add) %= P, o -> add = 0;
}
}
void maintain(node* o)
{
o -> sum = o -> key, o -> size = 1;
for (int i = 0; i < 2; i++)
if (o -> son[i]) {
pushdown(o -> son[i]);
(o -> sum += o -> son[i] -> sum) %= P;
o -> size += o -> son[i] -> size;
o -> son[i] -> fa = o;
}
}
void rotate(node* o)
{
node *p = o -> fa;
pushdown(p), pushdown(o);
int d = o -> dir();
p -> son[d] = o -> son[d ^ 1];
o -> son[d ^ 1] = p;
o -> fa = p -> fa;
if (!p -> isroot())
p -> fa -> son[p -> dir()] = o;
maintain(p), maintain(o);
}
void splay(node* o)
{
pushdown(o);
while (!o -> isroot()) {
node *p = o -> fa;
if (p -> isroot())
rotate(o);
else if (o -> dir() == p -> dir())
rotate(p), rotate(o);
else
rotate(o), rotate(o);
}
}
void access(node *o)
{
for (node *t = 0; o; t = o, o = o -> fa)
splay(o), o -> son[1] = t, maintain(o);
}
void increase(node* u)
{
access(u), splay(u), u -> add += 1;
}
ll query(node* u)
{
access(u), splay(u), pushdown(u);
return u -> sum;
}
void link(node *u, node *v)
{
u -> fa = v;
}
struct ask
{
int fi, se, th;
};
vector<ask> s[maxn]; ll ans[maxn];
int main()
{
int n = read(), m = read();
for (int i = 1; i <= n; i++)
no[i].size = 1;
for (int i = 2, u; i <= n; i++)
u = read() + 1, link(&no[i], &no[u]);
for (int i = 1; i <= m; i++) {
int x = read(), y = read() + 1, z = read() + 1;
s[x].p_b((ask) {z, -1, i});
s[y].p_b((ask) {z, 1, i});
}
for (int i = 1; i <= n; i++) {
increase(&no[i]);
for (int j = 0; j < s[i].size(); j++) {
ask tmp = s[i][j];
ans[tmp.th] += (ll) tmp.se * query(&no[tmp.fi]);
((ans[tmp.th] %= P) += P) %= P;
}
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
return 0;
}
BZOJ3626【LCT】
最新推荐文章于 2018-06-12 22:57:22 发布