Cod sursa(job #3338889)

Utilizator bulatalexandrinaAlexandrina Bulat bulatalexandrina Data 5 februarie 2026 13:04:04
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <bits/stdc++.h>
using namespace std;

long long A[15001];        
long long arb[60001];     

void construieste(int st, int dr, int nod) {
    if (st == dr) {
        arb[nod] = A[st];
        return; }

    int mij = (st + dr) / 2;
    construieste(st, mij, 2 * nod);
    construieste(mij + 1, dr, 2 * nod + 1);

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

void modifica(int st, int dr, int nod, int poz, long long val) {
    if (st == dr) {
        A[poz] = val;
        arb[nod] = val;
        return; }

    int mij = (st + dr) / 2;
    if (poz <= mij)
        modifica(st, mij, 2*nod, poz, val);
    else
        modifica(mij + 1, dr, 2*nod + 1, poz, val);

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

long long interogare(int st, int dr, int qs, int qd, int nod) {
    if (qd < st || qs > dr)
        return 0;

    if (qs <= st && dr <= qd)
        return arb[nod];

    int mij = (st + dr) / 2;
    return interogare(st, mij, qs, qd, 2 * nod) +
           interogare(mij + 1, dr, qs, qd, 2 * nod + 1); }

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

    int N, M;
    fin >> N >> M;

    for (int i = 1; i <= N; i++)
        fin >> A[i];

    construieste(1, N, 1);

    while (M--) {
        int tip, x, y;
        fin >> tip >> x >> y;

        if (tip == 0) {
            modifica(1, N, 1, x, A[x] - y); }
        else {
            fout << interogare(1, N, x, y, 1) << '\n'; } }
    return 0;
}