Cod sursa(job #3183386)

Utilizator andreea_chivuAndreea Chivu andreea_chivu Data 11 decembrie 2023 18:36:26
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <iostream>
#include <fstream>

using namespace std;

const int NMAX = 15001;

int aib[NMAX];
int n;
void adauga(int val, int poz, int sign){
    while(poz <= n){
        aib[poz] += val * sign;
        poz += poz&(-poz);
    }
}

int suma(int poz){
    int sum = 0;
    while(poz != 0){
        sum += aib[poz];
        poz -= poz&(-poz);
    }
    return sum;
}


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

    int m;
    fin >> n >> m;

    for(int i = 1; i <= n; i++){
        int x;
        fin >> x;
        adauga(x, i, 1);
    }

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

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