Cod sursa(job #715461)

Utilizator SteveStefan Eniceicu Steve Data 17 martie 2012 12:03:10
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <fstream>
#include <cstring>

using namespace std;

int N, M, A[16001], C[16001];
int k, i, j;

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

void Citire ()
{
	fin >> N >> M;
	memset (C, 0, sizeof (C));
	for (i = 1; i <= N; i++)
	{
		fin >> A[i];
		k = 1;
		j = i;
		while (!(j & 1))
		{
			k <<= 1;
			j >>= 1;
		}
		k = i - (1 << (k - 1)) + 1;
		for (j = k; j <= i; j++)
		{
			C[i] += A[j];
		}
	}
}

void Scadere (int T, int V)
{
	while (T <= N)
	{
		C[T] -= V;
		k = 1;
		j = T;
		while (!(j & 1))
		{
			k <<= 1;
			j >>= 1;
		}
		T += (1 << (k - 1));
	}
}

void Interogare (int P, int Q)
{
	int S1 = 0, S2 = 0;
	while (P >= 1)
	{
		S1 += C[P];
		k = 1;
		j = P;
		while (!(j & 1))
		{
			k <<= 1;
			j >>= 1;
		}
		P -= (1 << (k - 1));
	}
	while (Q >= 1)
	{
		S2 += C[Q];
		k = 1;
		j = Q;
		while (!(j & 1))
		{
			k <<= 1;
			j >>= 1;
		}
		Q -= (1 << (k - 1));
	}
	fout << S2 - S1 << "\n";
}

void Business ()
{
	int a, b, c;
	for (i = 1; i <= M; i++)
	{
		fin >> a >> b >> c;
		if (a == 0) Scadere (b, c);
		else Interogare (b - 1, c);
	}
	fin.close ();
	fout.close ();
}

int main ()
{
	Citire ();
	Business ();
	return 0;
}