Cod sursa(job #2920204)

Utilizator Alex_HossuHossu Alexandru Alex_Hossu Data 22 august 2022 18:34:49
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>

#define MAXN 100005

long long int aib[MAXN];

long long int query(int pos) {
  long long int ans = 0;
  while (pos) {
    ans += aib[pos];
    pos -= pos & (-pos);
  }
  return ans;
}

void update(int pos, int val, const int &nrn) {
  while (pos <= nrn) {
    aib[pos] += val;
    pos += pos & (-pos);
  }
}

inline long long int interval(int pos1, int pos2) {
  return query(pos2) - query(pos1 - 1);
}

int main() {
  std::ifstream fin("datorii.in");
  std::ofstream fout("datorii.out");
  long long int nrn, nrm, num, type, pos;
  int pos1, pos2;

  fin >> nrn >> nrm;
  for (int _ = 1; _ <= nrn; _++) {
    fin >> num;
    update(_, num, nrn);
  }

  for (int _ = 0; _ < nrm; _++) {
    fin >> type;
    if (type == 0) {
      // Update
      fin >> pos >> num;
      update(pos, -num, nrn);
    } else if (type == 1) {
      // Query 1
      fin >> pos1 >> pos2;
      fout << interval(pos1, pos2) << '\n';
    }
  }

  return 0;
}