Cod sursa(job #3321762)

Utilizator calin06calin mihaescu calin06 Data 11 noiembrie 2025 11:52:13
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include<math.h>
#include <stack>

using namespace std;

// #define debug 1
// #if debug
// ifstream fin("input.txt");
// #else
// #define fin cin
// #endif

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

int n, m;

void build(int nod, int st, int dr, vector<int> &arb, vector<int> &a) {
    if (st == dr)
        arb[nod] = a[st];
    else {
        int mid = (st + dr) / 2;
        build(2 * nod, st, mid, arb, a);
        build(2 * nod + 1, mid + 1, dr, arb, a);
        arb[nod] = arb[2 * nod] + arb[2 * nod + 1];
    }
}

void update(int nod, int st, int dr, vector<int> &arb, int poz, int val) {
    if (st == dr)
        arb[nod] = max(0, arb[nod] - val);
    else {
        int mid = (st + dr) / 2;
        if (poz <= mid)
            update(2 * nod, st, mid, arb, poz, val);
        else
            update(2 * nod + 1, mid + 1, dr, arb, poz, val);

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

}

void query(int nod,int st,int dr,vector<int>&arb,int stInt,int drInt,int &sol) {
    if (stInt <= st && dr <= drInt)
        sol += arb[nod];
    else {
        int mid = (st + dr) / 2;
        if (stInt <= mid)
            query(2 * nod,st,mid,arb,stInt,drInt,sol);
        if (drInt > mid)
            query(2 * nod + 1,mid + 1,dr,arb,stInt,drInt,sol);
    }
}

int main() {
    fin >> n >> m;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
        fin >> a[i];
    vector<int> arb(4 * n + 10, 0);
    build(1, 1, n, arb, a);

    for (int i = 1;i <= m;i++) {
        int tip,x,y;
        fin>>tip>>x>>y;
        if (tip == 0) {
            update(1,1,n,arb,x,y);
        }
        else {
            int sol = 0;
            query(1,1,n,arb,x,y,sol);
            fout<<sol<<"\n";
        }

    }
}