Cod sursa(job #1997992)

Utilizator trifangrobertRobert Trifan trifangrobert Data 6 iulie 2017 01:12:41
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.12 kb
#include <fstream>
#define DIMENSION 15010

using namespace std;

int n, m;
int v[DIMENSION], aint[4 * DIMENSION];
int answer;

//void DisplayVector(bool x)
//{
//	if (x == false)
//		for (int i = 1;i <= n;i++)
//			cout << v[i] << " ";
//	else
//		for (int i = 1;i <= 4 * n;i++)
//			cout << aint[i] << " ";
//	cout << "\n";
//}

inline int LeftSon(const int &x)
{
	return 2 * x;
}

inline int RightSon(const int &x)
{
	return 2 * x + 1;
}

void Build(int nod, int left, int right)
{
	if (left == right)
	{
		aint[nod] = v[left];
		return;
	}
	int mij = (left + right) / 2;
	Build(LeftSon(nod), left, mij);
	Build(RightSon(nod), mij + 1, right);
	aint[nod] = aint[LeftSon(nod)] + aint[RightSon(nod)];
}

void Update(int nod, int left, int right, const int &poz, const int &val)
{
	if (left == right)
	{
		aint[nod] = aint[nod] - val;
		v[poz] = aint[nod] - val;
		return;
	}
	int mij = (left + right) / 2;
	if (poz <= mij)
		Update(LeftSon(nod), left, mij, poz, val);
	else
		Update(RightSon(nod), mij + 1, right, poz, val);
	aint[nod] = aint[LeftSon(nod)] + aint[RightSon(nod)];
}

void Query(int nod, int left, int right, const int &LeftQuery, const int &RightQuery)
{
	if (LeftQuery <= left && right <= RightQuery)
	{
		answer += aint[nod];
		return;
	}
	int mij = (left + right) / 2;
	if (LeftQuery <= mij)
		Query(LeftSon(nod), left, mij, LeftQuery, RightQuery);
	if (RightQuery >= mij + 1)
		Query(RightSon(nod), mij + 1, right, LeftQuery, RightQuery);
	// aici e o problema pentru exemplul: 1 1 4
	// cand o sa ajunga left == 4 si right == 4 atunci o sa crape :)
}

void Read_Display()
{
	ifstream f("datorii.in");
	ofstream g("datorii.out");
	f >> n >> m;
	for (int i = 1;i <= n;i++)
		f >> v[i];
	Build(1,1,n);
	//DisplayVector(1);
	for (int i = 1;i <= m;i++)
	{
		int x, a, b;
		f >> x >> a >> b;
		switch (x)
		{
			case 0:
				Update(1, 1, n, a, b);
				//DisplayVector(1);
				break;
			case 1:
				answer = 0;
				Query(1, 1, n, a, b);
				g << answer <<"\n";
				break;
		}
	}
	f.close();
	g.close();

}

int main()
{
	Read_Display();
	return 0;
}