p4211 LCA 离线+树剖

(8 mins to read)

给定一棵1为根的树, 多次查询$\sum_{i=l}^{r} dep[LCA(i,z)]$

做法

只需把l-r的每个点到根的路径+1,然后查询z到根的路径的和即可考虑离线差分询问并按右端点排序

1
2
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define fi first
#define se second
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 1e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 201314;
int n, m, ans[N];
struct qry
{
int r, coef, z, id;
bool operator < (const qry &oth) {
return r < oth.r;
}
}q[N];
inline void add(int &u, int v)
{
u += v;
if(u>=mod) u -= mod;
}
vector<int> G[N];
int sz[N], top[N], son[N], dep[N], fa[N], dfn[N], idfn[N], tot;
struct seg
{
int l, r, sum, tag;
}t[N<<2];
void setval(int p, int v)
{
add(t[p].sum, v*(t[p].r-t[p].l+1));
add(t[p].tag, v);
}
void pushup(int p)
{
t[p].sum = t[p<<1].sum;
add(t[p].sum, t[p<<1|1].sum);
}
void pushdown(int p)
{
setval(p<<1, t[p].tag); setval(p<<1|1, t[p].tag);
t[p].tag = 0;
}
void build(int p, int l, int r)
{
t[p].l = l, t[p].r = r;
if(l==r) return;
int mid = (l+r)>>1;
build(p<<1, l, mid); build(p<<1|1, mid+1, r);
pushup(p);
}
void upd(int p, int x, int y, int v)
{
int l = t[p].l, r = t[p].r;
if(l>=x && r<=y)
{
setval(p, v);
return;
}
if(t[p].tag) pushdown(p);
int mid = (l+r)>>1;
if(x<=mid) upd(p<<1, x, y, v);
if(y>mid) upd(p<<1|1, x, y, v);
pushup(p);
}
int ask(int p, int x, int y)
{
int l = t[p].l, r = t[p].r;
if(l>=x && r<=y) return t[p].sum;
if(t[p].tag) pushdown(p);
int mid = (l+r)>>1, ans = 0;
if(x<=mid) add(ans, ask(p<<1, x, y));
if(y>mid) add(ans, ask(p<<1|1, x, y));
return ans;
}
void dfs1(int u, int f)
{
dep[u] = dep[f] + 1; fa[u] = f;
sz[u] = 1;
for(int v : G[u])
{
dfs1(v, u);
sz[u] += sz[v];
if(sz[v]>sz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int t)
{
top[u] = t;
dfn[u] = ++tot;
idfn[tot] = u;
if(!son[u]) return;
dfs2(son[u], t);
for(int v : G[u])
if(v!=son[u]) dfs2(v, v);
}
void updt(int x, int y, int v)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
upd(1, dfn[top[x]], dfn[x], v);
x = fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
upd(1, dfn[x], dfn[y], v);
}
int askt(int x, int y)
{
int ans = 0;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
add(ans, ask(1, dfn[top[x]], dfn[x]));
x = fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
add(ans, ask(1, dfn[x], dfn[y]));
return ans;
}
int main()
{
scanf("%d%d", &n, &m);
for(int i=2; i<=n; i++)
{
int u; scanf("%d", &u);
G[u+1].pb(i);
}
dfs1(1, 0); dfs2(1, 1);
build(1, 1, n);
int tot = 0;
for(int i=1; i<=m; i++)
{
int l, r, z; scanf("%d%d%d", &l, &r, &z);
q[++tot] = {l, -1, z+1, i};
q[++tot] = {r+1, 1, z+1, i};
}
sort(q+1, q+tot+1);
upd(1, dfn[1], dfn[2], 1);
int pre = 1;
for(int i=1; i<=tot; i++)
{
while(pre<=q[i].r)
{
updt(1, pre, 1);
++pre;
}
add(ans[q[i].id], q[i].coef*askt(1, q[i].z));
}
for(int i=1; i<=m; i++) printf("%d\n", (ans[i]+mod)%mod);
return 0;
}