树状数组 P3368【区间更新 单点查询】

题目

https://www.luogu.com.cn/problem/P3368

题目分析

是区间更新 单点查询,使用树状数组

代码

#include<iostream>
#include<cstdio>
using namespace std;
long long a[500001], c[500001];
int n, m;
int lowbit(int x)
{
    return x&(-x);
}
void update(int i, int k)
{
    while (i <= n)
    {
        c[i] += k;
        i += lowbit(i);
    }
}
long long getsum(int i)
{
    long long res = 0;
    while (i > 0)
    {
        res += c[i];
        i -= lowbit(i);
    }
    return res;
}
int main()
{
    int aa, bb, cc,dd;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
        update(i, a[i]-a[i-1]);
    }
    for (int i = 0; i < m; i++)
    {
        scanf("%d%d", &aa,&bb);
        if (aa == 1)
        {
            scanf("%d%d",&cc,&dd);
            update(bb, dd);
            update(cc+1, -dd);
        }
        else
            printf("%lld
", getsum(bb));
    }

}
原文地址:https://www.cnblogs.com/Jason66661010/p/12827681.html