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

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.mzph.cn/news/712699.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

00x集体——cad 中DB(database)对象—— vba实现

Database 对象 外部参照块的内容。 VBA 类名 AcadDatabase 创建方法 不可使用 访问途径 Block.XRefDatabase 该对象提供对外部参照块的访问。只有IsXRef属性为TRUE的块才适用。 方法 CopyObjects 属性 Application Blocks Dictionaries DimStyles Ele…

Mybatis 主从表有名字相同,只能查询出一条数据

Mybatis 主从表有名字相同&#xff0c;只能查询出一条数据 重新命名后&#xff0c;可以正常查询

力扣SQL50 使用唯一标识码替换员工ID 查询

Problem: 1378. 使用唯一标识码替换员工ID 思路 left join&#xff1a;左连接 Code select eu.unique_id,e.name from Employees e left join EmployeeUNI eu # left join 左连接 on e.id eu.id;

勒索病毒防范建议——企业缓解措施

勒索病毒防范建议——企业缓解措施 为公司的作业系统和应用程序保持为更新版本。 应用最新的安全补丁&#xff0c;确保关键软件是最新的&#xff0c;移动设备亦一样。可以的话&#xff0c;启用自动更新选项。 定時更新将确保设备更安全&#xff0c;性能亦更好。评估是否需要安…

零基础小白到底适不适合学鸿蒙,请看完这篇再决定吧~

随着华为鸿蒙系统的问世&#xff0c;不少技术小白在是否学习鸿蒙的问题上犹豫不决。鸿蒙作为华为自主研发的操作系统&#xff0c;拥有许多独特的技术优势和市场前景。但对于小白来说&#xff0c;是否值得投入时间和精力去学习鸿蒙开发呢&#xff1f; 1.鸿蒙系统开发&#xff1…

【总结】对大量函数进行trace调用流程+国际AIS3题

现在混淆的主要目的之一就有让逆向分析人员不清楚函数的调用流程&#xff0c;给你一堆函数&#xff0c;加了高强度的OLLVM&#xff0c;更不能看了。那么Trace跟踪技术就显得很重要的&#xff0c;如果清楚了函数调用流程&#xff0c;那么逐个分析&#xff0c;距离成功不就很快了…

前段时间公司招人,面了一个要20K的,一问自动化只会点皮毛···

前段时间公司要招2个自动化测试&#xff0c;同事面了几十个候选人&#xff0c;发现了一个很奇怪的现象&#xff0c;面试的时候&#xff0c;如果问的是框架api、脚本编写这些问题&#xff0c;基本上个个都能对答如流&#xff0c;等问到实际项目的时候&#xff0c;类似“怎么从0开…

Spring - InitializingBean、@PostConstruct、@Bean(initMethod = “init“)和构造方法执行优先级比较

