Cod sursa(job #2060350)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 8 noiembrie 2017 09:16:02
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream in ("datorii.in");
ofstream out ("datorii.out");
int const nmax = 15000;
long long aib[5 + 4 * nmax];
int n , k = 0;
void update(int p){
  if(0 < p){
    aib[p] = aib[p * 2] + aib[p * 2 + 1];
    update(p / 2);
  }
}
long long query(int from ,int to){
  long long result = 0;
  if(from < to){
    if(from % 2 == 1){
      result += aib[from];
      from++;
    }
    if(to % 2 == 0){
      result += aib[to];
      to--;
    }
    result += query(from / 2,to / 2);
  } else if(from == to){
    result = aib[from];
  } else
    result = 0;
  return result;
}
int main()
{
  int m;
  in>>n>>m;
  while((1 << k) <= n){
    k++;
  }
  int ni = (1 << k) - 1;
  for(int i = 1 ; i <= n ;i++){
    int a;
    in>>a;
    aib[ni + i] = a;
    update((ni + i) / 2);
  }

  for(int i = 1 ; i <= m ;i++){
    int op , a , b ;
    in>>op>>a>>b;
    if(op == 0){
      aib[ni + a] -= b;
      update((ni + a) / 2);
    } else{
      out<<query(ni + a , ni + b)<<'\n';
    }
  }
  return 0;
}