Cod sursa(job #459655)

Utilizator darrenRares Buhai darren Data 30 mai 2010 16:07:44
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include<fstream>
using namespace std;

int n, m;
int aib[15001];

void update(int pos, int val)
{
	int c = 0;
	while (pos <= n)
	{
		aib[pos] -= val;
		while (!(pos & 1 << c))
			++c;
		pos += 1 << c;
		++c;
	}
}

int querry(int pos)
{
	int s = 0, c = 0;
	while (pos >= 1)
	{
		s += aib[pos];
		while (!(pos & 1 << c))
			++c;
		pos -= 1 << c;
		++c;
	}
	return s;
}

int main()
{
	ifstream fin("datorii.in");
	ofstream fout("datorii.out");
	fin >> n >> m;
	
	int aux;
	for (int i = 1; i <= n; ++i)
	{
		fin >> aux;
		update(i, -aux);
	}
	int val, p1, p2;
	for (int i = 1; i <= m; ++i)
	{
		fin >> aux;
		switch (aux)
		{
		case 0:
			fin >> p1 >> val;
			update(p1, val);
			break;
		case 1:
			fin >> p1 >> p2;
			fout << querry(p2) - querry(p1 - 1) << '\n';
			break;
		}
	}
}