第九章 高级数据结构

第九章 高级数据结构

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、线段树

相关推荐
‎ദ്ദിᵔ.˛.ᵔ₎21 小时前
LIST 的相关知识
数据结构·list
M--Y21 小时前
Redis常用数据类型
数据结构·数据库·redis
༾冬瓜大侠༿1 天前
vector
c语言·开发语言·数据结构·c++·算法
汀、人工智能1 天前
[特殊字符] 第58课:两个正序数组的中位数
数据结构·算法·数据库架构··数据流·两个正序数组的中位数
liu****1 天前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯
汀、人工智能1 天前
[特殊字符] 第79课:分割等和子集
数据结构·算法·数据库架构·位运算·哈希表·分割等和子集
汀、人工智能1 天前
[特殊字符] 第74课:完全平方数
数据结构·算法·数据库架构·图论·bfs·完全平方数
CoderCodingNo1 天前
【GESP】C++四、五级练习题 luogu-P1177 【模板】排序
数据结构·c++·算法
汀、人工智能1 天前
[特殊字符] 第76课:单词拆分
数据结构·算法·均值算法·前缀树·trie·单词拆分
ambition202421 天前
斐波那契取模问题的深入分析:为什么提前取模是关键的
c语言·数据结构·c++·算法·图论