执行顺序优先级 构造方法 > postConstruct > afterPropertiesSet > init方法 代码案例 Component public class InitializingBeanTest implements InitializingBean {public InitializingBeanTest(){System.out.println("构造方法");}Overridepublic void…

《滴滴》24校招Java后端

1.问项目 2.Java的基本数据类型&#xff1f; 3.浮点型从二进制的视角是怎么存储的&#xff1f;&#xff08;IEEE 754&#xff09;小数位如何计算出来的&#xff1f; 4.浮点型的正4.5和负4.5转为int会怎么样&#xff1f; 5.Int型999除float的100再乘100结果&#xff1f; 6.Strin…

实现窗帘系统监控功能-代码实现

自定义监控指标是实现窗帘系统监控功能的关键一步。这通常涉及到你想要跟踪和衡量的系统特定方面的数据。以下是一些步骤和考虑因素&#xff0c;可以帮助你自定义监控指标&#xff1a; 1.明确监控目标&#xff1a; 确定你想要监控的窗帘系统的具体方面。这可能包括窗帘的开关状…

基于yolov8的半自动标注

一、前言介绍 在深度学习领域中&#xff0c;标注是一项非常重要的工作&#xff0c;因为许多深度学习模型都依赖于有标注的数据进行训练。然而&#xff0c;标注数据是一个费时费力的工作&#xff0c;因此人们希望有一种方式来对标注过程进行自动化。这就是“半自动标注”的来源…

Linux入门攻坚——16、Linux系统启动流程

CentOS5、6的启动流程 Linux&#xff1a;kernel rootfs&#xff0c;Linux系统就是内核加上根文件系统。 内核之上是库&#xff1a; 库&#xff1a;函数集合&#xff0c;function&#xff0c;函数具有调用接口&#xff0c;库函数不能单独执行&#xff0c;必须被其他程序调用…

【前端素材】推荐优质在线电影院商城电商网页Hyper平台模板(附源码)

一、需求分析 1、系统定义 在线电影商城是指一个通过互联网提供电影服务的平台&#xff0c;用户可以在该平台上浏览电影资源、租借或购买电影&#xff0c;以及观看在线影片。 2、功能需求 在线电影商城是指一个通过互联网提供电影服务的平台&#xff0c;用户可以在该平台上…

四川尚熠电子商务有限公司电商服务领域的佼佼者

在数字化浪潮席卷全球的今天&#xff0c;电子商务已成为推动企业转型升级、拓展市场渠道的重要力量。四川尚熠电子商务有限公司&#xff0c;作为一家专注于抖音电商服务的公司&#xff0c;凭借其独特的服务模式和创新的营销策略&#xff0c;在激烈的市场竞争中脱颖而出&#xf…

Linux 系统安装/卸载 Nginx教程

优质博文&#xff1a;IT-BLOG-CN 一、安装Nginx 【1】首先通过Nginx官网确定需要安装的版本&#xff0c;如果Linux联网则直接在Linux服务上使用wget命令将Nginx安装包下载到/usr/local/目录下&#xff1a; [rootxxx local]# wget -c http://nginx.org/download/nginx-1.22.1.…

【C++精简版回顾】14.(重载2)流重载

1.流重载 istream ostream 1.class class MM {friend ostream& operator<<(ostream& out, MM& mm);friend istream& operator>>(istream& in, MM& mm); public:MM() {}MM(int age,string name):age(age),name(name) {} private:int age;st…

Three.js-05坐标轴AxesHelper

1.构建对象 说明&#xff1a;参数一表示坐标轴的长度。红色代表 X 轴. 绿色代表 Y 轴. 蓝色代表 Z 轴. const axesHelper new THREE.AxesHelper( 1 ); 2.设置位置 axesHelper.position.y1 axesHelper.position.x1 axesHelper.position.z1 3. 网格 说明&#xff1a;立方体…

没有项目经历,该如何写简历?

没有项目经历&#xff0c;我该如何写简历 一、前言二、挖掘自己三、看现成的项目经验&#xff0c;转化成自己的语言1、硬件方面2、软件方面 四、最后 一、前言 相信有很多刚出来找工作的人会遇到这种情况&#xff0c;因为自身没有项目经历&#xff0c;投了很多的简历都石沉大海…

在python中,设置json支持中文字符串

# 省略以上环节 ... # 假设json格式如下 system_info_dict {uptime: uptime.split(".")[0],cpu_usage: cpu_usage,memory_usage: memory_usage,disk_usage: disk_usage,battery_percentage: battery_percentage,battery_status: batteryStatus }# 设置json支持中文字…

Day05:反弹SHELL不回显带外正反向连接防火墙出入站文件下载

目录 常规基本渗透命令 文件上传下载-解决无图形化&解决数据传输 反弹Shell命令-解决数据回显&解决数据通讯 防火墙绕过-正向连接&反向连接&内网服务器 防火墙组合数据不回显-ICMP带外查询Dnslog 思维导图 章节知识点&#xff1a; 应用架构&#xff1a;W…