藍橋杯備賽第四篇(高級數據結構)

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.pswp.cn/news/712699.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/712699.shtml
英文地址,請注明出處:http://en.pswp.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…