洛谷4211 [LNOI2014]LCA

可以发现题目可以转化为把从$l$到$r$节点到$1$的路径上的点的点权都加上$1$,然后统计$1$到$z$路径上的点权

然后发现这个东西可以差分。。。

于是我们就把询问拆成$l-1$和$r$,然后按$r$排序

从$1$到$n$把$1$到$i$路径点权全部$+1$

询问时查询$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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# include <bits/stdc++.h>
const int mod = 201314;
const int MaxN = 100010;
struct edge
{
int next, to;
};
struct node
{
int l, r;
int sum, tag;
};
struct query
{
int r, z, id;
};
edge e[MaxN << 1];
query q[MaxN << 1];
int n, m, cnt, dfsnum;
int hson[MaxN], fa[MaxN], dfn[MaxN], ans[MaxN];
int head[MaxN], size[MaxN], dep[MaxN], top[MaxN];
struct SegmentTree
{
node t[MaxN << 2];
inline void pushup(int id){t[id].sum = t[id << 1].sum + t[id << 1 | 1].sum;}
inline void build(int id, int l, int r)
{
t[id].l = l, t[id].r = r;
if(l == r)
return;
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
}
inline void pushdown(int id)
{
if(t[id].tag)
{
t[id << 1].sum += t[id].tag * (t[id << 1].r - t[id << 1].l + 1);
t[id << 1 | 1].sum += t[id].tag * (t[id << 1 | 1].r - t[id << 1 | 1].l + 1);

t[id << 1].tag += t[id].tag;
t[id << 1 | 1].tag += t[id].tag;

t[id].tag = 0;
}
}
inline void modify(int id, int l, int r, int val)
{
if(t[id].l > r || t[id].r < l)
return;
if(l <= t[id].l && t[id].r <= r)
{
t[id].sum += val * (t[id].r - t[id].l + 1);
t[id].tag += val;
return;
}
pushdown(id);
modify(id << 1, l, r, val);
modify(id << 1 | 1, l, r, val);
pushup(id);
}
inline int query(int id, int l, int r)
{
if(t[id].l > r || t[id].r < l)
return 0;
if(l <= t[id].l && t[id].r <= r)
return t[id].sum;
pushdown(id);
return query(id << 1, l, r) + query(id << 1 | 1, l, r);
}
}T;
inline int cmp(query a, query b)
{
return a.r < b.r;
}
inline void add_edge(int u, int v)
{
++cnt;
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt;
}
inline void dfs1(int u, int f)
{
size[u] = 1;
for(int i = head[u]; i; i = e[i].next)
{
int v = e[i].to;
if(v == f)
continue;
dep[v] = dep[u] + 1;
fa[v] = u;
dfs1(v, u);
size[u] += size[v];
if(size[v] > size[hson[u]])
hson[u] = v;
}
}
inline void dfs2(int u, int Top)
{
++dfsnum;
dfn[u] = dfsnum;
top[u] = Top;
if(hson[u])
dfs2(hson[u], Top);
for(int i = head[u]; i; i = e[i].next)
{
int v = e[i].to;
if(v == fa[u] || v == hson[u])
continue;
dfs2(v, v);
}
}
inline void update_chain(int u, int v)
{
while(top[u] != top[v])
{
if(dep[top[u]] < dep[top[v]])
std::swap(u, v);
T.modify(1, dfn[top[u]], dfn[u], 1);
u = fa[top[u]];
}
if(dep[u] < dep[v])
std::swap(u, v);
T.modify(1, dfn[v], dfn[u], 1);
}
inline int query_chain(int u, int v)
{
int ans = 0;
while(top[u] != top[v])
{
if(dep[top[u]] < dep[top[v]])
std::swap(u, v);
ans += T.query(1, dfn[top[u]], dfn[u]);
u = fa[top[u]];
}
if(dep[u] < dep[v])
std::swap(u, v);
ans += T.query(1, dfn[v], dfn[u]);
return ans;
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 2; i <= n; i++)
{
int u;
scanf("%d", &u);
++u;
add_edge(i, u);
add_edge(u, i);
}
dep[1] = 1;
dfs1(1, 0), dfs2(1, 1);
T.build(1, 1, n);
for(int i = 1; i <= m; i++)
{
int l, r, z;
scanf("%d%d%d", &l, &r, &z);
l++, r++, z++;
q[i * 2 - 1] = (query){l - 1, z, i * 2 - 1};
q[i * 2] = (query){r, z, i * 2};
}
int now = 1;
std::sort(q + 1, q + 2 * m + 1, cmp);
for(int i = 1; i <= n; i++)
{
update_chain(1, i);
while(q[now].r < i)
++now;
while(q[now].r == i)
{
ans[q[now].id] = query_chain(1, q[now].z);
++now;
}
}
for(int i = 1; i <= m; i++)
printf("%d\n", (ans[i * 2] - ans[i * 2 - 1]) % mod);
return 0;
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×