首页 > 编程语言 > 详细

树状数组

时间:2020-06-23 19:04:18      阅读:52      评论:0      收藏:0      [点我收藏+]

树状数组

本博客仅贴出树状数组模板

#include <bits/stdc++.h>

#define lowbit(x) (x & -x)

using namespace std;

const int N = 10010;
int a[N], n;

//a[x] += c
void insert(int x, int c){
      for(;x <= n; x += lowbit(x))a[x] += c;
}

// sum([1, x])
int get(int x){
      int res = 0;
      for(;x > 0; x -= lowbit(x))res += a[x];
      return res;
}

int main(){
      int t;
      cin >> n;
      for(int i = 1;i <= n; i++)cin >> t, insert(i, t);
}

树状数组

原文:https://www.cnblogs.com/waitti/p/13183723.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!