Cod sursa(job #3301521)

Utilizator Mocanu_Tudor_CristianMocanu Tudor Cristian Mocanu_Tudor_Cristian Data 27 iunie 2025 14:32:30
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 15000;
int n, m, arbint[3 * nmax], rasp, val, poz;

void update(int nod, int st, int dr, int zi, int dif)
{
    if(st == dr)
    {
        arbint[nod] += dif;
        return;
    }
    int mij = (st + dr) / 2;
    if(zi <= mij)
        update(2 * nod, st, mij, zi, dif);
    else
        update(2 * nod + 1, mij + 1, dr, zi, dif);
    arbint[nod] = arbint[2 * nod] + arbint[2 * nod + 1];
}

void query(int nod, int x, int y, int st, int dr)
{
    if(x <= st && y >= dr)
    {
        rasp += arbint[nod];
        return;
    }
    int mij = (st + dr) / 2;
    if(x <= mij)
        query(2 * nod, x, y, st, mij);
    if(y > mij)
        query(2 * nod + 1, x, y, mij + 1, dr);
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        int x;
        fin >> x;
        update(1, 1, n, i, x);
    }
    for(int i = 1; i <= m; i++)
    {
        int c, a, b;
        fin >> c;
        if(c == 1)
        {
            fin >> a >> b;
            rasp = 0;
            query(1, a, b, 1, n);
            fout << rasp << '\n';
            rasp = 0;
        }
        else
        {
            fin >> poz >> val;
            update(1, 1, n, poz, -val);
        }
    }

    return 0;
}