Cod sursa(job #2713561)

Utilizator StefanSanStanescu Stefan StefanSan Data 28 februarie 2021 11:29:49
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include      <iostream>
#include      <fstream>

using namespace std;

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

int n, m, v[15001], aib[15001];

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

void add(int x, int quantity) {
	for (int i = x; i <= n; i += zero(i)) aib[i] += quantity;
}

int query(int a) {
	int rez = 0;
	for (int i = a; i > 0; i -= zero(i)) rez += aib[i];
	return rez;
}

int main() {

	ios::sync_with_stdio(false);
	in.tie(NULL), out.tie(NULL);

	in >> n >> m;

	for (int i = 1; i <= n; i++) {
		in >> v[i];
		for (int j = i - zero(i) + 1; j <= i; j++) aib[i] += v[j];
	}
	while (m--) {
		int op, a, b;
		in >> op >> a >> b;
		if (op == 0) {
			add(a, -b);
		}
		if (op == 1) {
			out << query(b) - query(a - 1) << '\n';
		}
	}

	return 0;
}