Cod sursa(job #2013765)

Utilizator Andrei_CotorAndrei Cotor Andrei_Cotor Data 22 august 2017 12:53:31
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include<fstream>
using namespace std;
ifstream fi("datorii.in");
ofstream fo("datorii.out");
int F[15001],n,m,i,x,tip,p,q;

void update(int poz, int val)
{
    while(poz<=n)
    {
        F[poz]+=val;
        poz=poz+(poz&(poz^(poz-1)));
    }
}

int f(int poz)
{
    int rez=0;
    while(poz>0)
    {
        rez+=F[poz];
        poz=poz-(poz&(poz^(poz-1)));
    }
    return rez;
}

int query(int st, int dr)
{
    return f(dr)-f(st-1);
}

int main()
{
    fi>>n>>m;
    for(i=1; i<=n; i++)
    {
        fi>>x;
        update(i,x);
    }
    for(i=1; i<=m; i++)
    {
        fi>>tip>>p>>q;
        if(tip==0)
        {
            update(p,-q);
        }
        else
        {
            fo<<query(p,q)<<"\n";
        }
    }
    fi.close();
    fo.close();
    return 0;
}