Cod sursa(job #2765310)

Utilizator mihai_popaPopa Mihai-Razvan mihai_popa Data 26 iulie 2021 12:39:33
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 1e5 + 5;

int n, m, v[NMAX], a[4 * NMAX], q, p, val, l, r;

void build (int nod, int L, int R)
{
    if(L == R)
    {
        a[nod] = v[L];

        return;
    }

    int mij = ((L + R) >> 1);

    build (2 * nod, L, mij);
    build (2 * nod + 1, mij + 1, R);

    a[nod] = a[2 * nod] + a[2 * nod + 1];
}

void update (int nod, int L, int R, int val, int pos)
{
    int mij = (L + R) >> 1;

    if(L == R)
    {
        a[nod] -= val;

        return;
    }

    if(pos <= mij)
        update(2 * nod, L, mij, val, pos);
    else
        update(2 * nod + 1, mij + 1, R, val, pos);

    a[nod] = a[2 * nod] + a[2 * nod + 1];
}

int querry (int nod, int L, int R, int ql, int qr)
{
    if(ql <= L && R <= qr)
        return a[nod];

    int mij = (L + R) >> 1;
    int p_Left = 0, p_Right = 0;

    if(ql <= mij)
        p_Left = querry(2 * nod, L, mij, ql, qr);

    if(qr > mij)
        p_Right = querry(2 * nod + 1, mij + 1, R, ql, qr);

    return p_Left + p_Right;
}

int main()
{
    fin >> n >> m;

    for(int i = 1; i <= n; i++)
        fin >> v[i];

    build(1, 1, n);

    for(int i = 1; i <= m; i++)
    {
        fin >> q;

        if(q == 1)
        {
            fin >> l >> r;
            fout << querry(1, 1, n, l, r) << '\n';
        }
        else
        {
            fin >> p >> val;
            update(1, 1, n, val, p);
        }
    }

    return 0;
}