Cod sursa(job #2968675)

Utilizator andreea_chivuAndreea Chivu andreea_chivu Data 21 ianuarie 2023 18:52:48
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <iostream>
#include <fstream>

using namespace std;

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

#define NMAX 15001

int t[4 * NMAX];

void constructie(int p, int st, int dr){
    if(st == dr){
        fin >> t[p];
        return;
    }
    int m = (st + dr) / 2;
    int fs = 2 * p;
    int fd = 2 * p + 1;
    constructie(fs, st, m);
    constructie(fd, m + 1, dr);
    t[p] = t[fs] + t[fd];
}

void actualizare(int p, int st, int dr, int poz, int val){
    if(st == dr){
        t[p]-=val;
        return;
    }
    int m = (st + dr) / 2;
    int fs = 2 * p;
    int fd = 2 * p + 1;
    if(poz <= m){
        actualizare(fs, st, m, poz, val);
    }else{
        actualizare(fd, m + 1, dr, poz, val);
    }
    t[p] = t[fs] + t[fd];
}

int interogare(int p, int st, int dr, int a, int b){
    if(a <= st && dr <= b){
        return t[p];
    }
    int m = (st + dr) / 2;
    int fs = 2 * p;
    int fd = 2 * p + 1;
    int rez_st = 0;
    int rez_dr = 0;
    if(a <= m){
        rez_st = interogare(fs, st, m, a, b);
    }
    if(b >= m + 1){
        rez_dr = interogare(fd, m + 1, dr, a, b);
    }
    return rez_st + rez_dr;
}



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

    constructie(1, 1, n);
    for(int i = 0; i < m; i++){
        int tip;
        fin >> tip;
        if(tip == 0){
            int t, v;
            fin >> t >> v;
            actualizare(1, 1, n, t, v);
        }else{
            int p, q;
            fin >> p >> q;
            fout << interogare(1, 1, n, p, q) << "\n";
        }
    }

    fin.close();
    fout.close();
    return 0;
}