A simple integer array tree problem [] [] and maintenance prefix

Topic Portal

Fenwick tree can maintain the prefix and
district-level differential update array can be thought of
a single point of inquiry normal query

#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+100;
typedef long long ll;
//树状数组维护b的前缀和
ll a[N],b[N];
int n,m;
ll lowbit(ll x)
{
    return x&-x;
}

void add(ll x,ll y)
{
    for(;x<=n;x+=lowbit(x))
    {
        b[x]+=y;
    }
}
ll ask(ll x)
{
    ll res=0;
    for(;x;x-=lowbit(x))
    {
        res+=b[x];
    }
    return res;
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    while(m--)
    {
        //cout<<m<<endl;
        char op;
        cin>>op;
        if(op=='Q'){
            int pos;
            cin>>pos;

            cout<<ask(pos)+a[pos]<<endl;
        }else{
            int l,r,d;
            cin>>l>>r>>d;
            add(l,d);
            add(r+1,-d);
        }

    }
    return 0;
}

Published 152 original articles · won praise 4 · Views 3884

Guess you like

Origin blog.csdn.net/qq_43716912/article/details/100173495