Cod sursa(job #1105696)

Utilizator danny794Dan Danaila danny794 Data 11 februarie 2014 23:41:42
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <cstdio>

const int NMAX = 15005;

using namespace std;

int N, M, array[NMAX];

void add(int pos, int x) {
  int aux = 1;
  array[pos] -= x;
  while(true) {
    while( !(aux & pos) )
      aux <<= 1;
    if (aux + pos <= N) {
      pos += aux;
      array[pos] -= x;
    } else
      break;
  }
}

int getSum(int pos, int sum) {
  if( !pos )
    return sum;

  sum += array[pos];
  int aux = 1;
  while( !(aux & pos) )
    aux <<= 1;
  pos ^= aux;
  return getSum(pos, sum);
}

void read() {
  freopen("datorii.in", "r", stdin);
  freopen("datorii.out", "w", stdout);
  scanf("%d %d", &N, &M);
  int x;
  for(int i = 1; i <= N; i++) {
    scanf("%d", &x);
    add(i, -x);
  }
}

void solve() {
  int t, a, b;
  for(int i = 1; i <= M; i++) {
    scanf("%d%d%d", &t, &a, &b);
    if (t)
      printf("%i\n", getSum(b, 0) - getSum(a - 1 , 0));
    else
      add(a, b);
  }
}

int main() {
  read();
  solve();
  return 0;
}