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. Linux free显示讲解

    http://www.cnblogs.com/coldplayerest/archive/2010/02/20/1669949.html 解释一下Linux上free命令的输出. 下面是free的运行 ...

  2. JS 省市两级联动(不带地区版本)

    基于网上找的一个版本改造,因为项目需求不需要地区只要省.市,所以做了改版,两个input上直接取出了数据 <html> <head> <script src=" ...

  3. FFmpeg编码详细流程

    FFmpeg在编码一个视频的时候的函数调用流程.为了保证结构清晰,其中仅列出了最关键的函数,剔除了其它不是特别重要的函数. 函数背景色 函数在图中以方框的形式表现出来.不同的背景色标志了该函数不同的作 ...

  4. VS重置命令:devenv.exe/resetuserdata

    VS命令行下执行下面的命令: devenv.exe/resetuserdata

  5. GCJ Qualification Round 2016 C题

    题意是给定了一个叫“jamcoin”的定义,让你生成足够数量满足条件的jamcoin. jamcoin其实就可以理解成一个二进制整数,题目要求的要么长度为16位,要么为32位,一头一尾两个位必须是1, ...

  6. UVA10518 How Many Calls? —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/UVA-10518 题解: 问:求斐波那契数f[n]的时候调用了多少次f[n] = f[n-1] + f[n-2],没有记忆化,一直递归 ...

  7. 近期测试BUG总结

    前些日子上线了新版的app,在上线后发现了几个重大的bug,在此总结,在以后的测试工作中需要额外的关注. 需求流程bug 页面刷新bug 标签栏刷新bug 第一个bug出现的原因是产品需求与运营实际操 ...

  8. PICT实现组合测试用例

    成功安装后,在命令行中输入命令pict: 可以看到pict命令的一些选项: /o:N   组合数,默认值为2,即pict生成的测试用例集中每条测试数据会有两个值与其他测试集是不同的: /d:C   值 ...

  9. 打造基于Ubuntu+XBMC的家庭媒体中心

    作为一名高清爱好者,一直想配置一台HTPC放家里实现高清播放外加家庭服务器功能.Nvidia的Ion平台自然是高清平台的硬件首选,而家庭媒体中心的软件端则首先考虑开发的已经很成熟的开源利器 XBMC  ...

  10. android:Android中用文件初始化sqlite数据库

    很多时候在应用安装初始化时,需要创建本地数据库,同时为数据库添加数据,之后再从数据库中读取数据. 这里有2个思路 1.先在本地创建一个能支持android使用的sqlite数据库文件,启动时,用现成的 ...