RMQ
















int

i, j;
//we initialize every element in P with -1 for (i = 0; i < N; i++) for (j = 0; 1 << j < N; j++) P[i][j] = -1;
//the first ancestor of every node i is T[i] for (i = 0; i < N; i++) P[i][0] = T[i];
//bottom up dynamic programing for (j = 1; 1 << j < N; j++) for (i = 0; i < N; i++) if (P[i][j - 1] != -1) P[i][j] = P[P[i][j - 1]][j - 1]; }
这个过程将花费O(N logN) 的时间和空间。现在让我们看看如何查询。用L[i]来表示节点i在树中所处的层数。可以看到,如果p和q在树中的同一层中,我们可以使用一个类二分查找的方法进行搜索。因此,对于2的j次方(界于log[L[p]和0之间,降序),如果P[p][j] != P[q][j] ,那么可以知道LCA(p, q)必然在更高的层中,因此我们继续搜索LCA(p = P[p][j], q = P[q][j])。最后,p和q都有了相同的祖先,因此返回T[p]。让我们看看如果L[p] != L[q]的情况。 不妨假设L[p] < L[q]。我们可以使用
Range Minimum Query and Lowest Common Ancestor By danielp TopCoder Member Introduction Notations Range Minimum Query (RMQ) Trivial algorithms for RMQ A <O...
Topcoder 上有一篇详细阐述 RMQ 问题的“ Range Minimum Query and Lowest Common Ancestor ”,网上也有翻译版。在此,我来简单引用 & 总结下。至于为何要总结 ...
The lowest common ancestor is defined between two nodes v and w as the ... Range Minimum Query an... 23页 1下载券 Range Minimum Query an... ...
RMQ:Range Minimum Maximum Query – 在一个序列上,给定区间,求区间极值 ? LCA:Lowest Common Ancestor – 最近公共祖先是一个节点,是指定两点的祖先, 且深度...
(smallest lowest common ancestor) problem, and ...Query instances of XQuery FT and XML IR/fragment...Structural join of XML based on range partitioning...
(smallest lowest common ancestor); dewey encoding;...Query instances of XQuery FT and XML IR/fragment...Structural join of XML based on range partitioning...
lca = lowest common ancestor ? u = lca(v,w): v と w の共通の祖先 ...5 6 7 8 9 10 11 u v mw 5 6 13 Range Minimum Query 问题 (RMQ) ...
Abstract. The Range-Minimum-Query-Problem is to preprocess an array such ...1 Introduction The problem of ?nding the lowest common ancestor (LCA) of ...
Key words:algorithm design; binary tree; inorder traversal; query fast 摘要...[18] Daniel P.Range minimum query and lowest common ancestor[EB/OL]. ...
// 与根结点U合并(并查集操作) Find(u).ancestor?:=?u?? // 将u所在...57、P94? ②《Range? Minimum? Query? and? Lowest? Common? Ancestor》by?...
热门文档