首页 > 其他分享 >树状数组

树状数组

时间:2023-03-21 18:14:11浏览次数:31  
标签:std 10 树状 int 数组 include

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10;

int a[N], tr[N];
int n, m;

int lowbit(int x)
{
    return x & -x;
}

//在a位置处加上一个b
voi0d add(int a, int b)
{
    for(int i = a; i <= n; i += lowbit(i))  tr[i] += b;
}

//返回sx
int query(int x)
{
    int res = 0;
    for(int i = x; i; i -= lowbit(i))   res += tr[i];
    return res;
}

int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++ i)    scanf("%d", &a[i]);
    for(int i = 1; i <= n; ++ i)    add(i, a[i]);
    for(int i = 0; i < m; ++ i)
    {
        int x, y, k;
        scanf("%d%d%d", &k, &x, &y);
        if(k == 0)  printf("%d\n", query(y) - query(x - 1));
        else    add(x, y);        
    }
    return 0;
}

标签:std,10,树状,int,数组,include
From: https://www.cnblogs.com/cxy8/p/17240896.html

相关文章