第九章 高级数据结构

第九章 高级数据结构

1、二维单调队列

2、树状数组

3、带权并查集

4、st表

📖区间最值查询问题


⭐️ST表的实现


📖例题:RMQ最值查询



📚

java 复制代码
import java.util.*;

public class Main {
    final static int N = 100005;
    final static int M = (int)(Math.log(N) / Math.log(2) + 1);
    static int n,m;
    static int[] a = new int[N];
    static int[][] f = new int[N][M];
    static int[] log2 = new int[N];
    // ST表 预处理
    static void init(){
        // 优化:log2的处理
        log2[1] = 0;
        for (int i = 2; i < N; i++) {
            log2[i] = log2[i>>1] + 1;
        }
        for (int j = 0; j < M; j++) { // 指数
            for (int i = 1; i + (1 << j) - 1 <= n; i++) {
                if(j == 0){ // 自己本身
                    f[i][j]  = a[i];
                }else{
                    f[i][j] = Math.max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }
        m = sc.nextInt();
        
        init();

        // ST表 查询
        while (m --> 0) {
            int l = sc.nextInt();
            int r = sc.nextInt();
            int ans = query(l,r);
            System.out.println(ans);
        }
    }

    static int query(int l,int r){
        int len = r - l + 1;
        int k = log2[len];
        return Math.max(f[l][k],f[r-(1<<k)+1][k]);
    }
}

📚区间最大值

5、线段树

相关推荐
小小工匠4 小时前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾4 小时前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres8216 小时前
算法复键——树状数组
数据结构·算法
牛油果子哥q8 小时前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
凌波粒9 小时前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
WL学习笔记10 小时前
单项不带头不循环链表
数据结构·链表
小糯米60111 小时前
JS 数组
数据结构·算法·排序算法
小欣加油11 小时前
leetcode3612 用特殊操作处理字符串I
数据结构·c++·算法·leetcode·职场和发展
凌波粒11 小时前
LeetCode--90.子集II(回溯算法)
数据结构·算法·leetcode
凌波粒11 小时前
LeetCode--46.全排列(回溯算法)
数据结构·算法·leetcode