Cod sursa(job #1631711)

Utilizator pickleVictor Andrei pickle Data 5 martie 2016 18:18:13
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <iostream>
#include <fstream>

using namespace std;
const int Nmax = 15333;

int A[4*Nmax];
int N, M;
int first, last, pos, val, sum, x, a, b;

void update(int nod, int l, int r);
void query(int nod, int l, int r);


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

  fin >> N >> M;
  for(int i = 1; i <= N; i++) {
    fin >> x;
    pos = i, val = x;
    update(1, 1, N);
  }

  while(M--) {
    fin >> x >> a >> b;
    if (x == 0) {
      pos = a, val = -b;
      update(1, 1, N);
    } else if (x == 1) {
      first = a, last = b, sum = 0;
      query(1, 1, N);
      fout << sum << '\n';
    }
  }

  return 0;
}

void update(int nod, int l, int r) {
  A[nod] += val;
  if (l == r) { // l == r == pos
    //A[nod] += val;
    return;
  }

  int m = (l + r)/2;
  if (pos <= m) update(2*nod, l, m);
  if (pos > m)  update(2*nod+1, m+1, r);

  //A[nod] = A[2*nod] + A[2*nod+1];
}

void query(int nod, int l, int r) {
  if (first <= l && r <= last) {
    sum += A[nod];
    return;
  }

  int m = (l + r)/2;
  if (first <= m) query(2*nod, l, m);
  if (last > m)   query(2*nod + 1, m+1, r);
}