Cod sursa(job #3286173)

Utilizator alexiksmAgasanov Alecsei alexiksm Data 13 martie 2025 19:39:08
Problema Datorii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <deque>
#include <queue>
#include <map>
#include <cmath>
#include <limits.h>
#include <iomanip>

using namespace std;

const string nume = "datorii";
ifstream fin(nume + ".in");
ofstream fout(nume + ".out");

const int nmax = 15005;

int aib[nmax];

void update(int ok, int loc, int val, int nod = 1, int st = 1, int dr = nmax) { //ok = 1 adaugare, ok = 0 scadere
	if (st == dr)
		return (void)(ok == 1? aib[nod] = val : aib[nod] -= val);

	int mij = st + dr >> 1;
	if (loc <= mij) update(ok, loc, val, 2*nod, st, mij);
	else update(ok, loc, val, 2*nod + 1, mij + 1, dr);

	aib[nod] = aib[2 * nod] + aib[2 * nod + 1];
}

int query(int inc, int sf, int nod = 1, int st = 1, int dr = nmax) {
	if (st >= inc && dr <= sf)
		return aib[nod];

	int s = 0;
	int mij = st + dr >> 1;
	if (inc <= mij) s += query(inc, sf, 2 * nod, st, mij);
	if (sf > mij) s += query(inc, sf, 2 * nod + 1, mij + 1, dr);

	return s;
}

int main() {

	int n, q;

	fin >> n >> q;

	for (int i = 1; i <= n; i++) {
		int x;
		fin >> x;
		update(1, i, x);
	}

	while (q--) {
		int cerr;
		fin >> cerr;
		if (cerr) {
			int x, y;
			fin >> x >> y;
			fout << query(x, y) << '\n';
		}
		else {
			int loc, val;
			fin >> loc >> val;
			update(0, loc, val);
		}
	}

	return 0;
}