Cod sursa(job #1762438)

Utilizator mvcl3Marian Iacob mvcl3 Data 23 septembrie 2016 15:27:30
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <fstream>

using namespace std;

const int lInt = 15000 * 4 + 200;

class IntervalTree
{
	int n, m;

	int ArbInt[lInt];

	void UpdateTree(int node, pair < int, int > extr, int idx, int money)
	{
		if(extr.first == extr.second)	{

			ArbInt[node] += money;
			return;
		}

		int mid = (extr.first + extr.second) >> 1;
		
		if(idx <= mid)
			UpdateTree(2 * node, make_pair(extr.first, mid), idx, money);
		else
			UpdateTree(2 * node + 1, make_pair(mid + 1, extr.second), idx, money);

		ArbInt[node] = ArbInt[2 * node] + ArbInt[2 * node + 1];
	}

	void Querry(int node, pair< int, int > extr, pair < int, int > curInt, int *solution)
	{
		if(curInt.first <= extr.first && extr.second <= curInt.second)	{

			*solution += ArbInt[node];
			return;
		}

		int mid = (extr.first + extr.second) >> 1;

		if(curInt.first <= mid)	
			Querry(2 * node, make_pair(extr.first, mid), curInt, solution);
		if(mid < curInt.second)
			Querry(2 * node + 1, make_pair(mid + 1, extr.second), curInt, solution);
	}

public :

	IntervalTree()
	{
		for(int i = 1; i < lInt; ++i)
			ArbInt[i] = 0;
	}

	void Compute()
	{
		ifstream inputFile("datorii.in");
		ofstream outputFile("datorii.out");

		inputFile >> n >> m;

		for(int x, i = 1; i <= n; ++i)
		{
			inputFile >> x;
			UpdateTree(1, make_pair( 1, n ), i, x);
		}

		for(int type, x, y, i = 1; i <= m; ++i)
		{
			inputFile >> type >> x >> y;

			if(type == 0)	{

				UpdateTree(1, make_pair(1, n), x, -y);
				continue;
			}

			int solution = 0;
			Querry(1, make_pair(1, n), make_pair(x, y), &solution);

			outputFile << solution << '\n';
		}
	}
};

int main()
{
	IntervalTree solver;
	solver.Compute();

	return 0;
}