Cod sursa(job #2843717)

Utilizator Luca_Miscocilucainfoarena Luca_Miscoci Data 2 februarie 2022 20:13:52
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>
#define NMAX 15000

using namespace std;

int aib[NMAX + 1];
int val, n;

inline int lsb (int x){
  return x&(-x);
}

void update (int poz, int val){
  while (poz <= n){
    aib[poz] += val;
    poz += lsb(poz);
  }
}

int query (int poz){
  int sol = 0;
  while (poz > 0){
    sol += aib[poz];
    poz -= lsb(poz);
  }
  return sol;
}
int main(){

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

  int q;

  fin >> n >> q;
  for (int i = 1; i <= n; i++){
    fin >> val;
    update(i, val);
  }

  int type, a, b;
  for (int i = 1; i <= q; i++){
    fin >> type >> a >> b;
    if (type == 0)
      update (a, -b);
    else
      fout << query (b) - query (a - 1) << "\n";
  }
  return 0;
}