Cod sursa(job #2552667)

Utilizator popashtefan10Popa Stefan popashtefan10 Data 21 februarie 2020 09:04:11
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <iostream>
#include <cstdio>
#define NMAX 15000

using namespace std;

int n, m;
int aib[NMAX + 5];

int lsb(int x) {
  return (x ^ (x - 1)) & x;
}

void update(int x, int poz) {
  for(int i = poz; i <= n; i += lsb(i))
    aib[i] += x;
}

int query(int poz) {
  if(!poz)
    return 0;

  int s = 0;

  for(int i = poz; i >= 1; i -= lsb(i))
    s += aib[i];
  return s;
}

int main() {
  freopen("datorii.in", "r", stdin);
  freopen("datorii.out", "w", stdout);
  int x, y, z;

  scanf("%d %d", &n, &m);
  for(int i = 1; i <= n; i++) {
    scanf("%d", &x);
    update(x, i);
  }

  for(int i = 1; i <= m; i++) {
    scanf("%d %d %d", &z, &x, &y);
    if(!z)
      update(-y, x);
    else
      printf("%d\n", query(y) - query(x - 1));
  }

  return 0;
}