Cod sursa(job #3030833)

Utilizator Asgari_ArminArmin Asgari Asgari_Armin Data 17 martie 2023 22:03:07
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <bits/stdc++.h>

using namespace std;

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

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

void build(int node, int from, int to) {
  if (from == to) {
    aint[node] = v[from];
    return ;
  }
  int mid = (from + to) >> 1;
  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 x, int y) {
  if (from == to) {
    aint[node] -= y;
    return ;
  }

  int mid = (from + to) >> 1;
  if (x <= mid)
    update(2 * node, from, mid, x, y);
  else
    update(2 * node + 1, mid + 1, to, x, y);
  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) >> 1;
  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, a, x, y;
  fin >> n >> m;
  for( i = 1; i <= n; ++i )
    fin >> v[i];
  build(1, 1, n);
  while( m-- ){
    fin >> a >> x >> y;
    if( a == 0 )
      update(1, 1, n, x, y);
    else
      fout << query(1, 1, n, x, y) << "\n";
  }
  return 0;
}