蓝桥杯备赛第四篇(高级数据结构)

2024-03-01 23:28

本文主要是介绍蓝桥杯备赛第四篇(高级数据结构),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.树状数组

	public static int getSum(int i) {int sum = 0;for (int j = i; j >= 1; j -= lowbit(j)) {sum += tree[j];}return sum;}public static void update(int i, int update) {for (int j = i; j <= n; j += lowbit(j)) {tree[j] += update;}}public static int lowbit(int num) {return num & -num;}

2.ST表

作用是:快速进行区间查询,ST表创建O ( n l o g ( n ) ) O(nlog(n))O(nlog(n)),查询O ( 1 ) O(1)O(1),不支持在线修改

public class ST表 {static int[] arr;static int n;static int[][] dp;//nlog(n)public static void createST() {int max = (int) (Math.log(n) / Math.log(2));dp = new int[n + 1][max + 1];//自己到自己的最值就是自己for (int i = 1; i < n; i++) {dp[i][0] = arr[i];}for (int j = 1; j <= max; j++) {for (int i = 1; i + (1 << j) - 1 <= n; i++) {dp[i][j] = Math.max(dp[i][j - 1], dp[i + (1 << (j - 1)) + 1][j - 1]);}}}//o(1)public static int query(int l, int r) {int max = (int) (Math.log(l - r + 1) / Math.log(2));return Math.max(dp[l][max], dp[r - (1 << max) + 1][max]);}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);n = scanner.nextInt();arr = new int[n + 1];for (int i = 1; i <= n; i++) {arr[i] = scanner.nextInt();}}
}

3.线段树

//区间和
import java.util.Scanner;
public class Main {static int[] a;static int n;static Node[] tree;static class Node {int l;int r;int num;int lazy;//用于记录对于这个节点进行过什么操作public Node(int l, int r, int num) {this.l = l;this.r = r;this.num = num;}}public static void build(int index, int l, int r) {if (l == r) {tree[index] = new Node(l, r, a[l]);return;}int mid = (l + r) / 2;build(index * 2, l, mid);build(index * 2 + 1, mid + 1, r);tree[index] = new Node(l, r, tree[index * 2].num + tree[index * 2 + 1].num);}//单点修改public static void update(int index, int i, int newnum) {if (tree[index].l == tree[index].r && tree[index].r == i) {tree[index].num = newnum;return;}int mid = (tree[index].l + tree[index].r) / 2;if (i <= mid) {update(index * 2, i, newnum);} else {update(index * 2 + 1, i, newnum);}tree[index].num = tree[index * 2].num + tree[index * 2 + 1].num;}//区间修改:给区间每个值加dpublic static void change(int index, int l, int r, int d) {if (l <= tree[index].l && tree[index].r <= r) {tree[index].num += (tree[index].r - tree[index].l + 1) * d;tree[index].lazy += d;return;}spred(index);int mid = (tree[index].l + tree[index].r) / 2;if (l <= mid) {change(index * 2, l, r, d);}if (r > mid) {change(index * 2 + 1, l, r, d);}tree[index].num = tree[index * 2].num + tree[index * 2 + 1].num;}//一共5个步骤public static void spred(int index) {if (tree[index].lazy != 0) {tree[index * 2].num += (tree[index * 2].r - tree[index * 2].l + 1) * tree[index].lazy;tree[index * 2 + 1].num += (tree[index * 2 + 1].r - tree[index * 2 + 1].l + 1) * tree[index].lazy;tree[index * 2].lazy += tree[index].lazy;tree[index * 2 + 1].lazy += tree[index].lazy;tree[index].lazy = 0;}}public static int ask(int index, int l, int r) {if (l <= tree[index].l && tree[index].r <= r) {return tree[index].num;}spred(index);int sum = 0;int mid = (tree[index].l + tree[index].r) / 2;if (l <= mid) {sum += ask(index * 2, l, r);}if (r > mid) {sum += ask(index * 2 + 1, l, r);}return sum;}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);n = scanner.nextInt();a = new int[n + 1];tree = new Node[n * 4];int m = scanner.nextInt();for (int i = 1; i <= n; i++) {a[i] = scanner.nextInt();}build(1, 1, n);for (int i = 0; i < m; i++) {int caozuo = scanner.nextInt();if (caozuo == 1) {int x = scanner.nextInt();int y = scanner.nextInt();int k = scanner.nextInt();change(1, x, y, k);} else {int x = scanner.nextInt();int y = scanner.nextInt();System.out.println(ask(1, x, y));}}}
}

这篇关于蓝桥杯备赛第四篇(高级数据结构)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/764179

相关文章

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

《数据结构(C语言版)第二版》第八章-排序(8.3-交换排序、8.4-选择排序)

8.3 交换排序 8.3.1 冒泡排序 【算法特点】 (1) 稳定排序。 (2) 可用于链式存储结构。 (3) 移动记录次数较多,算法平均时间性能比直接插入排序差。当初始记录无序,n较大时, 此算法不宜采用。 #include <stdio.h>#include <stdlib.h>#define MAXSIZE 26typedef int KeyType;typedef char In

Java基础回顾系列-第七天-高级编程之IO

Java基础回顾系列-第七天-高级编程之IO 文件操作字节流与字符流OutputStream字节输出流FileOutputStream InputStream字节输入流FileInputStream Writer字符输出流FileWriter Reader字符输入流字节流与字符流的区别转换流InputStreamReaderOutputStreamWriter 文件复制 字符编码内存操作流(

Java基础回顾系列-第五天-高级编程之API类库

Java基础回顾系列-第五天-高级编程之API类库 Java基础类库StringBufferStringBuilderStringCharSequence接口AutoCloseable接口RuntimeSystemCleaner对象克隆 数字操作类Math数学计算类Random随机数生成类BigInteger/BigDecimal大数字操作类 日期操作类DateSimpleDateForma

【408数据结构】散列 (哈希)知识点集合复习考点题目

苏泽  “弃工从研”的路上很孤独,于是我记下了些许笔记相伴,希望能够帮助到大家    知识点 1. 散列查找 散列查找是一种高效的查找方法,它通过散列函数将关键字映射到数组的一个位置,从而实现快速查找。这种方法的时间复杂度平均为(

浙大数据结构:树的定义与操作

四种遍历 #include<iostream>#include<queue>using namespace std;typedef struct treenode *BinTree;typedef BinTree position;typedef int ElementType;struct treenode{ElementType data;BinTree left;BinTre

Python 内置的一些数据结构

文章目录 1. 列表 (List)2. 元组 (Tuple)3. 字典 (Dictionary)4. 集合 (Set)5. 字符串 (String) Python 提供了几种内置的数据结构来存储和操作数据,每种都有其独特的特点和用途。下面是一些常用的数据结构及其简要说明: 1. 列表 (List) 列表是一种可变的有序集合,可以存放任意类型的数据。列表中的元素可以通过索

Mysql高级篇(中)——索引介绍

Mysql高级篇(中)——索引介绍 一、索引本质二、索引优缺点三、索引分类(1)按数据结构分类(2)按功能分类(3) 按存储引擎分类(4) 按存储方式分类(5) 按使用方式分类 四、 索引基本语法(1)创建索引(2)查看索引(3)删除索引(4)ALTER 关键字创建/删除索引 五、适合创建索引的情况思考题 六、不适合创建索引的情况 一、索引本质 索引本质 是 一种数据结构,它用