Codeforces1110F dfs + 线段树 + 询问离线
F. Nearest Leaf
Description:
Let's define the Eulerian traversal of a tree (a connected undirected graph without cycles) as follows: consider a depth-first search algorithm which traverses vertices of the tree and enumerates them in the order of visiting (only the first visit of each vertex counts). This function starts from the vertex number \(1\) and then recursively runs from all vertices which are connected with an edge with the current vertex and are not yet visited in increasing numbers order. Formally, you can describe this function using the following pseudocode:
next_id = 1id = array of length n filled with -1visited = array of length n filled with falsefunction dfs(v): visited[v] = true id[v] = next_id next_id += 1 for to in neighbors of v in increasing order: if not visited[to]: dfs(to)You are given a weighted tree, the vertices of which were enumerated with integers from \(1\) to \(n\) using the algorithm described above.
A leaf is a vertex of the tree which is connected with only one other vertex. In the tree given to you, the vertex \(1\) is not a leaf. The distance between two vertices in the tree is the sum of weights of the edges on the simple path between them.
You have to answer \(q\) queries of the following type: given integers \(v\), \(l\) and \(r\), find the shortest distance from vertex \(v\) to one of the leaves with indices from \(l\) to \(r\) inclusive.
Input:
The first line contains two integers \(n\) and \(q\) (\(3 \leq n \leq 500\,000, 1 \leq q \leq 500\,000\)) — the number of vertices in the tree and the number of queries, respectively.
The \((i - 1)\)-th of the following \(n - 1\) lines contains two integers \(p_i\) and \(w_i\) (\(1 \leq p_i < i, 1 \leq w_i \leq 10^9\)), denoting an edge between vertices \(p_i\) and \(i\) with the weight \(w_i\).
It's guaranteed that the given edges form a tree and the vertices are enumerated in the Eulerian traversal order and that the vertex with index \(1\) is not a leaf.
The next \(q\) lines describe the queries. Each of them contains three integers \(v_i\), \(l_i\), \(r_i\) (\(1 \leq v_i \leq n, 1 \leq l_i \leq r_i \leq n\)), describing the parameters of the query. It is guaranteed that there is at least one leaf with index \(x\) such that \(l_i \leq x \leq r_i\).
Output
Output \(q\) integers — the answers for the queries in the order they are given in the input.
Sample Input:
5 3
1 10
1 1
3 2
3 3
1 1 5
5 4 5
4 1 2
Sample Output:
3
0
13
Sample Input:
5 3
1 1000000000
2 1000000000
1 1000000000
1 1000000000
3 4 5
2 1 5
2 4 5
Sample Output:
3000000000
1000000000
2000000000
Sample Input:
11 8
1 7
2 1
1 20
1 2
5 6
6 2
6 3
5 1
9 10
9 11
5 1 11
1 1 4
9 4 8
6 1 4
9 7 11
9 10 11
8 1 11
11 4 5
Sample Output:
8
8
9
16
9
10
0
34
题目链接
题解:
有一颗带边权的树,给定一个方法生成每一个点的\(id\)(实际上就是\(dfs\)序),\(q\)次询问,每次询问距离点\(v\)最近的\(id\)在\(l\)和\(r\)之间的叶子节点的距离。
首先考虑固定点\(v\)怎么做,那么相当于\(q\)次询问,一次\(dfs\)即可以处理出所有点到\(v\)的距离,线段树区间询问最小值就行了
现在考虑\(v\)点会变化的情况,考虑\(v->u\)这条边,假设现在的\(dis\)数组记录的是各个点到\(v\)的距离,那么只要把\(u\)的子树权值(子树\(dfs\)序是连续的一段)减去边权,补集加上边权就行了,只要在\(dfs\)的过程中处理询问就行了,总复杂度\(O((n + q)log(n))\)
ps:这里有一个trick,修改线段树的时候,可以先全局加上边权,子树减去两倍边权,避免了分三段讨论的情况
AC代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 5e5 + 10;
int deg[N], vis[N], id[N][2], cnt, n, q, p, w, v, l, r;
vector< pair<int, int> > edge[N];
struct query {
int l, r, id;
};
vector<query> Q[N];
LL mn[N << 2], dis[N], lazy[N << 2], ans[N];
void dfs(int rt, LL d) {
id[rt][0] = ++cnt;
dis[rt] = d;
vis[rt] = 1;
for(int i = 0; i < edge[rt].size(); ++i) {
if(vis[edge[rt][i].first])
continue;
dfs(edge[rt][i].first, d + edge[rt][i].second);
}
id[rt][1] = cnt;
}
void pushup(int rt) {
mn[rt] = min(mn[rt << 1], mn[rt << 1 | 1]);
}
void build(int rt, int l, int r) {
if(l == r) {
if(deg[l] == 1) {
mn[rt] = dis[l];
}
else
mn[rt] = 0x3f3f3f3f3f3f3f3f;
return;
}
int mid = l + r >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
pushup(rt);
}
void pushdown(int rt) {
if(lazy[rt]) {
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
mn[rt << 1] += lazy[rt];
mn[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void update(int rt, int l, int r, int L, int R, int val) {
if(L <= l && r <= R) {
lazy[rt] += val;
mn[rt] += val;
return;
}
pushdown(rt);
int mid = l + r >> 1;
if(mid >= L)
update(rt << 1, l, mid, L, R, val);
if(mid < R)
update(rt << 1 | 1, mid + 1, r, L, R, val);
pushup(rt);
}
LL get(int rt, int l, int r, int L, int R) {
if(L <= l && r <= R)
return mn[rt];
pushdown(rt);
int mid = l + r >> 1;
LL ans = 0x3f3f3f3f3f3f3f3f;
if(mid >= L)
ans = min(ans, get(rt << 1, l, mid, L, R));
if(mid < R)
ans = min(ans, get(rt << 1 | 1, mid + 1, r, L, R));
return ans;
}
void solve(int rt) {
vis[rt] = 1;
for(int i = 0; i < Q[rt].size(); ++i) {
ans[Q[rt][i].id] = get(1, 1, n, Q[rt][i].l, Q[rt][i].r);
}
for(int i = 0; i < edge[rt].size(); ++i) {
if(vis[edge[rt][i].first])
continue;
int j = edge[rt][i].first, w = edge[rt][i].second;
update(1, 1, n, id[j][0], id[j][1], -w);
if(id[j][0] > 1)
update(1, 1, n, 1, id[j][0] - 1, w);
if(id[j][1] < n)
update(1, 1, n, id[j][1] + 1, n, w);
solve(j);
update(1, 1, n, id[j][0], id[j][1], w);
if(id[j][0] > 1)
update(1, 1, n, 1, id[j][0] - 1, -w);
if(id[j][1] < n)
update(1, 1, n, id[j][1] + 1, n, -w);
}
}
int main() {
scanf("%d%d", &n, &q);
for(int i = 2; i <= n; ++i) {
scanf("%d%d", &p, &w);
edge[i].push_back(make_pair(p, w));
edge[p].push_back(make_pair(i, w));
++deg[i], ++deg[p];
}
for(int i = 1; i <= n; ++i)
sort(edge[i].begin(), edge[i].end());
dfs(1, 0);
build(1, 1, n);
for(int i = 1; i <= q; ++i) {
scanf("%d%d%d", &v, &l, &r);
Q[v].push_back({l, r, i});
}
memset(vis, 0, sizeof(vis));
solve(1);
for(int i = 1; i <= q; ++i)
printf("%lld\n", ans[i]);
return 0;
}