Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线
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 + 线段树 + 询问离线的更多相关文章
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- dfs+线段树 zhrt的数据结构课
zhrt的数据结构课 这个题目我觉得是一个有一点点思维的dfs+线段树 虽然说看起来可以用树链剖分写,但是这个题目时间卡了树剖 因为之前用树剖一直在写这个,所以一直想的是区间更新,想dfs+线段树,有 ...
- 【Codeforces-707D】Persistent Bookcase DFS + 线段树
D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...
- 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 ...
- hdu 5692(dfs+线段树) Snacks
题目http://acm.hdu.edu.cn/showproblem.php?pid=5692 题目说每个点至多经过一次,那么就是只能一条路线走到底的意思,看到这题的格式, 多个询问多个更新, 自然 ...
- HDU 3974 Assign the task (DFS+线段树)
题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...
- 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 ...
- Partition(线段树的离线处理)
有一点类似区间K值的求法. 这里有两颗树,一个是自己建的线段树,一个是题目中给定的树.以线段树和树进行区分. 首先离散化一下,以离散化后的结果建线段树,线段树的节点开了2维,一维保存当前以当前节点为权 ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
随机推荐
- cocos2d-x项目101次相遇:在HelloWorld上--建立新场景
cocos2d-x 101次相遇 / 文件夹 1 安装和环境搭建 -xcode 2 Scenes , Director, Layers, Sprites 3 建立图片菜单 4 在 ...
- EC2的维护更新
2014年9月28日 近期几天.我们收到了一些客户关于我们即将进行维护更新的问题.下面是AWS全球Blog网站对这个问题的说明,供客户參照. 我们已经開始通知那些受影响的客户,关于我们即将实施的 ...
- CCBPM中的消息机制,CCIM服务端安装说明
1.改动LeeIMService.exe.config服务端配置文件: 2.client的配置就比較简单了,仅仅须要在"SercerIP"节点上写上server的IP: 3.然后要 ...
- 九度OJ 1129:Skew数 (大数运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:734 解决:548 题目描述: 在 skew binary表示中, 第 k 位的值xk表示xk*(2k+1-1). 每个位上的可能数字是0 ...
- Jquey模糊选择
Jquey模糊选择 属性字头选择器(Attribute Contains Prefix Selector): jQuery 属性字头选择器的使用格式是 jQuery(‘[attribute|=va ...
- FIL代币是什么?
自从比特币价格暴涨.区块链技术火了以后,出现了币圈,币圈中有各种各样的代币,本文就和大家介绍其中的FIL代币相关内容,希望能帮助大家一点一点的了解币圈. IPFS与Filecoin的关系 ...
- sdut oj 3058 路线冲突问题(BFS+记录路径算法,回溯路径 )
路线冲突问题 题目描述 给出一张地图,地图上有n个点,任意两点之间有且仅有一条路.点的编号从1到n. 现在兵团A要从s1到e1,兵团B要从s2到e2,问两条路线是否会有交点,若有则输出交点个数,否出输 ...
- Redis雪崩效应以及解决方案
缓存雪崩产生的原因 缓存雪崩通俗简单的理解就是:由于原有缓存失效(或者数据未加载到缓存中),新缓存未到期间(缓存正常从Redis中获取,如下图)所有原本应该访问缓存的请求都去查询数据库了,而对数据库C ...
- linux命令行快捷键记录
摘自: http://www.cnblogs.com/webzhangnan/p/3221410.html [移动光标] Ctrl+A 标移到行首.它在多数文本编辑器和 Mozilla 的 URL 字 ...
- 迁移学习算法之TrAdaBoost ——本质上是在用不同分布的训练数据,训练出一个分类器
迁移学习算法之TrAdaBoost from: https://blog.csdn.net/Augster/article/details/53039489 TradaBoost算法由来已久,具体算法 ...