Cod sursa(job #2590572)

Utilizator AndreeaGherghescuAndreea Gherghescu AndreeaGherghescu Data 28 martie 2020 13:51:26
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int N=15001;
const int L=13;

int n,aib[N];

void actualizare (int p, int val)
{
    while (p<=n)
    {
        aib[p]-=val;
        p+=(p&(-p));
    }
}
void construct (int p, int val)
{
    while (p<=n)
    {
        aib[p]+=val;
        p+=(p&(-p));
    }
}
int interogare (int p)
{
    int s=0;
    while (p!=0)
    {
        s+=aib[p];
        p-=(p&(-p));
    }
    return s;
}
int main()
{
    int cod,x,y,m;
    in>>n>>m;
    for (int i=1;i<=n;i++)
    {
        in>>x;
        construct (i,x);
    }
    for (int i=1;i<=m;i++)
    {
        in>>cod>>x>>y;
        if (cod==0)
        {
            actualizare (x,y);
        }
        else
        {
            out<<interogare(y)-interogare(x-1)<<'\n';
        }
    }
    return 0;
}