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;
}

Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线的更多相关文章

  1. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  2. dfs+线段树 zhrt的数据结构课

    zhrt的数据结构课 这个题目我觉得是一个有一点点思维的dfs+线段树 虽然说看起来可以用树链剖分写,但是这个题目时间卡了树剖 因为之前用树剖一直在写这个,所以一直想的是区间更新,想dfs+线段树,有 ...

  3. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

  4. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

  5. hdu 5692(dfs+线段树) Snacks

    题目http://acm.hdu.edu.cn/showproblem.php?pid=5692 题目说每个点至多经过一次,那么就是只能一条路线走到底的意思,看到这题的格式, 多个询问多个更新, 自然 ...

  6. HDU 3974 Assign the task (DFS+线段树)

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

  7. hdu-2586 How far away ?(lca+bfs+dfs+线段树)

    题目链接: How far away ? Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Ot ...

  8. Partition(线段树的离线处理)

    有一点类似区间K值的求法. 这里有两颗树,一个是自己建的线段树,一个是题目中给定的树.以线段树和树进行区分. 首先离散化一下,以离散化后的结果建线段树,线段树的节点开了2维,一维保存当前以当前节点为权 ...

  9. HDU 3874 Necklace (树状数组 | 线段树 的离线处理)

    Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

随机推荐

  1. XML(四)dom4j解析XML

    使用dom4j须要导入jar包 jar包下载地址:http://pan.baidu.com/s/1o65jWRw 将dom4j-1.6.1.jar包导入Eclipse book2.xml <?x ...

  2. CSS入门学习

    一.What? CSS的全称是CascadingStyle Sheet,汉语意思是"级联样式表".通常又称为"风格样式表(StyleSheet)".它是用来进行 ...

  3. PythonCookBook笔记——迭代器与生成器

    迭代器与生成器 迭代是Python最强大的功能之一,虽然看起来迭代只是处理序列中元素的一种方法,但不仅仅如此. 手动遍历迭代器 想遍历但不想使用for循环. 使用next()方法并在代码中捕获Stop ...

  4. slidemenu

    1. 在github上有一个效果不错的开源库,SlidingMenu 最新的代码下载下来后,会报错: No resource found that matches the given name: at ...

  5. springboot实战--笔记

    由于这本书看过一遍,所以这里是二次复习,记录的东西比较少,就不分章节了. 共12章,524页,预计时间是18h 第一章 spring基础: 第二章 spring常用配置: bean的Scope:sin ...

  6. React深入源码--了解Redux用法之Provider

    在Redux中最核心的自然是组件,以及组件相关的事件与数据流方式.但是我们在Redux中并没有采用传统的方式在getInitialState()中去初始化数据,而是采用Provider统一处理,省去了 ...

  7. 使用c函数库的两个函数strtok, strncpy遇到的问题记录

    1. strtok 问题背景: 解析形如 “1,2,3,4,5”字符串到整数数组 (1)计算个数 char* delim = ","; int count = 0; int *nu ...

  8. appium(10)-iOS predictate

    iOS predictate It is worth looking at ’-ios uiautomation’ search strategy with Predicates. UIAutomat ...

  9. Cocos2d-x如何添加新场景及切换新场景(包括场景特效)

    做了一天多的工作终于把此功能搞定了,实际上添加新场景花费不了多少时间,时间主要花在切换到另一个场景的实现上,主要原因是编译时出现了一个错误,百思不得其解,后来经过查资料不断摸索才知道自己问题的所在,改 ...

  10. spring+mybatis项目整合

    前辈总结的很详细,贴出链接,参考学习 http://www.open-open.com/lib/view/open1392252233301.html