Cod sursa(job #2439838)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 16 iulie 2019 22:40:29
Problema Datorii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

ifstream is("datorii.in");
ofstream os("datorii.out");

vector<int> tree;

int query(const int left, const int right, const int a, const int b, const int node) {
  if (a >= left && b <= right) {
    return tree[node];
  }
  const int md = a + ((b - a) >> 1);
  int sum = 0;
  if (left <= md)
    sum = query(left, right, a, md, 2 * node);
  if (md < right)
    sum += query(left, right, md + 1, b, 2 * node + 1);
  return sum;
}

int main() {
  int n, m, N;
  is >> n >> m;
  for (N = 1; N < n; N <<= 1);
  tree = vector<int>(2 * N);
  for (int i = 0; i < n; ++i) {
    is >> tree[N + i];
  }
  for (int i = N - 1; i; --i) {
    tree[i] = tree[2 * i] + tree[2 * i + 1];
  }

  int type, x, y;
  while (m--) {
    is >> type >> x >> y;
    if (type == 0) {
      for (int i = N + x - 1; i; i /= 2) {
        tree[i] -= y;
      }
    } else {
      os << query(x, y, 1, n, 1) << "\n";
    }
  }

  is.close();
  os.close();
  return 0;
}