Cod sursa(job #3206853)

Utilizator Raul_AArdelean Raul Raul_A Data 24 februarie 2024 12:11:08
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const string fn("datorii");

ifstream in(fn + ".in");
ofstream out(fn + ".out");

#define cin in
#define cout out

int n,q;

struct FenwickTree{
    vector<ll> aib;
    int n;

    void init(int _n)
    {
        n=_n;
        aib.resize(n+5,0);
    }

    void upd(int pos,int val)
    {
        for(;pos<=n;pos+=pos&-pos)
            aib[pos]+=val;
    }

    ll query(int pos)
    {
        ll sum=0;
        for(;pos>0;pos-=pos&-pos)
            sum+=aib[pos];
        return sum;
    }
}aib;

int main()
{
    int n,q;
    cin>>n>>q;
    aib.init(n);
    for(int i=1,x;i<=n;i++)
        cin>>x,aib.upd(i,x);
    
    while(q--)
    {
        int t,l,r;
        cin>>t>>l>>r;

        if(t==0)
        {
            aib.upd(l,-r);
        }
        else
        {
            cout<<aib.query(r)-aib.query(l-1)<<'\n';
        }
    }
    return 0;
}