归并树 学习笔记

归并树

Problem

给定一个长为 n n n 的数组。
q q q 次查询,每次查询包括一个区间,需要回答关于区间元素排序的问题

示例 :区间第 k k k 大值,link

数据强制在线 , n , q ≤ 1 0 5 n,q \leq 10^5 n,q≤105。

Algorithm description

Method

归并树顾名思义,就是逐个归并的树。

每个节点是其子节点代表的数组归并后的数组,这样能使任意一个节点有序。

也就是说,建树时,每个节点代表了 a l a_l al 至 a r a_r ar 归并后的数组

我们在实现时,为了节约空间,可以开 dep × n \text{dep}\times n dep×n 的归并数组,每一层为下一层归并的结果。

这种树的优势在于,能够像线段树一样 O ( log ⁡ n ) O(\log n) O(logn) 统计出可以叠加的排序的结果

Code

以 POJ2104 (见上文)为例,可以二分答案再查找。

cpp 复制代码
int sorted[25][2000001], a[200001];

void build(int l, int r, int dep){
    if(l == r){
        sorted[dep][l] = a[l]; return;
    }
    int mid = l + r >> 1;
    build(l, mid, dep + 1);
    build(mid + 1, r, dep + 1);
    // Merging.
    int pos = l, pos1 = mid + 1, tot = l;
    while(pos <= mid && pos1 <= r){
        if(sorted[dep + 1][pos] < sorted[dep + 1][pos1])
            sorted[dep][tot] = sorted[dep + 1][pos], pos++, tot++;
        else
            sorted[dep][tot] = sorted[dep + 1][pos1], pos1++, tot++;
    }
    while(pos <= mid) sorted[dep][tot] = sorted[dep + 1][pos], pos++, tot++;
    while(pos1 <= r) sorted[dep][tot] = sorted[dep + 1][pos1], pos1++, tot++;
}
int query(int l, int r, int x, int y, int tar, int dep){
    if(x <= l && r <= y){
    	// Here it is possible to calculate the sum thanks to the merging above.
        return upper_bound(sorted[dep] + l, sorted[dep] + r + 1, tar) - sorted[dep] - l;
    }
    int mid = l + r >> 1, ans = 0;
    if(x <= mid) ans += query(l, mid, x, y, tar, dep + 1);
    if(mid < y) ans += query(mid + 1, r, x, y, tar, dep + 1);
    return ans;
}
相关推荐
hn小菜鸡3 小时前
LeetCode 377.组合总和IV
数据结构·算法·leetcode
Deepoch4 小时前
Deepoc 大模型:无人机行业的智能变革引擎
人工智能·科技·算法·ai·动态规划·无人机
heimeiyingwang9 天前
【深度学习加速探秘】Winograd 卷积算法:让计算效率 “飞” 起来
人工智能·深度学习·算法
时空自由民.9 天前
C++ 不同线程之间传值
开发语言·c++·算法
ai小鬼头9 天前
AIStarter开发者熊哥分享|低成本部署AI项目的实战经验
后端·算法·架构
小白菜3336669 天前
DAY 37 早停策略和模型权重的保存
人工智能·深度学习·算法
zeroporn9 天前
以玄幻小说方式打开深度学习词嵌入算法!! 使用Skip-gram来完成 Word2Vec 词嵌入(Embedding)
人工智能·深度学习·算法·自然语言处理·embedding·word2vec·skip-gram
亮亮爱刷题9 天前
飞往大厂梦之算法提升-7
数据结构·算法·leetcode·动态规划
_周游9 天前
【数据结构】_二叉树OJ第二弹(返回数组的遍历专题)
数据结构·算法
双叶8369 天前
(C语言)Map数组的实现(数据结构)(链表)(指针)
c语言·数据结构·c++·算法·链表·哈希算法