Cod sursa(job #3367607)

Utilizator cezarica23cezar tambozi cezarica23 Data 8 octombrie 2026 21:18:59
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <bits/stdc++.h>

using namespace std;

int v[15005], aint[30005];

void build(int l, int r, int x)
{
    if (l == r)
    {
        aint[x] = v[l];
        return;
    }
    int mid = (l + r) / 2;
    build(l, mid, 2 * x);
    build(mid + 1, r, 2 * x + 1);
    aint[x] = aint[2 * x] + aint[2 * x + 1];
}

void update(int l, int r, int x, int poz, int val)
{
    if (l == r)
    {
        aint[x] -= val;
        return;
    }
    int mid = (l + r) / 2;
    if (poz <= mid)
    {
        update(l, mid, 2 * x, poz, val);
    }
    else
    {
        update(mid + 1, r, 2 * x + 1, poz, val);
    }
    aint[x] = aint[2 * x] + aint[2 * x + 1];
}

int query(int l, int r, int x, int liml, int limr)
{
    if (l > limr || r < liml)
    {
        return 0;
    }
    if (liml <= l && limr >= r)
    {
        return aint[x];
    }
    int mid = (l + r) / 2;
    return query(l, mid, 2 * x, liml, limr) + query(mid + 1, r, 2 * x + 1, liml, limr);
}

int main() 
{
    ifstream cin("datorii.in");
    ofstream cout("datorii.out");
	int n, m; cin >> n >> m;
    for (int i = 1; i <= n; i ++)
    {
        cin >> v[i];
    }
    build(1, n, 1);
    while (m --)
    {
        int c, a, b; cin >> c >> a >> b;
        if (c == 0)
        {
            update(1, n, 1, a, b);
        }
        else if (c == 1)
        {
            cout << query(1, n, 1, a, b) << '\n';
        }
    }
}