Cod sursa(job #2903066)

Utilizator andriciucandreeaAndriciuc Andreea andriciucandreea Data 17 mai 2022 01:56:56
Problema Datorii Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int nodes[60100], v[15005];

void construct(int pos, int left, int right)
{
    if(left == right)
    {
        nodes[pos] = v[left];
        return;
    }
    construct(pos*2, left, (left+right)/2);
    construct(pos*2+1, (left+right)/2+1, right);
    nodes[pos] = nodes[pos*2]+nodes[pos*2+1];
}

void update(int pos, int left, int right, int i, int x)
{
    if(i < left || i > right)
        return;
    if(left == right)
    {
        nodes[pos] -= x;
        return;
    }
    int mid = (left+right)/2;
    if(i <= mid)
        update(pos*2, left, mid, i, x);
    else
        update(pos*2+1, mid+1, right, i, x);
    nodes[pos] = nodes[pos*2]+nodes[pos*2+1];
}

int query(int pos, int left, int right, int i, int j)
{
    if(right < i || left > j)
        return 0;
    if(i <= left && j >= right)
        return nodes[pos];
    int mid = (left+right)/2;
    return query(pos*2, left, mid, i, j) + query(pos*2+1, mid+1, right, i, j);
}

int main()
{
    int n, m, i, op, x, pos;
    fin>>n>>m;
    for(i = 1; i <= n; i++)
        fin>>v[i];
    construct(1, 1, n);
    while(m)
    {
        fin>>op>>pos>>x;
        if(op == 0)
            update(1, 1, n, pos, x);
        if(op == 1)
            fout<<query(1, 1, n, pos, x)<<'\n';
        m--;
    }
    return 0;

}