树状数组模板

单点更新 区间查询

使用树状数组维护原数组即可

public class Test01 {
    static final int N = 10010;
    static int[] c = new int[N];
    static int n;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for (int i = 1; i <= n; i++) {
            int x = in.nextInt();
            update(i, x);
        }

        // 这里执行单点修改的代码

        // q个查询,区间查询
        int q = in.nextInt();
        while (q-- > 0) {
            int l, r;
            l = in.nextInt();
            r = in.nextInt();
            System.out.println(sums(r) - sums(l - 1));
        }
    }

    /**
     * 更新原数组中的idx
     *
     * @param idx
     * @param x
     */
    public static void update(int idx, int x) {
        while (idx <= n) {
            c[idx] += x;
            idx += idx & -idx;
        }
    }

    /**
     * 求原数组中a[1...j]的和
     *
     * @param j
     * @return
     */
    public static int sums(int j) {
        int res = 0;
        while (j > 0) {
            res += c[j];
            j -= j & -j;
        }
        return res;
    }
}

区间更新 单点查询

使用树状数组维护原数组的差分数组

/**
 * 树状数组
 * 区间修改 单点查询
 * 使用树状数组维护原数组的差分数组
 */
public class Test02 {
    static final int N = 10010;
    static int[] c = new int[N];
    static int[] d = new int[N];
    static int n;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for (int i = 1; i <= n; i++) {
            int x = in.nextInt();
            update(i, x);
            update(i + 1, -x);
        }

        // 这里执行区间修改的代码

        // 单点查询,输出原数组
        for(int i = 1; i <= n; i ++){
            System.out.print(getsum(i) + " ");
        }
    }

    /**
     * 如果对区间a[l...r]+x 即 update(l, x);update(r + 1, -x);
     * @param idx
     * @param x
     */
    public static void update(int idx, int x) {
        while (idx <= n) {
            c[idx] += x;
            idx += idx & -idx;
        }
    }

    /**
     * 求原数组a[j]
     * @param j
     */
    public static int getsum(int j) {
        int res = 0;
        while(j > 0){
            res += c[j];
            j -= j & -j;
        }
        return res;
    }
}

相关推荐

  1. 树状数组模板

    2024-04-05 19:22:02       43 阅读
  2. 线段树、树状数组、归并排序模板

    2024-04-05 19:22:02       35 阅读
  3. 数据结构-树状数组

    2024-04-05 19:22:02       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-05 19:22:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-05 19:22:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-05 19:22:02       82 阅读
  4. Python语言-面向对象

    2024-04-05 19:22:02       91 阅读

热门阅读

  1. 自动化缺陷检测:提升产品质量的关键

    2024-04-05 19:22:02       35 阅读
  2. 谷歌(Google)技术面试概述

    2024-04-05 19:22:02       35 阅读
  3. 逻辑回归都有什么类型

    2024-04-05 19:22:02       28 阅读
  4. RKE2部署k8s集群实战

    2024-04-05 19:22:02       37 阅读
  5. docker入门

    2024-04-05 19:22:02       45 阅读
  6. QT之单例模式

    2024-04-05 19:22:02       39 阅读
  7. 软件测试用例(3)

    2024-04-05 19:22:02       41 阅读
  8. 深入理解Spring框架:设计模式的巧妙运用

    2024-04-05 19:22:02       43 阅读
  9. centOS安装git客户端

    2024-04-05 19:22:02       38 阅读
  10. 纯C++设置浮点数精度

    2024-04-05 19:22:02       37 阅读
  11. Flask学习(七):pymysql链接数据库

    2024-04-05 19:22:02       44 阅读