Cod sursa(job #1556458)

Utilizator ThomasFMI Suditu Thomas Thomas Data 24 decembrie 2015 21:55:00
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <fstream>
using namespace std;

#define NMax 15005

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

int n,m;
int A[NMax];

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

int query(int poz)
{
    int sum=0;
    while(poz>0)
    {
        sum+=A[poz];
        poz-=(poz&(-poz));
    }
    return sum;
}

int main()
{
    int a,b,c,i;
    f>>n>>m;
    for(i=1;i<=n;++i)
    {
        f>>a;
        update(i,a);
    }
    while(m--)
    {
        f>>a>>b>>c;
        if(!a) update(b,-c);
        else g<<query(c)-query(b-1)<<"\n";
    }

    f.close();
    g.close();
    return 0;
}