Cod sursa(job #2642721)

Utilizator GiosinioGeorge Giosan Giosinio Data 16 august 2020 22:25:14
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <iostream>
#include <fstream>
#define DIM 15005

using namespace std;

ifstream f("datorii.in");
ofstream g("datorii.out");

int N,M,T[DIM];

void update(int ind, int key){
    while(ind <= N){
        T[ind] += key;
        int c = 0;
        while(!(ind & (1 << c) ))
            c++;
        ind += (1 << c);
        c++;
    }
}

int query(int poz){
    int sum = 0;
    while(poz > 0){
        sum += T[poz];
        int c = 0;
        while(!(poz & (1 << c) ))
            c++;
        poz -= (1 << c);
        c++;
    }
    return sum;
}

int main()
{
    f>>N>>M;
    int val;
    for(int i=1; i<=N; i++){
        f>>val;
        update(i,val);
    }
    int op,a,b;
    for(int i=1; i<=M; i++){
        f>>op>>a>>b;
        if(op == 0)
            update(a,-b);
        else
            g<<query(b) - query(a-1)<<"\n";
    }
}