Cod sursa(job #3215846)

Utilizator Asgari_ArminArmin Asgari Asgari_Armin Data 15 martie 2024 13:34:34
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 15000;
int aint[4 * NMAX + 2];
int arr[NMAX + 2];

void build(int node, int from, int to) {
  if (from == to) {
    aint[node] = arr[from];
    return ;
  }

  int mid = (from + to) / 2;
  build(2 * node, from, mid);
  build(2 * node + 1, mid + 1, to);

  aint[node] = aint[2 * node] + aint[2 * node + 1];
}

void update(int node, int from, int to, int pos, int x) {
  if (from == to) {
    aint[node] -= x;
    return ;
  }

  int mid = (from + to) / 2;

  if (pos <= mid)
    update(2 * node, from, mid, pos, x);
  else
    update(2 * node + 1, mid + 1, to, pos, x);

  aint[node] = aint[2 * node] + aint[2 * node + 1];
}

int query(int node, int from, int to, int x, int y) {
  if (from == x && y == to)
    return aint[node];

  int mid = (from + to) / 2;

  if (y <= mid)
    return query(2 * node, from, mid, x, y);
  if (x > mid)
    return query(2 * node + 1, mid + 1, to, x, y);
  return query(2 * node, from, mid, x, mid) + query(2 * node + 1, mid + 1, to, mid + 1, y);
}

int main() {
  int n, m, i;

  fin >> n >> m;
  for (i = 1; i <= n; ++i)
    fin >> arr[i];

  build(1, 1, n);
  for (i = 1; i <= m; ++i) {
    int cer, x, y;
    fin >> cer >> x >> y;

    if (cer == 0)
      update(1, 1, n, x, y);
    else
      fout << query(1, 1, n, x, y) << "\n";
  }
  return 0;
}