第九章 高级数据结构

第九章 高级数据结构

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

相关推荐
春弦_5 小时前
自用算法整理二
数据结构·算法·图论
xxxiugou12310 小时前
链表算法题精讲:从两数相加到K组翻转
数据结构·c++·算法·链表
悠仁さん14 小时前
【算法C++】双指针(一)
数据结构·c++·算法
YukiRinLL14 小时前
字符串存储与 BF、KMP 匹配算法
数据结构·算法
INGNIGHT16 小时前
151 · 买卖股票的最佳时机 III(序列dp)
数据结构·算法·leetcode
Tisfy18 小时前
LeetCode 3644.排序排列:不对则与(脑筋急转弯)
数据结构·算法·leetcode·题解·位运算
H.莓飛19 小时前
【数据结构】快速排序专题
linux·服务器·c语言·数据结构·visual studio
重生之小比特20 小时前
【算法拾光】二分查找|在排序数组中查找元素的第一个和最后一个位置 + 搜索旋转排序数组
数据结构·算法·排序算法
YukiRinLL20 小时前
查找与排序算法:复杂度、稳定性
数据结构·算法·排序算法
careathers1 天前
【数据结构】从堆到 PriorityQueue:手写一个优先级队列
数据结构