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


    1.树状数组

    1. public static int getSum(int i) {
    2. int sum = 0;
    3. for (int j = i; j >= 1; j -= lowbit(j)) {
    4. sum += tree[j];
    5. }
    6. return sum;
    7. }
    8. public static void update(int i, int update) {
    9. for (int j = i; j <= n; j += lowbit(j)) {
    10. tree[j] += update;
    11. }
    12. }
    13. public static int lowbit(int num) {
    14. return num & -num;
    15. }

    2.ST表

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

    1. public class ST表 {
    2. static int[] arr;
    3. static int n;
    4. static int[][] dp;
    5. //nlog(n)
    6. public static void createST() {
    7. int max = (int) (Math.log(n) / Math.log(2));
    8. dp = new int[n + 1][max + 1];
    9. //自己到自己的最值就是自己
    10. for (int i = 1; i < n; i++) {
    11. dp[i][0] = arr[i];
    12. }
    13. for (int j = 1; j <= max; j++) {
    14. for (int i = 1; i + (1 << j) - 1 <= n; i++) {
    15. dp[i][j] = Math.max(dp[i][j - 1], dp[i + (1 << (j - 1)) + 1][j - 1]);
    16. }
    17. }
    18. }
    19. //o(1)
    20. public static int query(int l, int r) {
    21. int max = (int) (Math.log(l - r + 1) / Math.log(2));
    22. return Math.max(dp[l][max], dp[r - (1 << max) + 1][max]);
    23. }
    24. public static void main(String[] args) {
    25. Scanner scanner = new Scanner(System.in);
    26. n = scanner.nextInt();
    27. arr = new int[n + 1];
    28. for (int i = 1; i <= n; i++) {
    29. arr[i] = scanner.nextInt();
    30. }
    31. }
    32. }

    3.线段树

    1. //区间和
    2. import java.util.Scanner;
    3. public class Main {
    4. static int[] a;
    5. static int n;
    6. static Node[] tree;
    7. static class Node {
    8. int l;
    9. int r;
    10. int num;
    11. int lazy;//用于记录对于这个节点进行过什么操作
    12. public Node(int l, int r, int num) {
    13. this.l = l;
    14. this.r = r;
    15. this.num = num;
    16. }
    17. }
    18. public static void build(int index, int l, int r) {
    19. if (l == r) {
    20. tree[index] = new Node(l, r, a[l]);
    21. return;
    22. }
    23. int mid = (l + r) / 2;
    24. build(index * 2, l, mid);
    25. build(index * 2 + 1, mid + 1, r);
    26. tree[index] = new Node(l, r, tree[index * 2].num + tree[index * 2 + 1].num);
    27. }
    28. //单点修改
    29. public static void update(int index, int i, int newnum) {
    30. if (tree[index].l == tree[index].r && tree[index].r == i) {
    31. tree[index].num = newnum;
    32. return;
    33. }
    34. int mid = (tree[index].l + tree[index].r) / 2;
    35. if (i <= mid) {
    36. update(index * 2, i, newnum);
    37. } else {
    38. update(index * 2 + 1, i, newnum);
    39. }
    40. tree[index].num = tree[index * 2].num + tree[index * 2 + 1].num;
    41. }
    42. //区间修改:给区间每个值加d
    43. public static void change(int index, int l, int r, int d) {
    44. if (l <= tree[index].l && tree[index].r <= r) {
    45. tree[index].num += (tree[index].r - tree[index].l + 1) * d;
    46. tree[index].lazy += d;
    47. return;
    48. }
    49. spred(index);
    50. int mid = (tree[index].l + tree[index].r) / 2;
    51. if (l <= mid) {
    52. change(index * 2, l, r, d);
    53. }
    54. if (r > mid) {
    55. change(index * 2 + 1, l, r, d);
    56. }
    57. tree[index].num = tree[index * 2].num + tree[index * 2 + 1].num;
    58. }
    59. //一共5个步骤
    60. public static void spred(int index) {
    61. if (tree[index].lazy != 0) {
    62. tree[index * 2].num += (tree[index * 2].r - tree[index * 2].l + 1) * tree[index].lazy;
    63. tree[index * 2 + 1].num += (tree[index * 2 + 1].r - tree[index * 2 + 1].l + 1) * tree[index].lazy;
    64. tree[index * 2].lazy += tree[index].lazy;
    65. tree[index * 2 + 1].lazy += tree[index].lazy;
    66. tree[index].lazy = 0;
    67. }
    68. }
    69. public static int ask(int index, int l, int r) {
    70. if (l <= tree[index].l && tree[index].r <= r) {
    71. return tree[index].num;
    72. }
    73. spred(index);
    74. int sum = 0;
    75. int mid = (tree[index].l + tree[index].r) / 2;
    76. if (l <= mid) {
    77. sum += ask(index * 2, l, r);
    78. }
    79. if (r > mid) {
    80. sum += ask(index * 2 + 1, l, r);
    81. }
    82. return sum;
    83. }
    84. public static void main(String[] args) {
    85. Scanner scanner = new Scanner(System.in);
    86. n = scanner.nextInt();
    87. a = new int[n + 1];
    88. tree = new Node[n * 4];
    89. int m = scanner.nextInt();
    90. for (int i = 1; i <= n; i++) {
    91. a[i] = scanner.nextInt();
    92. }
    93. build(1, 1, n);
    94. for (int i = 0; i < m; i++) {
    95. int caozuo = scanner.nextInt();
    96. if (caozuo == 1) {
    97. int x = scanner.nextInt();
    98. int y = scanner.nextInt();
    99. int k = scanner.nextInt();
    100. change(1, x, y, k);
    101. } else {
    102. int x = scanner.nextInt();
    103. int y = scanner.nextInt();
    104. System.out.println(ask(1, x, y));
    105. }
    106. }
    107. }
    108. }

  • 相关阅读:
    反射学习总结
    ROS2与turtlebot4仿真入门教程-turtlebot4单点导航
    监听器
    2023年03月 Scratch(一级)真题解析#中国电子学会#全国青少年软件编程等级考试
    什么是机器学习中的监督学习和无监督学习,举例说明
    全网超50万粉丝的Linux大咖良许,出书了!
    mall-1-搭建环境
    关于初始化page入参的设计思路
    踩了个DNS解析的坑,但我还是没想通
    userver-framework/userver
  • 原文地址:https://blog.csdn.net/m0_73473352/article/details/136406653