Cod sursa(job #3177275)

Utilizator TeodorVTeodorV TeodorV Data 28 noiembrie 2023 20:18:34
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("datorii.in");
ofstream fout("datorii.out");

const int Nmax=15001;
long long aib[Nmax]; int n;

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

void addAIB(int pos, int x)
{
    while(pos<=n)
    {
        aib[pos]+=x;
        pos+=p2min(pos);
    }
}
long long getsumAIB(int pos)
{
    long long rez=0;
    while(pos>0)
    {
        rez+=aib[pos];
        pos-=p2min(pos);
    }
    return rez;
}

int main()
{
    int m,x;
    fin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        fin>>x;
        addAIB(i, x);
    }
    for(int i=1; i<=m; i++)
    {
        int t,a,b;
        fin>>t>>a>>b;
        if(t==0)
        {
            addAIB(a, -b);
        }
        else
        {
            fout<<getsumAIB(b)-getsumAIB(a-1)<<'\n';
        }
    }
    return 0;
}