Cod sursa(job #2398266)

Utilizator stoicaandreiStoica Marius-Andrei stoicaandrei Data 5 aprilie 2019 11:28:12
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.64 kb
#include <bits/stdc++.h>

#define up(x) (x&-x)
#define nmax 15005

using namespace std;

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

int n, m;
int a[nmax];

void add(int pos, int val)
{
  for (int i = pos; i <= n; i += up(i))
    a[i] += val;
}

int compute(int x)
{
  int sum = 0;
  for (int i = x; i > 0; i -= up(i))
    sum += a[i];
  return sum;
  
}

int main()
{
  fin >> n >> m;
  for (int i = 1; i <= n; i ++)
  {
    int x;
    fin >> x;
    add(i, x);
  }

  for (int i = 1; i <= m; i ++)
  {
    int a, b, c;
    fin >> a >> b >> c;

    if (a == 0)
      add(b, -c);
    else
      fout << compute(c) - compute(b-1) << "\n";
  }
}