Cod sursa(job #2086096)

Utilizator RaduhhRadu Flocea Raduhh Data 11 decembrie 2017 13:42:47
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include<fstream>
using namespace std;
ifstream in("datorii.in");
ofstream out("datorii.out");
int n,m,ARB[32768];
void update(int nod, int st, int dr, int poz, int val)
{
    if(st == dr)
        ARB[nod] += val;
    else
    {
        int m = (st + dr)/2;
        if(poz <= m)
            update(2 * nod, st, m, poz, val);
        else update(1 + 2 * nod, m+1, dr, poz, val);
        ARB[nod] = ARB[2 * nod] + ARB[1 + 2 * nod];
    }
}
int query(int nod, int st, int dr, int a, int b, int &sol)
{
    if(a <= st and dr <= b) sol += ARB[nod];
    else
    {
        int m = (st + dr)/2;
        if(a <= m)
            query(2 * nod, st, m, a, b, sol);
        if(m < b)
            query(1 + 2 * nod, m + 1, dr, a, b, sol);
    }
}
int main()
{
    in >> n >> m;
    for(int x, i=1; i <= n; i++)
    {
        in>>x;
        update(1,1,n,i,x);
    }
    int t, a, b;
    while(m--)
    {
        in >> t >> a >> b;
        if(t)
        {
            int sol = 0;
            query(1, 1, n, a, b, sol);
            out << sol << '\n';
        }
        else update(1, 1, n, a, -b);
    }
    out.close();
    return 0;
}