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基础(5)- nginx服务、nfs服务

    一.nginx服务 源码安装: yum install gcc-* glibc-* openssl openssl-devel pcre pcre-devel zlib zlib-devel -yls ...

  2. Java泛型 类型变量的限定

    有时候,类和方法须要对类型变量加以约束.比方你有一个方法,你仅仅希望它接收某个特定类型及其子类型作为參数. 以下就举一个方法限定接收參数的类型的样例来说明怎样限定类型变量. 首先有几个简单的辅助类: ...

  3. Linux驱动经典面试题目

    1.  linux驱动分类 2.  信号量与自旋锁 3.  platform总线设备及总线设备怎样编写 4.  kmalloc和vmalloc的差别 5.  module_init的级别 6.  加入 ...

  4. 再过半小时,你就能明白kafka的工作原理了

    本文在个人技术博客不同步发布,详情可猛戳 亦可扫描屏幕右侧二维码关注个人公众号,公众号内有个人联系方式,等你来撩... 为什么需要消息队列 周末无聊刷着手机,某宝网APP突然蹦出来一条消息" ...

  5. .Net Core表单定义

    创建表单 <form asp-controller="Account" asp-action="Login" asp-route-returnurl=&q ...

  6. 01 json方式封装通信接口

    新建一个json_api.php<?php class Response{ /** *按json方式输出通信 *@param integet $code 状态码 *@param string $ ...

  7. Django框架学习——python模拟Django框架(转载)

    原贴来源 http://wiki.woodpecker.org.cn/moin/ObpLovelyPython/AbtWebModules python实现web服务器 web开发首先要有web服务器 ...

  8. unity3d从零開始(五):了解摄像机

    1.简单介绍         Unity的摄像机是用来将游戏世界呈现给玩家的,游戏场景中至少有一台摄像机.也能够有多台. 2.类型         Unity中支持两种类型的摄像机,各自是Perspe ...

  9. Cocos2d-JS 项目接入 触控广告平台(基于anysdk2.0.2)

    本文以Cocos2d-JS项目作为示例,讲解如何集成AnySDK-JS. 一.如何创建项目我就省略了,直接进入主题. 1.1 首先,我需要下载anysdk框架包,下载地址:http://www.any ...

  10. C++设计模式实现--策略(Strategy)模式

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/L_Andy/article/details/30489331 一. 举例说明 曾经做了一个程序,程序 ...