Cod sursa(job #2182056)

Utilizator alexandru822Bosinta Alexandru alexandru822 Data 22 martie 2018 07:53:14
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>
using namespace std;

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

const int N_MAX = 15000;
int N, M;
int SumTree[2*N_MAX];

void update(int node, int l, int r, int pos, int val)
{
    if(l == r)
    {
        SumTree[node] += val;
        return;
    }

    int mid = (l+r)/2;
    if(pos <= mid)
        update(2*node, l, mid, pos, val);
    else
        update(2*node+1, mid+1, r, pos, val);

    SumTree[node] = SumTree[2*node] + SumTree[2*node+1];
}

int query(int node, int nodeL, int nodeR, int l, int r)
{
    if(l <= nodeL && nodeR <= r)
        return SumTree[node];

    int mid = (nodeL + nodeR)/2;
    if(r <= mid)
        return query(2*node, nodeL, mid, l, r);
    if(mid+1 <= l)
        return query(2*node+1, mid+1, nodeR, l, r);
    return query(2*node, nodeL, mid, l, mid) + query(2*node+1, mid+1, nodeR, mid+1, r);
}

int main()
{
    in >> N >> M;
    for(int i = 1; i <= N; i++)
    {
        int x;
        in >> x;
        update(1, 1, N, i, x);
    }
    for(int i = 1; i <= M; i++)
    {
        bool c;
        int x, y;
        in >> c >> x >> y;
        if(c)
            out << query(1, 1, N, x, y) << "\n";
        else
            update(1, 1, N, x, -y);
    }
    return 0;
}