Cod sursa(job #715501)

Utilizator SteveStefan Eniceicu Steve Data 17 martie 2012 12:50:05
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <fstream>
#include <cstring>

using namespace std;

long long C[16001];
int N, M;
int A[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 - k + 1;
		for (j = k; j <= i; j++)
		{
			C[i] += A[j];
		}
	}
}

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

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

void Business ()
{
	int a;
	int b;
	int 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;
}