Cod sursa(job #3249751)

Utilizator dariustgameTimar Darius dariustgame Data 17 octombrie 2024 17:14:49
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <fstream>

using namespace std;

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

int n, m, a[15005], ai[100005];

void build(int nod, int st, int dr)
{
	if (st == dr)
	{
		ai[nod] = a[st];
	}
	else
	{
		int mij = (st + dr) / 2;
		build(2 * nod, st, mij);
		build(2 * nod + 1, mij + 1, dr);
		ai[nod] = ai[2 * nod] + ai[2 * nod + 1];
	}
}

void update(int nod, int st, int dr, int p, int x)
{
	if (st == dr)
	{
		ai[nod] = x;
		return;
	}
	int mij = (st + dr) / 2;
	if (p <= mij)
	{
		update(2 * nod, st, mij, p, x);
	}
	else
	{
		update(2 * nod + 1, mij + 1, dr, p, x);
	}
	ai[nod] = ai[2 * nod] + ai[2 * nod + 1];
}

int sol = 0;
void query(int nod, int st, int dr, int a, int b)
{
	if (a <= st && b >= dr)
	{
		sol += ai[nod];
		return;
	}
	int mij = (st + dr) / 2;
	if (a <= mij)
	{
		query(2 * nod, st, mij, a, b);
	}
	if (b >= mij + 1)
	{
		query(2 * nod + 1, mij + 1, dr, a, b);
	}
}

int main()
{
	fin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		fin >> a[i];
	}
	build(1, 1, n);
	int p, x, y;
	for (int i = 0; i < m; i++)
	{
		fin >> p >> x >> y;
		if (p == 0)
		{
			a[x] -= y;
			update(1, 1, n, x, a[x]);
		}
		else
		{
			sol = 0;
			query(1, 1, n, x, y);
			fout << sol << '\n';
		}
	}
}